Skip to main content
Known Participant
June 6, 2013
Answered

Can the AS3 FLVPlayback Component detect when there is no source file?

  • June 6, 2013
  • 2 replies
  • 1653 views

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); 

}

This topic has been closed for replies.
Correct answer moccamaximum

Here are some classes that check if a file already exists on a server:

http://www.kirupa.com/forum/showthread.php?328651-Checking-to-see-if-a-file-exists-before-loading

2 replies

moccamaximumCorrect answer
Inspiring
June 7, 2013

Here are some classes that check if a file already exists on a server:

http://www.kirupa.com/forum/showthread.php?328651-Checking-to-see-if-a-file-exists-before-loading

ankhcommAuthor
Known Participant
June 7, 2013

Those classes are great! Problem solved; thanks!

Ned Murphy
Legend
June 6, 2013

If you make thios as an AIR file you can change the design such that you read the contents of the folder into an array and use that rather than using a number-naming scheme where files potentially do not exist.

ankhcommAuthor
Known Participant
June 7, 2013

Thanks for the suggestion. I need to research what AIR is all about as I've never actually used it and was just looking for a quick fix.

Ned Murphy
Legend
June 7, 2013

You mentioned wanting to use a local folder.  AIR is capable of doing that while Flash is not... with Flash you would need to be dealing with a server and a server-side script to access the folder/file information.