Embedding sound effects in your Flex applications

by Peter deHaan on August 7, 2007

in Embed, MP3, SoundAsset, SoundEffect

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>

{ 10 comments… read them below or add one }

1 Sameer September 21, 2007 at 4:53 pm

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

Reply

2 peterd September 21, 2007 at 5:34 pm

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

Reply

3 Dale Fraser January 9, 2008 at 10:47 pm

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.

Reply

4 judah July 1, 2008 at 10:53 pm

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.

Reply

5 Rico March 22, 2009 at 8:58 pm

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

Any suggestions?

Reply

6 Anonymous July 18, 2009 at 9:07 am

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

Reply

7 Anonymous July 18, 2009 at 9:09 am

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

<mx:SoundEffect id="mySE" target="{this}"/>

Reply

8 Anonymous August 11, 2009 at 4:07 pm
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;      
}

Reply

9 Frans August 26, 2009 at 11:10 pm

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

Reply

10 ankur January 4, 2010 at 3:50 am

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?

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: