sound not playing at the right moment
I created a MediaElement for a Background Music in my Windows Phone App and it plays perfectly now the problem when I tried to add sound for a button when pressed
When I debug , the Background music plays , but when I press the button it navigates me to the next page without playing the sound I created, and when I press BackButton from the Navigated Page , it plays it, why?
code:
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();
}
you should handle this outside button click,
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
Add an event handler to the click and call separately,
Click.MediaEnded += Click_Completed;
void Click_Completed(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/NavPage.xaml", UriKind.Relative));
}
or you can make MediaEnded event of the mediaElement and there in you can call the navigation
Like this:
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/25994.html
上一篇: 一次播放一个声音
下一篇: 声音不在正确的时刻播放