MidnightGun
MidnightGun

Reputation: 1006

How to handle errors loading with the Flex Sound class

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?

Upvotes: 3

Views: 2261

Answers (3)

davr
davr

Reputation: 19137

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.

Upvotes: 1

Antti
Antti

Reputation: 3159

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

Upvotes: 5

grapefrukt
grapefrukt

Reputation: 27045

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.

Upvotes: 1

Related Questions