07
Aug
07

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>

4 Responses to “Embedding sound effects in your Flex applications”


  1. 1 Sameer Sep 21st, 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 ?

  2. 2 peterd Sep 21st, 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

  3. 3 Dale Fraser Jan 9th, 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.

  4. 4 judah Jul 1st, 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.

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Firefox 2
  • Powered by Redoable 1.2
  • Feeds burnt by Feedburner
  • Feed