2012-01-14 55 views
2

我正在使用NetStream播放本地.FLV文件。 NetStream接收FLV文件的名稱。AS3:檢查FLV文件是否存在

如何在嘗試播放之前檢查FLV是否存在?或者如果可能,試圖播放不存在的視頻時是否會發生一個事件?

// Doesn't catch an error if the FLV does not exist 
try { 
    ns.play("MyFLV.flv"); 
} catch (e:Error) { 
    trace("File does not exist"); 
} 
+0

這應做到: [檢查是否存在FLV] [1] [1]:http://stackoverflow.com/questions/3335790/how-to-檢查爲flv文件存在之前播放,使用flvplayback在 – John 2012-01-15 03:57:45

+0

我只使用NetStreams,而不是FLVPlayback類,所以這將無法正常工作。 – Abdulla 2012-01-15 04:12:50

回答

2

即使該文件不存在,Event.OPEN仍然會響應。將其更改爲ProgressEvent爲我工作。

fileTest.addEventListener(ProgressEvent.PROGRESS, fileTest_progressHandler); 

... 

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

您是否使用AIR?您可以使用File類:

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

,如果你正在開發一個Web播放壽沒有太大的幫助,你很可能使用URLLoader在這種情況下?像這樣?

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 
} 
0

警告:上面的代碼示例略有誤導。如果您忘記在路徑中放置一個前導斜槓,則上面設置nativePath字段的代碼將因錯誤2004而崩潰,因爲根相對路徑總是以正斜槓(在Macintosh上)開始。在Windows上,您可以輸入類似於「C:\ my \ folder \ path \ filename.txt」的路徑

通常,使用resolvePath函數更安全一些,它使用正斜槓創建獨立於平臺的路徑結構作爲子文件夾的分隔符。即使您需要根相對路徑,也可以使用resolvePath函數。在下面的代碼中,您可以看到我們已經創建了一個相對於applicationDirectory的路徑(這是AIR中的一個特殊的元文件夾名稱),但是如果子路徑有一個開始斜槓,那麼路徑將是根相對的。

var f:File = File.applicationDirectory.resolvePath(subpath); 
if (f.exists) 
    return T; 
else 
    return F;