Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 838
  • Last Modified:

Silverlight sound effect on alert

I'm trying to add a simple sound effect when an alert box opens in a Silverlight application.
I have added an MP3 file to my project, created a MediaElement in my user control and assigned the MP3 file as the source. In my code, I fire the MediaElement .Play() method when the alert box is displayed. I get no errors, but also no sound, even though the play method is definitely being fired. Any ideas?

Here is the relevant XAML line in my control:

<MediaElement x:Name="effectsElement" HorizontalAlignment="Left" Margin="80,16,0,84" Width="100" AutoPlay="False" Source="boing_1.mp3" Volume="1"/>

1 Solution
You can try wrapping the MediaElement.Play() in a try catch block.  There's a chance it can't find the file correctly.  If it doesn't try dragging the .mp3 into the solution in VS3, as shown in the first picture attached.

If there are no exceptions caught in the try catch and it still doesn't play try changing the properties of the .mp3 so that it is included in the project as a resource, as shown in the last picture attached.

garybarnesAuthor Commented:
After following the suggestion of reloading the MP3 file by dragging it on to the control (using Expression Blend 3) the AutoPlay flag was set to true by default, and when the control loaded up the effect would play, so this confirmed that the file was being found.  It also now plays on demand. Note that the effect has to be reset using something like effectsElement.Position = New System.TimeSpan(0) for repeat effects.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now