简体   繁体   中英

Windows Phone 8.1: MediaElement not Playing after Frame.Navigate

I started building my own Windows Phone 8.1 App. I implemented a mp3 file. Whenever I implement it on the main page in the XAML

<MediaElement x:Name="GoalHorn" Source="/Sounds/mySound.mp3" AutoPlay="False" Visibility="Visible"></MediaElement>

I can call it in the source code and start it by

GoalHorn.Play()

I now wanted to put it on another frame. I used:

Frame.Navigate(typeof(ScoredPage)), scorerBox.Text);

However, when I want to start the sound on the new frame, nothing is done when calling

GoalHorn.Play()

I have it in the XAML of the new Frame aswell. When I set the autoplay to "true", it also works on the frame but I can't stop it.

Can anybody help?

It happens when media is not opened/loaded still and .Paly method is called on the media element. Add MediaOpened event handler and call play method in it.

<MediaElement MediaOpened="GoalHorn_MediaOpened" x:Name="GoalHorn" Source="/Sounds/mySound.mp3" AutoPlay="False" Visibility="Visible"></MediaElement>

Event handler

private void GoalHorn_MediaOpened(object sender, RoutedEventArgs e)
{
    GoalHorn.Play();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM