Search code examples
c#uwpwindows-10-universalwindows-10-mobilebackground-audio

Windows 10 BackgroundMediaPlayer SystemTransportControls Pause Button Not Working


I have recently decided to add Background Audio playback support for podcasts in my app. I have got most of it to work but, the pause button in the SystemTransportControls of the BackgroundMediaPlayer doesn't seem to do anything.

Here is my background audio task class file:

public sealed class AudioPlayer : IBackgroundTask
{
    private BackgroundTaskDeferral _deferral;
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        _deferral = taskInstance.GetDeferral();

        var control = BackgroundMediaPlayer.Current.SystemMediaTransportControls;
        control.IsEnabled = true;
        control.IsPauseEnabled = true;
        control.IsPlayEnabled = true;
        control.IsNextEnabled = false;
        control.IsPreviousEnabled = false;

        taskInstance.Canceled += TaskInstance_Canceled;

        BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;
    }

    private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
    {
        BackgroundMediaPlayer.Shutdown();
        _deferral.Complete();
    }

    void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
    {
        object obj;
        e.Data.TryGetValue("MessageBody", out obj);

        string url = (string)obj;
        url = url.Replace("\"", "");
        var source = MediaSource.CreateFromUri(new Uri(url, UriKind.Absolute));

        var list = new MediaPlaybackList();
        list.Items.Add(new MediaPlaybackItem(source));

        BackgroundMediaPlayer.Current.Source = list;
        BackgroundMediaPlayer.Current.Play();
    }
}

It is important to note that the audio does start playing in the background but I don't have the ability to pause the audio.

Thanks, P.


Solution

  • To react to SystemMediaTransportControls (SMTC) and handle each Universal Volume Control (UVC) button: play, pause, next, and previous, we need handle the SystemMediaTransportControls.ButtonPressed event in Run method like following:

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        var control = BackgroundMediaPlayer.Current.SystemMediaTransportControls;
        control.ButtonPressed += control_ButtonPressed;
        ...
    }
    

    And in control_ButtonPressed method, deal with different buttons like:

    private void control_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
    {
        switch (args.Button)
        {
            case SystemMediaTransportControlsButton.Play:
                //Todo with play
                break;
            case SystemMediaTransportControlsButton.Pause:
                //Todo with pause
                break;
            case SystemMediaTransportControlsButton.Next:
                //Todo with skip to next;
                break;
            case SystemMediaTransportControlsButton.Previous:
                //Todo with skip to previous;
                break;
        }
    }
    

    For a complete sample, please refer to the official Background audio sample on GitHub, especially the MyBackgroundAudioTask class. And there is also a very nice post that will walk you through setting up background audio, reacting to device media controls, communicating with the background media player, and managing playlists. Read more at The Basics of Background Audio.