Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Jun 06, 2013 Jun 06, 2013

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

}

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Jun 07, 2013 Jun 07, 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

Translate
LEGEND ,
Jun 06, 2013 Jun 06, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2013 Jun 07, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 07, 2013 Jun 07, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2013 Jun 07, 2013
LATEST

I'm using a Flash projector; I've never had a problem reading from local files doing it that way so I went for the tried and true. After solving it that way I can see that it wouldn't have taken much time to use an AIR as you mention. I'll have to spend some time learning about AIR now to be able to discern when using it will prove the better solution.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 07, 2013 Jun 07, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2013 Jun 07, 2013

Those classes are great! Problem solved; thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines