Topic: Best way to have a webcam load images with a listener?
I'm working on some code to load a webcam using a listener, which would load the images at the fastest possible speed.
For those who don't know, a listener fires an event after the image is fully loaded. So right after frame 1 is loaded, it could start loading frame 2, etc. The current method used by the webcam sample can easily get overbuffered and display a blank screen.
I'm having a couple of problems with the listener:
- how to load the image into an invisible movie clip, and have it become visible only after loading?
- how to repeat the function without recursion? In other words, currently when the image is loaded it allows us to fire some event, and if we fire the doLoad function again that would be doLoad executing doLoad, which is a recursion problem.
Here's some starter code I have, does anyone have any ideas?
// URL of webcam
url = "http://216.203.123.137/jpg/image.jpg";
// movie clip to hold the webcam image
this.createEmptyMovieClip("cam", 1);
function doLoad() {
start_time = (new Date()).getTime();
// Defeat caching by adding a dummmy query string
var url = url + "?" + start_time;
// Our movie loader
var loader:MovieClipLoader = new MovieClipLoader();
loader.loadClip(url, cam);
var preload:Object = new Object();
loader.addListener(preload);
preload.onLoadStart = function(target){
trace("Started Loading");
}
preload.onLoadProgress = function (target, loadedBytes, totalBytes){
trace(Math.floor((loadedBytes/totalBytes)*100)+"% loaded");
}
preload.onLoadComplete = function(target){
finish_time = (new Date()).getTime();
total_time = (finish_time - start_time) / 1000;
trace ("Loaded " + url + " in " + total_time + " seconds");
// Scaling it for the tiny Chumby screen (320 x 240)
cam._yscale = 50;
cam._xscale = 50;
// Now load the next one!
// ?????
}
}
doLoad();