如何播放背景音频Windows Phone 8.1

时间:2015-05-28 04:45:36

标签: c# windows-phone-8.1

我阅读了this关于"如何在后台播放音频的文章(XAML)"并且工作,我很好地播放了我的mp3文件,但如果我尝试从应用程序音乐停止,我想"背景音频"即使应用程序没有关注屏幕也要播放!

XAML

<Grid>
    <MediaElement x:Name="musicPlayer" 
      Source="Assets/VIGEVANO.mp3"
      AudioCategory="BackgroundCapableMedia"
      CurrentStateChanged="MusicPlayer_CurrentStateChanged" />

</Grid>

CS

SystemMediaTransportControls systemControls;
public MainPage()
    {

        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
        // Hook up app to system transport controls.
        systemControls = SystemMediaTransportControls.GetForCurrentView();
        systemControls.ButtonPressed += SystemControls_ButtonPressed;

        // Register to handle the following system transpot control buttons.
        systemControls.IsPlayEnabled = true;
        systemControls.IsPauseEnabled = true;

    }

private void SystemControls_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
    {
        switch (args.Button)
        {
            case SystemMediaTransportControlsButton.Play:
                PlayMedia();
                break;
            case SystemMediaTransportControlsButton.Pause:
                PauseMedia();
                break;
            default:
                break;
        }
    }
    async void PlayMedia()
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            musicPlayer.Play();
        });
    }

    async void PauseMedia()
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            musicPlayer.Pause();
        });
    }

Manifest File

我想念的是什么?

1 个答案:

答案 0 :(得分:0)

达成总结答案 由于背景流是主要要求所以解决方案达到了 是查看此sample并告诉我它是否适合您。 您的后台任务第一个教程适用于本地mp3文件,但流式音频本身就是一个不同的场景 另外,对于流式传输,我们曾使用PhoneSM