3
votes

I'm creating a little app to help me better understand how to play sounds on WP7 devices but I'm having a problem actually getting the sound to come out of the device.

I have the following code:

<MediaElement x:Name="note1" Source="test.mp3" AutoPlay="False" />

private void btn1_Click(object sender, RoutedEventArgs e)
{
    note1.Source = new Uri("test.mp3", UriKind.Relative);
    note1.Play();
}

Where test.mp3's Build Action is a Resource.

The thing I don't understand is when I add a breakpoint on the method btn1_Click and I stop at note1.Play() it actually plays test.mp3 but when debug without breakpoints and click on the button I hear nothing.

Is there a way to fix this issue?

6
I have no problem playing an MP3 the way you described. Is there any other action that you are doing after the button click?Den Delimarsky
I think the problem was that my sound was only 1 second long and I believe MediaElement doesn't like that. I used SoundEffect instead.Michael

6 Answers

3
votes

Have you tried playing with test.mp3's Build Action set as content.

Also did you close zune software after it recognizes the phone and completes sync, and connect using wp7connect tool. for more info about wp7connect tool try here. zune locks all media on wp7 device and you cant play any media, but the status of the media will be "ended". try setting up media's following events MediaFailed MediaOpened,MediaEnded, DownloadProgressChanged, CurrentStateChanged and BufferingProgressChanged

2
votes

Also, make sure you have add the capability ID_CAP_MEDIALIB to your manifest (WMAppManifest.xml), this seems to be required for MediaElement (otherwise you'll get AG_E_NETWORK_ERROR in your MediaFailed handler).

1
votes

i dont recommend mediaElement for more than one audio item ..it has weird effects ...use something like:

Stream stream = TitleContainer.OpenStream(@"Audio/buzzer.wav");

        SoundEffect effect = SoundEffect.FromStream(stream);
        FrameworkDispatcher.Update();
        effect.Play();

using the xna framework ....and make sure there WAV files.

0
votes

Uri kind must be RelativeOrAbsolute.

private void btn1_Click(object sender, RoutedEventArgs e)
{
    note1.Source = new Uri("test.mp3", UriKind.RelativeOrAbsolute);
    note1.Play();
}
0
votes

You need to make sure the MediaElement has been opened before you can call .Play() on it - you can do so by adding an event receiver to the MediaOpened event. It would also be good to call .Stop() any time prior to reassigning the Source property - take a look at this thread for more details.

0
votes

This can't be solved without an Eventhandler. Do as mentioned below.

 <MediaElement x:Name="note1" Source="test.mp3" AutoPlay="False" />

 private void btn1_Click(object sender, RoutedEventArgs e)
 {
   note1.Source = new Uri("test.mp3", UriKind.Relative);
   note1.MediaOpened += new RoutedEventHandler(note1_MediaOpened);
 }

  void note1_MediaOpened(object sender, RoutedEventArgs e)
    {
        note1.Play();
    }

this is perfectly works. enjoy...