简体   繁体   English

在Windows Phone中触摸按钮时,声音不会播放。 (按钮导航到下一页)

[英]Sound don't play when a button is touched in windows phone. (Buttons navigates to next page)

I am creating application, which use Button to Navigate to another page, and I want to keep sound when user touch the button. 我正在创建应用程序,该应用程序使用Button导航到另一个页面,并且我想在用户触摸按钮时保持声音。 The code works fine when there is button only, but when I use button to navigate to another page, the sound didn't play, But play when I press the Back button of Windows phone. 仅当有按钮时,该代码才能正常工作,但是当我使用按钮导航到另一页时,声音没有播放,但是当我按Windows Phone的“后退”按钮时,声音会播放。

public void playSound()
{
    MediaElement playSound1 = new MediaElement();
    playSound1.Source = new Uri("/Sound/Lionsound.mp3", UriKind.Relative);
    playSound1.Play();
}

void btnClassicPuzzle_Click(object sender, System.Windows.RoutedEventArgs e)
{
    playSound();
    NavigationService.Navigate(new Uri("/Menu/SelectPack.xaml", UriKind.Relative));
}

You are calling a function to play the sound on the button click and immediately executing the page naviagation. 您正在调用一个函数,以在单击按钮时播放声音并立即执行页面导航。 How compiler is processing it is after playSound1.Play() , the sound is started and it immediately calls the NavigationService . playSound1.Play() ,编译器如何处理它,声音开始并且立即调用NavigationService The page is changed and all the objects in the current pages are destroyed therefore, sound stops. 页面被更改,当前页面中的所有对象均被破坏,因此声音停止。 What you have to do is navigate to the next page on MediaEnded event so it can play complete sound before navigation 您需要做的是导航到MediaEnded事件的下一页,以便它可以在导航之前播放完整的声音。

<MediaElement MediaEnded="eventhandler" ../>

Referenced 已引用

Also you can check the MediaPlayer state by using MediaPlayer.State ; 您也可以使用MediaPlayer.State检查MediaPlayer状态; before navigation 导航之前

I think you can use a globlal MediaElement in your app. 我认为您可以在应用程序中使用全局MediaElement。

to use a global a global MediaElement you can follow these steps. 要使用全局MediaElement,可以按照以下步骤操作。

First in your app.xaml add a ControlTemplate 首先在您的app.xaml中添加一个ControlTemplate

         <ControlTemplate x:Key="AudioContentTemplate">
        <Grid x:Name="MediaElementContainer">

          <!-- The media element used to play sound -->
          <MediaElement Loaded="OnGlobalMediaLoaded"
                        Visibility="Collapsed" />

          <!-- Added for the normal content -->
          <Grid x:Name="ClientArea">
            <ContentPresenter />
          </Grid>
        </Grid>
      </ControlTemplate>

Second in your app.xaml.cs you have to declare the Globlal mediaElement and add methods to play and stop. 其次,您必须在app.xaml.cs中声明Globlal mediaElement并添加播放和停止的方法。

   private MediaElement globalMediaElement = null;

    private void OnGlobalMediaLoaded(object sender, RoutedEventArgs e)
    {
        if (this.globalMediaElement == null)
            this.globalMediaElement = sender as MediaElement;

    }


    public   void playMedia(Uri source)
    {
        this.globalMediaElement.Source = source;
        this.globalMediaElement.Play();
    }

    public void stopMedia()
    {
        this.globalMediaElement.Stop();
    }

Third in end of app.xaml.cs there is the initialization section and here you injected the global Mediaelement in the system with the template from the resource: 在app.xaml.cs的第三部分,有初始化部分,在这里您将全局Mediaelement与资源中的模板一起注入了系统中:

   private void Application_Launching(object sender, LaunchingEventArgs e)
   {


      RootFrame.Style = (Style)Resources["AudioContentTemplate"];

    }

And finally you can call it in your code behind 最后,您可以在后面的代码中调用它

  private void btnClassicPuzzle_Click(object sender, System.Windows.RoutedEventArgs e)
  {
    //play the globlal MediaElement 
     ((App)App.Current).playMedia(new Uri("/Sound/Lionsound.mp3", UriKind.Relative));

      NavigationService.Navigate(new Uri("/Menu/SelectPack.xaml", UriKind.Relative));
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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