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>
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.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
18 Responses to Embedding sound effects in your Flex applications
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Do you know of a way to do text-speech in Flex ? Is there any library or example that shows how to do that ?
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
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.
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.
I’ve tried your code but Flex 3 gives me a couple of messages: unsupported sampling rate 16000HZ) and Unable to Transcode.
Any suggestions?
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
Tried to include this link:
http://stackoverflow.com/questions/4690285/unsupported-sampling-flex-actionscript
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();
Sorry… forgot too make my mxml “HTML-friendly”. Here is the mxml code:
<mx:SoundEffect id="mySE" target="{this}"/>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
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?
Anonymous thanks!
Your solution came to the right time.
From where can I download sound ding.mp3 or others like PC alert ?
@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
Can any one help me making a player that play only wav file
@Abid,
I believe that Flash Player only supports .MP3 playback, not .WAV files.
Peter
Anonymous, from 2009, you’re god