2

I'm using a NetStream to play a local .FLV file. The NetStream receives the FLV file's name.

How can I check if the FLV exists before attempting to play it? Or if possible, is there an event thrown when trying to play a video that does not exist?

// Doesn't catch an error if the FLV does not exist
try {
    ns.play("MyFLV.flv");
} catch (e:Error) {
    trace("File does not exist");
}
Abdulla
  • 1,117
  • 4
  • 21
  • 44
  • This should do it: [check if flv exists][1] [1]: http://stackoverflow.com/questions/3335790/how-to-check-for-the-flv-file-existence-before-playing-that-using-flvplayback-in – John Jan 15 '12 at 03:57
  • I'm only using NetStreams, not the FLVPlayback class, so that won't work. – Abdulla Jan 15 '12 at 04:12

3 Answers3

3

Event.OPEN will still respond even if the file does not exist. Changing this to a ProgressEvent worked for me.

fileTest.addEventListener( ProgressEvent.PROGRESS, fileTest_progressHandler );

...

function fileTest_progressHandler( event:ProgressEvent ):void
{
    fileTest.close();
    // Your file exists
}
Nick
  • 46
  • 2
1

Are you using AIR at all? You could use the File class:

var f:File = new File();
f.nativePath = "path/to/your/FLV";
if (f.exists) 
{
    // Your file exists
}
else
{
   // Your file doesn't exist
}

Not much help if you're developing a webplayer tho, you could probably use the URLLoader in that case? Something like this?

var fileTest:URLLoader = new URLLoader();
fileTest.addEventListener( IOErrorEvent.IO_ERROR, fileTest_errorHandler );
fileTest.addEventListener( Event.OPEN, fileTest_openHandler );
fileTest.load( new URLRequest( "path/to/your/FLV" ));

function fileTest_errorHandler( event:Event ):void
{
    // Your file doesn't exist 
}

function fileTest_openHandler( event:Event ):void
{
    fileTest.close();
    // Your file exists
}
Michael
  • 3,776
  • 1
  • 16
  • 27
0

Warning: the code example above is slightly misleading. If you forget to put a leading slash into your path, the code above that sets the nativePath field will crash with error 2004, because a root relative path always starts with a forward slash (on the macintosh). On Windows you would put in a path like "C:\my\folder\path\filename.txt"

In general, you are much safer using the resolvePath function, which creates a platform independent path structure, using forward slash as the delimiter for subfolders. Even if you want a root relative path, you can use the resolvePath function. In this code below you can see that we have made a path relative to the applicationDirectory (which is a special meta-folder name in AIR), but if subpath had a beginning slash, the path would be root relative.

var f:File = File.applicationDirectory.resolvePath(subpath);
if (f.exists)
    return T;
else
    return F;
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Edward De Jong
  • 151
  • 1
  • 9