How to handle error loading using the Flex Sound class

I see weird behavior 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 does not help. I get a stream error message and it really sees what is in the sound constructor.

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

I saw one example in Flex docs where they add an event listener for IOErrorEvent, WHAT should I not do this and can I just use try-catch? Can I set a null event listener?

+3
source share
3 answers

IOError = target file could not be found (or for some other reason could not be read). Check the file path.

: , , -? , :

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

, URLRequest . , , Event-listener. , ( IOErrorEvent) livedocs.

+1

try ... catch applies only to errors that occur when calling this function. Any method that involves downloading files from the network, disk, etc., will be asynchronous, i.e. It does not execute correctly when you call it, but instead, it happens some time after it is called. In this case, you need addEventListener to catch any errors or events or find out when they finished loading.

+1
source

Source: https://habr.com/ru/post/1697032/


All Articles