Playing a sound effect when an Alert control is displayed in Flex

by Peter deHaan on September 10, 2008

in Alert, SoundEffect

The following example shows how you can play an embedded MP3 when a Flex Alert control is displayed by setting the creationCompleteEffect style/effect.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/10/playing-a-sound-effect-when-an-alert-control-is-displayed-in-flex/ -->
<mx:Application name="Alert_creationCompleteEffect_test_2"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

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

            [Embed("assets/iconCritical.png")]
            private var IconCritical:Class;

            private function showAlert():void {
                var message:String = "The quick brown fox jumped over the lazy dog.";
                var title:String = "Alert title";
                var a:Alert = Alert.show(message,
                            title,
                            Alert.OK,
                            null,
                            null,
                            IconCritical);
                a.status = Capabilities.version;
                a.isPopUp = false;
            }
        ]]>
    </mx:Script>

    <mx:Style>
        Alert {
            creationCompleteEffect: ding;
        }
    </mx:Style>

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

    <mx:Button id="button"
            label="Launch Alert"
            click="showAlert();" />

</mx:Application>

View source is enabled in the following example.

You can also embed the MP3, specify the SoundEffect, and set the creationCompleteEffect in ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/10/playing-a-sound-effect-when-an-alert-control-is-displayed-in-flex/ -->
<mx:Application name="Alert_creationCompleteEffect_test_2"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

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

            [Embed("assets/iconCritical.png")]
            private const IconCritical:Class;

            [Embed("assets/ding.mp3")]
            private const DingSoundEffect:Class;

            private var ding:SoundEffect;

            private function init():void {
                ding = new SoundEffect();
                ding.source = DingSoundEffect;
                ding.useDuration = false;
            }

            private function showAlert():void {
                var message:String = "The quick brown fox jumped over the lazy dog.";
                var title:String = "Alert title";
                var a:Alert = Alert.show(message,
                            title,
                            Alert.OK,
                            null,
                            null,
                            IconCritical);
                a.setStyle("creationCompleteEffect", ding);
                a.status = Capabilities.version;
                a.isPopUp = false;
            }
        ]]>
    </mx:Script>

    <mx:Button id="button"
            label="Launch Alert"
            click="showAlert();" />

</mx:Application>

{ 5 comments… read them below or add one }

1 Anonymous September 13, 2008 at 10:03 am

Why i can’t hear anything when the dialog box displayed?

Reply

2 MusGar September 28, 2008 at 4:27 am

Yeap, im dont hear too :(

Reply

3 peterd September 28, 2008 at 8:44 am

Anonymous/MusGar,

What version/build of Flash Player are you guys using (the Alert control above should tell you the exact build number, or visit http://blog.flexexamples.com/about-you/)? Also, what browser/OS are you using?

I’m using the following browsers/Flash Players and hear the audio “beep” on each:
(a) Win 10,0,0,525 / Windows XP SP3 / Chrome
(b) Win 10,0,0,525 / Windows XP SP3 / Firefox 3.0.3
(c) Win 10,0,2,54 / Windows XP SP3 / IE7

Peter

Reply

4 Hashashino October 23, 2008 at 6:57 pm

Im running on Flash player 9 and the sound’s good.

Reply

5 Marc December 2, 2008 at 9:02 am

This tiny code also works:

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

<mx:Style>
       Alert { creationCompleteEffect: SndError; }
</mx:Style>

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: