在Windows Store应用程序的每个页面上播放音频(mp3)C#XAML

问题描述:

我在Windows应用商店应用程序项目上有多个页面。在MainPage背景音频开始播放,但当我点击按钮并转到其他页面时,音乐已停止。有一个背景声音必须发挥每一页每一页都有自己的音频(MP3)。(我知道的MediaElement可以一次只播放一个音频)在Windows Store应用程序的每个页面上播放音频(mp3)C#XAML

我想这 http://www.c-sharpcorner.com/UploadFile/7e39ca/play-audion-on-background-in-windows-store-apps/

它的工作原理背景,但当更改页面音频已停止。

您是否尝试将上述背景播放器代码放入您的App.xaml/App.xaml.cs中?

+0

当我这些代码添加到app.xaml它给出错误

+0

在资源块中添加它时是否也会出现错误? –

+0

感谢您的回复,不,当我在块添加代码但它不开始播放音频时它不会给出错误。 –

不要使用mediaelement,使用背景音频播放器,如果你已经使用背景音频播放器,那么它应该像你想要的那样工作。如果没有,那么虽然它应该反正工作,因为它在后台运行,但你可以试试这个

论onNavigatedFrom事件的每一页暂停后台播放和回放的OnNavigatedTo事件

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     if ((BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing) && (BackgroundAudioPlayer.Instance.CanSeek)) 
     { 
      BackgroundAudioPlayer.Instance.Pause(); 
     } 
    } 

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Pause) 
      { 
       BackgroundAudioPlayer.Instance.PlayerState = BackgroundAudioPlayer.Instance.Play(); 
      } 
    } 
+0

BackgroundAudioPlayer给我错误。试图使用Microsoft.Phone.BackgroundAudio添加 ;但它也给我错误。 –

+0

什么是错误?你必须先创建一个背景音频应用程序,你知道吗? – Hamza

+0

是的,但我猜BackgroundAudioPlayer需要Microsoft.Phone.BackgroundAudio库,当我试图添加项目时,它会给出错误。我认为windows商店应用程序项目没有这个库。 –