Reputation: 3077
I am using MedialPlayer control to play sound effects in my app as explained here. Sounds are short sound effects that play "in the background" when something happens in the app, user should not see any sort of media playback control.
Code is fairly straight forward, looks something like this:
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.Source = MediaSource.CreateFromUri(pathUri);
mPlayer.Play();
This works well, except, when user presses volume control button on the keyboard, a mini media player control appears next to volume control and the user can press play button to play the last sound again (see picture). I want to hide this. User should not see this or be able to replay sounds.
Solutions offered in question 14578867 do not work. Properties mentioned in the answers do not exist (e.g. IsPlayPauseVisible, uImode, IsInteractive). I tried using similar properties from SystemMediaTransportControls but it makes no difference. I think these are meant for the control that appears in the app (which I do not have), not for the "OS media control" that I want to hide.
mPlayer.SystemMediaTransportControls.IsEnabled = false;
mPlayer.SystemMediaTransportControls.IsPlayEnabled = false;
How can I disable/hide this?
Here is a step-by-step guide to replicate the issue:
Pressing play button plays the sound again. If you are quick you can also pause the playback. SystemMediaTransportControls properties make no difference.
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();
}
}
}
Upvotes: 3
Views: 2477
Reputation: 3286
It seems it is caused by the MediaPlayer is not ready, when we set false
to the MediaPlayer.SystemMediaTransportControls.IsPlayEnabled
.
We should be able to add MediaOpened
event of the MediaPlayer
, then we can set false
to the MediaPlayer.SystemMediaTransportControls.IsPlayEnabled
in the MediaOpened
event.
For example:
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;
}
If you just disable the button that in SystemMediaTransportControls, you should be able to set false to 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.
For more info, see Set up transport controls.
For example:
_mediaPlayer = new MediaPlayer();
_systemMediaTransportControls = _mediaPlayer.SystemMediaTransportControls;
_mediaPlayer.CommandManager.IsEnabled = false;
Upvotes: 3