Silverlight sound effect on alert

Posted on 2010-01-07
Last Modified: 2013-11-12
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"/>

Question by:garybarnes
    LVL 5

    Accepted 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.

    LVL 1

    Author Closing Comment

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
    This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now