locked
How to Play a MP3 file Using MediaElement in Silverlight RRS feed

  • Question

  • This HowTo is provided via Microsoft’s Windows Phone 7 Developer Support team.  They are intended to serve as a guide in helping developers quickly find out how to do common tasks in their Windows Phone 7 applications.  They are not complete samples and often they are just demonstrating one of what could be many possible approaches to solve a given problem or task.

     

    Developers are free to incorporate any of the suggestions provided within their own code, but please review the following standard sample disclaimer:

    © 2011 Microsoft Corporation. All rights reserved. All sample code provided is not supported under any Microsoft standard support program or service. The samples are provided AS IS without warranty of any kind. Microsoft disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the samples and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the samples be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the samples or documentation, even if Microsoft has been advised of the possibility of such damages.

    How to Play a MP3 file Using MediaElement in Silverlight

    A common task in a media-based application is to be able to play sounds. While this should be simple, everything must be set up exactly right for it to work as expected.

    Assumptions

    The sample code below assumes:

    ·         A sound file named ‘Ding.mp3’ is included with the application in the SoundFiles folder

    ·         A button on the main page named ‘button1’ can be clicked to play the sound file.

    Details
    Silverlight applications can play MP3 and WMA audio files and there are several ways to include such files with your application. The sound file in the SoundFiles folder has its Build Action is set to Content, and Copy to Output Directory set to Copy if Newer.

    Sound files are played using the Silverlight MediaElement class; here are example steps:

    ·         Add a MediaElement to a page and name it ‘me’.

    ·         Ensure that the MediaElement is in the ‘stopped’ state and AutoPlay is set to False

    ·         Set the MediaElement’s Source property to the URI that identifies the sound file.

    ·         Play the sound

    The first trick to remember is that a sound file cannot be played until it has been successfully opened. This means that the last step must be performed in the MediaElement class’ MediaOpened event handler.

    XAML:
               
    <MediaElement Height="120" HorizontalAlignment="Left"

                        Margin="131,471,0,0" Name="me"

                        VerticalAlignment="Top" Width="160" AutoPlay="False" />

    This code is the default set by Visual Studio when you use the designer to add a MediaElement and doesn’t require modification if you clear the AutoPlay checkbox in the Properties window.

    C# code:
    The button1_Click event handler:

          private void button1_Click(object sender, RoutedEventArgs e)

          {

              me.Stop();

              me.Source = new Uri("SoundFiles/Ding.mp3", UriKind.Relative);

          }

    The me_MediaOpened event handler:

          private void me_MediaOpened(object sender, RoutedEventArgs e)

          {

              MediaElement m = (MediaElement)sender;

              m.Play();

          }

     

    The call to Stop is to ensure that the MediaElement is in an inactive state. Setting the Source property opens the media file and calling Play plays it. The last trick to remember is that no sound will play if the device is connected to your computer using a USB cable. Disconnect the device first.

    Related Tasks

    How to Play Video using Silverlight
    How to Play Sound using XNA

    Tuesday, January 25, 2011 11:56 PM

All replies

  • How would we go about playing several mp3/wma/other compressed audio at once?
    Creating several MediaElement tags doesn't seem to work. Creating them in C# code (new MediaElement() etc) doesn't seem to work either.
    Thursday, August 11, 2011 7:59 AM
  • You want to play several mp3 files at the same time?
    Thursday, August 11, 2011 11:02 AM
  • Yes, that is my exact question!
    Thursday, August 11, 2011 11:21 AM
  • Sorry to bump this again, but this problem eventually stopped my app in the validation process. It violates rule 6.5.1, which literally says you shouldn't stop music that is already played on the device (the Zune queue) with sound from your app, unless you expressly ask the user for permission to do so.

    Playing mp3 sounds using the way described above, will surely break this rule and your app will fail validation. If several mp3s could be played at the same time, I figure this will not be a problem. But I guess that mp3 playback is handled by the hardware which limits it to one mp3 at a time, am I right? I'm quite clueless right now how to tackle this problem. I'll not be able to convert all mp3 sounds to wav because that would make my 20 MB app around 200 MB big and no one wants that.

    And oh, I could pop up an alert asking the user for permission, but that would really feel too cumbersome and I'd avoid it if possible...
    Wednesday, August 31, 2011 12:36 PM
  • Ok, I had this working until I read the last post. I cannot use this technique if it is going to stop my app in the validation process. So, I am back to "square 1"...

    With that said, I have a very simple timer and counter app and I want to play a sound when an event is complete (e.g. timer done, counter complete, etc). What then is the easiest way to accomplish this task in a simple Windows Phone application page?
    Tuesday, November 8, 2011 8:25 PM
  • Can I play MP3 file while user are calling? As I tried all code samples here, music cannot be heard when phone call start. How can I listen to a MP3 file and a phone call concurrently?
    Sunday, January 1, 2012 8:47 AM
  • hey,
    is the mp3-file also playing using the emulator or does it only work on a real device?
    I'm using the emulator and it doesn't work but I'm not sure if I have done something wrong.

    I want to play a very short sound (2-3seconds). Is the MediaElement the best choice or is there a better way to do it?

    Timo

    Friday, January 20, 2012 2:35 PM

  • I want to play a very short sound (2-3seconds). Is the MediaElement the best choice or is there a better way to do it?
    SoundEffect is the best choice for that.
    Friday, January 20, 2012 7:53 PM
  • Note that MediaElement's documentation states that it won't play sound in the emulator (not that good design choice for the emulator in my opinion)

    Microsoft MVP J# 2004-2010, Borland Spirit of Delphi 2001

    Tuesday, July 15, 2014 2:51 PM
  • I can't make it to play sound from C# code directly either, probably one has to have MediaElement in the visual tree to make it work?

    Microsoft MVP J# 2004-2010, Borland Spirit of Delphi 2001

    Tuesday, July 15, 2014 2:52 PM
  • George: that is correct.

    Tuesday, July 15, 2014 2:57 PM
    Moderator