(UWP) How to activate the “Taskbar Miniplayer” like in Groove

安稳与你 提交于 2021-02-07 20:15:37

问题


I use the BackgroundMediaPlayer for my App to play Audio in the Background. Now i see these buttons:

How can i activate them?


回答1:


In order to make the media controls from the taskbar to work, you need to load and configure the SystemMediaTransportControls from the foreground application AND the background task. If you are doing it only from the background task, the controls will be displayed but they will remain disabled.

In your foreground application, you should have the following code:

var smtc = SystemMediaTransportControls.GetForCurrentView();
smtc.ButtonPressed += smtc_ButtonPressed;
smtc.PropertyChanged += smtc_PropertyChanged;
smtc.IsEnabled = true;
smtc.IsPauseEnabled = true;
smtc.IsPlayEnabled = true;
smtc.IsNextEnabled = true;
smtc.IsPreviousEnabled = true;

And in the background task, you should have :

smtc = BackgroundMediaPlayer.Current.SystemMediaTransportControls;
smtc.ButtonPressed += smtc_ButtonPressed;
smtc.PropertyChanged += smtc_PropertyChanged;
smtc.IsEnabled = true;
smtc.IsPauseEnabled = true;
smtc.IsPlayEnabled = true;
smtc.IsNextEnabled = true;
smtc.IsPreviousEnabled = true;

Beware that the API to get the control instance is not the same:
SystemMediaTransportControls.GetForCurrentView() in the foreground app and BackgroundMediaPlayer.Current.SystemMediaTransportControls in the background task.

You will have to support the button pressed event in the two (foreground + background)




回答2:


That's System Media Transport Controls and you should add code to handle click event.
Here is official sample:

public MainPage()
{
this.InitializeComponent();

// Hook up app to system transport controls.
systemMediaControls = SystemMediaTransportControls.GetForCurrentView();
systemMediaControls.ButtonPressed += SystemControls_ButtonPressed;

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

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


来源:https://stackoverflow.com/questions/37786261/uwp-how-to-activate-the-taskbar-miniplayer-like-in-groove

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!