MediaElement Windows Phone 7

I am creating a small application to help me better understand how to play sounds on WP7 devices, but I had a problem with sound output from 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 Build Action is a resource.

What I do not understand is when I add a breakpoint in the btn1_Click method and stop at note1.Play (), it actually plays test.mp3, but when I debug without breakpoints and click on the button, I don’t hear anything.

Is there any way to fix this problem?

+3
source share
6 answers

You tried to play with test.mp3 Build Action installed as content.

zune , , wp7connect. wp7connect tool . zune wp7, - , "". : MediaFailed MediaOpened, MediaEnded, DownloadProgressChanged, CurrentStateChanged BufferingProgressChanged

+3

, , ID_CAP_MEDIALIB (WMAppManifest.xml), , , MediaElement ( AG_E_NETWORK_ERROR MediaFailed).

+2

mediaElement -. ... - :

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

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

xna framework.... , WAV .

+1

RelativeOrAbsolute.

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

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

0
source

This cannot be resolved without an Eventhandler. Do as 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();
    }

It works great. enjoy...

0
source

Source: https://habr.com/ru/post/1796665/


All Articles