开发者

Try Catch With Sound Element into Flash CS5-as3

Hi All—anyone has any idea why this code doesn’t work?

With other load [example image] work perfect with sound...no :(

mySoundURL = new URL开发者_运维知识库Request(var+".mp3"); 
mySoundURLDefault = new URLRequest("default.mp3"); 

try{ 
    sound.load(mySoundURL); 
}catch(e:IOErrorEvent){ 
    trace("Can't load sound: "+e); 
    sound.load(mySoundURLDefault);
} 

this is the error I’m getting:

Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error

Thanks and good day!


You do not use try/catch with loaders. Here's what you do instead:

sound.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorEvent);
sound.load(mySoundURL); 

private function onIOErrorEvent(e:IOErrorEvent):void{
  trace(e);
  trace(e.message);

  // ^  This will show you the file it tried to load and failed at

  e.currentTarget.removeEventListener(IOErrorEvent.IOError, onIOErrorEvent);
  e.currentTarget.removeEventListener(Event.COMPLETE, onComplete;
  // ^ don't forget to clean up your listeners!
}

The reason for this is, as you've seen, the uncaught error does not tell you anything helpful, such as which sound failed to load, or what the URL you tried to contact was.

I think @Alexander Sobolev is on the right track with his first guess, based on the fact that you still get an error. Handling errors from loaders is just different than handling errors from synchronous code, and try/catch will not help you here.

If you want to try playing an alt sound on fail from the first one, you'd do that in the onIOErrorEvent function.

Other times you'd listen for IOErrorEvent are when using Loader to load a swf or image file

 loader.contentLoaderInfo.addEventListener(IOErrorEvent....

Or a URLLoader

 urlLoader.addEventListener(IOErrorEvent...


It is not much information in the post, i suggest to check following conditions:

1) Is your var+".mp3" a correct URI? Does it point to the existing file? Just try in the browser - should it find the file? If you are pointing to the file in internet you should use notation like http://site.com/song.mp3 If you are trying to play a file on your hard drive you should use direct file address like C:/audio/song.mp3

2) If its ok with the URI than maybe file is not in mp3 format.

3) You can try to play with SoundLoaderContext like

            var mySoundURL = new URLRequest(var+".mp3")
            var context:SoundLoaderContext = new SoundLoaderContext(0, false);
            snd.load(mySoundURL , context);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜