如何在 UWP 中隐藏 MediaPlayer 控件

How to hide MediaPlayer control in UWP

我按照说明 here 使用 MedialPlayer 控件在我的应用程序中播放音效。声音是当应用中发生某些事情时播放的短音效 "in the background",用户不应看到任何类型的媒体播放控件。

代码相当简单,看起来像这样:

 MediaPlayer mPlayer = new MediaPlayer();
 mPlayer.Source =  MediaSource.CreateFromUri(pathUri);
 mPlayer.Play();

这很好用,除了当用户按下键盘上的音量控制按钮时,音量控制旁边会出现一个迷你媒体播放器控件,用户可以按下播放按钮再次播放上次播放的声音(见图)。我想隐藏这个。用户不应看到此内容或无法重播声音。

问题中提供的解决方案 14578867 不起作用。答案中提到的属性不存在(例如 IsPlayPauseVisible、uImode、IsInteractive)。我尝试使用 SystemMediaTransportControls 中的类似属性,但没有任何区别。我认为这些是针对出现在应用程序中的控件(我没有),而不是针对我想隐藏的 "OS media control"。

mPlayer.SystemMediaTransportControls.IsEnabled = false;
mPlayer.SystemMediaTransportControls.IsPlayEnabled = false;

我怎么能disable/hide这个?

这里是 step-by-step 重现问题的指南:

  1. 创建新的 Windows 通用 Visual C# 空白应用程序
  2. 将按钮添加到 MainPage.xaml 并将 mp3 文件添加到资产
  3. 将下面的代码粘贴到 MainPage.cs
  4. 运行 应用程序,点击按钮
  5. 在键盘上按提高音量按钮
  6. 观察 "media control" 音量控制旁边的播放按钮(见上图)。

按下播放按钮再次播放声音。如果你很快,你也可以暂停播放。 SystemMediaTransportControls 属性没有区别。

using Windows.Media.Core;
using Windows.Media.Playback;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace App2
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Tapped(object sender, TappedRoutedEventArgs e)
        {
            MediaPlayer mPlayer = new MediaPlayer();
            mPlayer.Source = MediaSource.CreateFromUri(new System.Uri("ms-appx:///Assets/clap.mp3"));
            mPlayer.SystemMediaTransportControls.IsPlayEnabled = false;
            mPlayer.SystemMediaTransportControls.IsEnabled = false;
            mPlayer.Play();
        }
    }
}

好像是MediaPlayer没准备好,当我们把false设置成MediaPlayer.SystemMediaTransportControls.IsPlayEnabled的时候。

我们应该可以添加MediaPlayerMediaOpened事件,然后我们可以在MediaOpened事件中设置falseMediaPlayer.SystemMediaTransportControls.IsPlayEnabled

例如:

MediaPlayer mPlayer;
private  void Button_Click(object sender, RoutedEventArgs e)
{
    mPlayer = new MediaPlayer();
    mPlayer.Source = MediaSource.CreateFromUri(new System.Uri("ms-appx:///Assets/xxxxx.mp3"));
    mPlayer.MediaOpened += MPlayer_MediaOpened;
    mPlayer.Play();
}

private void MPlayer_MediaOpened(MediaPlayer sender, object args)
{
    mPlayer.SystemMediaTransportControls.IsEnabled = false;
}

如果您只是禁用 SystemMediaTransportControls 中的按钮,您应该能够将 false 设置为 MediaPlayer.CommandManager.IsEnabled

If you are using MediaPlayer to play media, you can get an instance of the SystemMediaTransportControls class by accessing the MediaPlayer.SystemMediaTransportControls property. If you are going to manually control the SMTC, you should disable the automatic integration provided by MediaPlayer by setting the CommandManager.IsEnabled property to false.+

If you disable the MediaPlaybackCommandManager of the MediaPlayer by setting IsEnabled to false, it will break the link between the MediaPlayer the TransportControls provided by the MediaPlayerElement, so the built-in transport controls will no longer automatically control the playback of the player. Instead, you must implement your own controls to control the MediaPlayer.

有关详细信息,请参阅 Set up transport controls

例如:

_mediaPlayer = new MediaPlayer();
_systemMediaTransportControls = _mediaPlayer.SystemMediaTransportControls;
_mediaPlayer.CommandManager.IsEnabled = false;