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>
 
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

18 Responses to Embedding sound effects in your Flex applications

  1. Sameer says:

    Do you know of a way to do text-speech in Flex ? Is there any library or example that shows how to do that ?

  2. peterd says:

    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

  3. Dale Fraser says:

    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.

  4. judah says:

    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.

  5. Rico says:

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

    Any suggestions?

  6. Anonymous says:

    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();

  7. Anonymous says:

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

    <mx:SoundEffect id="mySE" target="{this}"/>
  8. Anonymous says:
    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;      
    }
  9. Frans says:

    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

  10. ankur says:

    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?

  11. cris marc says:

    Anonymous thanks!

    Your solution came to the right time.

  12. Zied says:

    From where can I download sound ding.mp3 or others like PC alert ?

    • Peter deHaan says:

      @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

  13. Abid says:

    Can any one help me making a player that play only wav file

  14. George says:

    Anonymous, from 2009, you’re god

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree