(UWP)如何激活"任务栏迷你层"喜欢在Groove中

时间:2016-06-13 09:33:33

标签: background media-player uwp windows-10-universal

我使用BackgroundMediaPlayer为我的应用程序在后台播放音频。现在我看到这些按钮:

Buttons

如何激活它们?

2 个答案:

答案 0 :(得分:1)

为了使任务栏中的媒体控件正常工作,您需要从前台应用程序和后台任务加载和配置SystemMediaTransportControls。如果您仅从后台任务执行此操作,则会显示控件但它们将保持禁用状态。

在前台应用程序中,您应该拥有以下代码:

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;

在后台任务中,您应该:

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;

请注意获取控件实例的API不一样:
SystemMediaTransportControls.GetForCurrentView()  在前台应用程序中,BackgroundMediaPlayer.Current.SystemMediaTransportControls在后台任务中。

您必须支持两个(前景+背景)

中的按钮按下事件

答案 1 :(得分:0)

那是System Media Transport Controls,您应该添加代码来处理点击事件 这是官方样本:

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;
  }
}