声音不在正确的时刻播放

我在我的Windows Phone应用程序中为背景音乐创建了一个MediaElement ,当我尝试在按下按钮为其添加声音时,它现在完全播放问题

当我调试时, 背景音乐会播放 ,但是当我按下按钮时,它会导航到下一页, 而不播放我创建的声音,并且当我从导航页BackButton时,它会播放它,为什么?

码:

private void play_Click(object sender, RoutedEventArgs e)
{
    MediaElement Click = new MediaElement();
    Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
    Click.Position = TimeSpan.Zero;
    Click.Volume = 1;
    LayoutRoot.Children.Add(Click);
    Click.Play();
    NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}

private void play_Click(object sender, RoutedEventArgs e)
        {
            MediaElement Click = new MediaElement();
            Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
            Click.Position = TimeSpan.Zero;
            Click.Volume = 1;
            LayoutRoot.Children.Add(Click);
            Click.Play();

        }

你应该处理这个外部按钮点击,

NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));

添加一个事件处理程序到单独的点击和呼叫,

 Click.MediaEnded += Click_Completed;

 void Click_Completed(object sender, RoutedEventArgs e)
        {
           NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
        }

或者您可以创建mediaElement的MediaEnded事件,并且您可以调用导航

喜欢这个:

private void play_Click(object sender, RoutedEventArgs e)
        {
            MediaElement Click = new MediaElement();
            Click.Source = new Uri ("/Assets/Sounds/press.mp3",UriKind.Relative);
            Click.Position = TimeSpan.Zero;
            Click.Volume = 1;
            LayoutRoot.Children.Add(Click);
            Click.MediaEnded += Click_MediaEnded;
            Click.Play();
        }

void Click_MediaEnded(object sender, RoutedEventArgs e)
       {
            NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
       }
链接地址: http://www.djcxy.com/p/25993.html

上一篇: sound not playing at the right moment

下一篇: C# playing multiple audio files at once from your resources?