views:

1118

answers:

3

I am seeing strange behaviour with the flash.media.Sound class in Flex 3.

var sound:Sound = new Sound();
try{
sound.load(new URLRequest("directory/file.mp3"))
} catch(e:IOError){
 ...
}

However this isn't helping. I'm getting a stream error, and it actually sees to be in the Sound constructor.

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

I saw one example in the Flex docs where they add an event listener for IOErrorEvent, SURELY I don't have to do this, and can simply use try-catch? Can I set a null event listener?

+1  A: 

You will need to add a listener since the URLRequest is not instantaneous. It will be very fast if you're loading from disk, but you will still need the Event-listener. There's a good example of how to set this up (Complete with IOErrorEvent handling) in the livedocs.

grapefrukt
+2  A: 

IOError = target file cannot be found (or for some other reason cannot be read). Check your file's path.

Edit: I just realized this may not be your problem, you're just trying to catch the IO error? If so, you can do this:

var sound:Sound = new Sound();
sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
sound.load(new URLRequest("directory/file.mp3"));

function ioErrorHandler(event:IOErrorEvent):void {
    trace("IO error occurred");
}
Antti
+1  A: 

try...catch only applies for errors that are thrown when that function is called. Any kind of method that involves loading stuff from the network, disk, etc will be asynchronous, that is it doesn't execute right when you call it, but instead it happens sometime shortly after you call it. In that case you DO need the addEventListener in order to catch any errors or events or to know when it's finished loading.

davr