Can the AS3 FLVPlayback Component detect when there is no source file?
Hello all,
I have an AS3 FLVPlayback component on stage that plays movies from a local folder. There are 22 movies in the folder and it chooses the one it will play randomly from a mathRandom function where it concatenates .flv to the number that was generated (1.flv, 2.flv, and so on).
For ease of maintenance I decided to control the range of the random numbers with a variable that is defined in an external text file, so if someone adds new movies all that person needs to do is go into the external text file and increase the value of the variable to reflect the number of movies in the folder (and any new movie would have to follow the naming convention which is number.flv). When I am in control of the files there's no issue, however I'm having trouble looking for a way to detect if the generated movie number exists or not (let's say the random number is 10 so 10.flv needs to be played but 10.flv doesn't exist in the folder). I just want to add an if statement that handles that if the chosen movie does not exist look for another but I can't seem to find any property that I can use to test that.
My FLVPlayback is named myMoviePlayer. I've tried:
myMoviePlayer.totalTime: this always returns NaN, whether the movie exists or not.
myMoviePlayer.preferredWidth: this always returns -1, whether the movie exists or not.
myMoviePlayer.state: this always returns loading, whether movie exists or not.
I tried all the other read-only properties defined for FLVPlayback but they always return the same value whether the movie exists or not. So then I tried tracing those properties but with myMoviePlayer.source and that always returns an error: "Access of possibly undefined property through a reference with static type String."
Anyway, this is my code as it stands right now as I try to solve this:
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, loading);
loader.load(new URLRequest("movies.txt"));
var extNumber;
function loading (event:Event):void {
extNumber = loader.data.nMovies;
ran = genRandomNum(1, extNumber);
trace(ran + ".flv");
myMoviePlayer.source = 25 + ".flv"; //I actually have extNumber defined as 22 in the movies.txt file so 25 should force an error
trace(myMoviePlayer.totalTime);
if (myMoviePlayer.totalTime > 0) {
trace ("It lives!")
}
}
myMoviePlayer.addEventListener(Event.COMPLETE, playAgain);
stage.displayState = StageDisplayState.FULL_SCREEN;
myMoviePlayer.fullScreenTakeOver = false;
stage.scaleMode = StageScaleMode.NO_SCALE;
function playAgain(e:Event):void {
trace("starting over");
trace(extNumber);
ran = genRandomNum(1, extNumber);
trace(ran);
myMoviePlayer.source = ran + ".flv";
myMoviePlayer.play();
}
var ran:Number = genRandomNum(1, extNumber);
function genRandomNum(minNum:Number, maxNum:Number):Number {
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}