声音没有在正确的时间播放
本文关键字:时间 播放 声音 | 更新日期: 2023-09-27 17:51:08
我在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和call添加事件处理程序,
Click.MediaEnded += Click_Completed;
void Click_Completed(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}
或者你可以设置mediaElement的MediaEnded事件并在其中调用navigation
:
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));
}