Try Catch With Sound Element into Flash CS5-as3

若如初见. 提交于 2019-12-11 17:52:19

问题


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

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

mySoundURL = new URLRequest(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!


回答1:


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...



回答2:


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


来源:https://stackoverflow.com/questions/4977909/try-catch-with-sound-element-into-flash-cs5-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!