A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: How to best check if a Sound is loadable

  1. #1
    Senior Member
    Join Date
    Jul 2008
    Posts
    418

    How to best check if a Sound is loadable

    Edit:
    If i import a sound in the flash library, and then change the mp3 outside flash,
    will the changes be in the new SWF, or do i have to reimport them?




    I've created a load system to load external sound files, but i need a way to handle if the load went wrong, in which case he would simply not play the file.
    Right now I get an error all the time. i just want it to not play the sound if it's unloadable.

    how do i do this?

    edit: It's unloadable sometimes when it can't access the files on another server.
    Last edited by omniscient232; 04-17-2010 at 04:56 AM.
    I program in AS3 only.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Sounds like all you need to do is gracefully handle the error condition. If it's an error event, catch that event. If it's an actual exception, use try/catch to catch it.

  3. #3
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    Hey.
    I took your advice:
    PHP Code:
    var sound:Sound null;
                var 
    nUrl:URLRequest = new URLRequest("mp3ss/"+url)
                try
                {
                    
    sound = new Sound(nUrl);
                }
                catch (
    e:*) {return null;} 
    I made sure the soundfile didn't exist to test, but it still says: unhandled error event.
    What is wrong with this code?
    I program in AS3 only.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It looks like you have an error EVENT, not an ERROR. You'll need to add a listener for that event. Can you post the full error message you are getting?

  5. #5
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    this is the error:
    Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.
    at SoundLoader/loadSound()
    at SoundLoader/doLoadBackgroundMusicSounds()
    at SoundLoader/doLoadGameSounds()
    at SoundLoader/startLoading()
    at MainController/addedToStage()
    I program in AS3 only.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yep, that's an event. Those are not caught by try/catch because they are not really Errors.

    You'll need to separate the sound instantiation and loading so that you can add your listener before the load.
    Code:
    var sound:Sound = new Sound();
    var nUrl:URLRequest = new URLRequest("mp3ss/"+url)
    sound.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
    sound.load(nUrl);
    
    function handleIOError(e:Event):void{
      trace("whoops, sound not available due to IO error");
    }

  7. #7
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    Well this is actually a bit of a problem.
    Because the thing is. that function should have returned null if the sound load failed.
    Is that possible with this method?
    Or: When i now play the sound, what will happen if the load went wrong?
    will it just not play?
    I program in AS3 only.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Because the load is asynchronous, you can't know whether it works or not before that function finishes.

    I don't know what will happen if you call play without a valid sound loaded into your Sound object.

  9. #9
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    I've decided I'm probably going to embed the sounds into my swf.
    I've got tons of sounds. right now I just call a function loadSound(url:string) for each
    how do i do this when i want to embed:
    PHP Code:
    package
    {
        
    import flash.display.Sprite;
        
    import flash.media.Sound;
        
    import flash.media.SoundChannel;

        public class 
    EmbeddedSoundExample extends Sprite
        
    {
            [
    Embed(source="smallSound.mp3")]
            public var 
    soundClass:Class;
            
            public function 
    EmbeddedSoundExample()
            {
                var 
    smallSound:Sound = new soundClass() as Sound;
                
    smallSound.play();
            }
        }

    2 questions:
    first: I don't understand from the above code that i got from the internet how to use the sound you've embedded.
    second: Can i embed entire maps of sounds at once?, or do i have to write an
    [Embed(source="smallSound.mp3")]
    line for every mp3 file?
    I program in AS3 only.

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You use the Sound made from the embedded mp3 in exactly the same way you use the Sound loaded from an external mp3 file. The only difference is in how those bytes become a Sound instance.

    To use the Embed directive like that, you have to be using the flex compiler. You do have to embed each file independently.

  11. #11
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    Ah. i see.
    How do i do it in flash?
    Is there another way than simply importing it in the library? which is kind of a nuicance if you want to change the sounds often.
    otherwise, i'll just do it that way.
    I program in AS3 only.

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I think that you do just have to import the mp3s into the library in Flash, but I almost never use Flash itself so I'm not sure.

  13. #13
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    If i import a sound in the flash library, and then change the mp3 outside flash, will the changes be in the new SWF, or do i have to reimport them?
    I program in AS3 only.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center