Embedding sound effects in your Flex applications

I was investigating a bug today with embedded sounds over a Remote Desktop Connection and came up with the following code that I thought I’d share.

I tested three different methods of embedding sound effects into a Flex application:
1) Using the <mx:SoundEffect /> tag, an inline @Embed, and mouseDownEffect
2) Using the [Embed] metadata, <mx:SoundEffect /> with a binding to my embedded asset, and mouseDownEffect
3) Using the [Embed] metadata, the SoundAsset class, and the SoundAsset.play() method.

Full code after the jump.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            private var alert:Alert;

            private function showAlert():void {
                alert = Alert.show("Are you sure you want to delete the internet?", "Confirm delete...", Alert.YES | Alert.NO);
            }
        ]]>
    </mx:Script>

    <mx:SoundEffect id="soundEffect" source="@Embed(source='assets/ding.mp3')" />

    <mx:Button label="Delete Internet?" click="showAlert();" mouseDownEffect="{soundEffect}" />

</mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;

            [Bindable]
            [Embed('assets/ding.mp3')]
            private var ding_mp3:Class;

            private var alert:Alert;

            private function showAlert():void {
                alert = Alert.show("Are you sure you want to delete the internet?", "Confirm delete...", Alert.YES | Alert.NO);
            }
        ]]>
    </mx:Script>

    <mx:SoundEffect id="soundEffect" source="{ding_mp3}" />

    <mx:Button label="Delete Internet?" click="showAlert(); " mouseDownEffect="{soundEffect}" />

</mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.core.SoundAsset;

            [Embed('assets/ding.mp3')]
            private var ding_mp3:Class;

            private var ding:SoundAsset = new ding_mp3() as SoundAsset;

            private var alert:Alert;

            private function showAlert():void {
                alert = Alert.show("Are you sure you want to delete the internet?", "Confirm delete...", Alert.YES | Alert.NO);
            }
        ]]>
    </mx:Script>

    <mx:Button label="Delete Internet?" click="showAlert(); ding.play()" />

</mx:Application>

18 thoughts on “Embedding sound effects in your Flex applications

  1. Sameer,

    I haven’t heard of any text to speech engines lately. I vaguely recall Robert Hall working on one several years ago, but I don’t remember how far he got with it, or if he released the source code. But I don’t think I remember it being ActionScript 3.0. You can try searching his blog at http://www.impossibilities.com/blog/ and see if you can find any more information.

    A better place to ask would be on the FlexCoders mailing list, as it currently has over 7600 members, and I’m sure somebody may have implemented/researched this. You can find more information about the FlexCoders mailing lists at http://tech.groups.yahoo.com/group/flexcoders/.

    Good luck and happy Flexing!
    Peter

  2. Love your stuff.

    Is it possible to play a sound effect from actionScript.

    ie, what actionscript code would I need to play that.

    I would have thought that

    sndBuzzer.play() would have done it, it doesn’t error but no sound.

  3. thanks peter. how would you fade in a looping sound over a 2000 duration? for example, lets say you have a 12 second loop for background music and you want to fade that in.

  4. I’ve tried your code but Flex 3 gives me a couple of messages: unsupported sampling rate 16000HZ) and Unable to Transcode.

    Any suggestions?

    1. Your comment is old, but for anyone else reading, I found the reason this happens. Flex only supports certain sampling rates, so you have to re-encode your mp3. See

  5. I have to leave a comment regarding this because I desperately needed to use the SoundEffect.play() method and this was the closest related topic under a Google search. It took me about 3 hours to find out why SoundEffect.play() doesn’t trigger the effect.

    When SoundEffect.play() doesn’t work, it is because the “target” property for the SoundEffect object hasn’t been defined. The below MXML illustrates a SoundEffect component where the play() method will launch the effect:

    ;

    Notice the need for binding tags. Essential… otherwise your app will throw an error. After the target has been defined, you can then call the play() method programmatically:

    mySE.play();

  6. Sorry… forgot too make my mxml “HTML-friendly”. Here is the mxml code:

    <mx:SoundEffect id="mySE" target="{this}"/>
  7. private var mySound:Sound = new Sound();
    private var audio:SoundChannel = new SoundChannel();
     
    private function start():void
    {
        mySound = new Sound(new URLRequest("http:// or local file"));
        audio = mySound.play();
    }         
     
    private function stop():void
    {
        audio.stop();
        mySound = null;      
    }
  8. Anonymous thanks!

    It took me quite some while to get sound running until I found your message and noticed that the target=”{this}” part was missing. After fixing that it worked like a charm.

    Unbelievable this is not the default value.

    Frans

  9. i am trying to put a music loop which plays in the background for the client as long as he is on the website(he should of course be able to turn it off) how can i do this?

    1. @Zied,

      I’m using Windows 7 and I think I got the sounds from the C:\Windows\Media\ folder. There are a bunch of .WAV files in there which I probably converted to .MP3 files using iTunes or something. Just search your \Windows\ folder for *.wav and you should probably get a bunch of results that the OS uses for various events (ie: logon, logoff, system errors, etc).

      Peter

Comments are closed.