Actionscript 3 - Test if URLRequest fails
I have a music player that loads 2 files, and then plays the 2 files on 2 separate soundchannels. I need to test to see if the 2nd music file is able to load, and then let the play controls check for this to determine how many sound channels to play. Can I do something like:
var soundFile2:URLRequest = new URLRequest("mp3Player/king.mp3");
....开发者_运维问答
myMusic2.load(soundFile2, myContext);
if (soundFile2 != null){
channel2 = myMusic2.play(channel.position);
}
You need to add event listeners, for an Event.COMPLETE and for an IOErrorEvent.IO_ERROR:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html#includeExamplesSummary
Loading files is asynchronous, so you can't check for a result on the line after the load call in your code example, you have to wait for a response that comes at a later time, by using an event listener.
You could use a URLLoader
as illustrated in response to Adobe Air how to check if URL is online\gives any response exists?
Even though the question says "AIR", the answer uses only the flash.events
and flash.net
libraries. The major downside for the non-AIR version is that you can't do "HEAD" requests, but only "GET". Since you're planning on loading the files if they are available, this shouldn't be an issue for you.
精彩评论