Search code examples
actionscript-3audioairembedflash

Why isn't this embedded sound file playing?


I've been going through a few tutorials, trying to find how to embed audio files and play them in AS3, and they tend to show examples which are very similar. However when I try to use these examples, nothing is played. My mp3 file will be embedded successfully, all the lines of code will execute sucessfully, but there will just be no sound. Take the following code, for instance:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.media.Sound;

    public class Main extends Sprite 
    {
        [Embed(source='/../lib/Kalimba.mp3')]        
        private var MySound : Class;         
        private var sound : Sound;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            sound = (new MySound()) as Sound;
            sound.play();
        }       
    }   
}

What's going wrong here? Many examples online basically use this code, just changing variable names and the like. I'm using FlashDevelop, in case that makes any difference. Thanks.

EDIT Apparently it's somehow linked to that file. I tried Kalimba.mp3, Maid with the Flaxen Hair.mp3, and Sleep Away.mp3, all of which are Windows 7 defaults in Libraries\Music\Sample Music. None of them worked. Then I downloaded a random mp3 file elsewhere and tried to use it, and it worked just fine (Blackbird Blackbird - Heartbeat.mp3 from http://www.last.fm/music/+free-music-downloads/sample). I have tried using converters to make sure Kalimba was at 44100 Hz with a bitrate of 128 kbps, but that didn't seem to work. What's the difference?


Solution

  • It worked perfectly for me with your code. Check your mp3 file. Try a different file and see if it works.

    Also check for silly mistakes like:

    • Have you used addChild() to add instance of Main class? The init() in your Main.as will fire only after you've added it to display list
    • Is the pathing in your source correct? I've changed my path to "test.mp3", put your mp3 in a root folder to be sure.