Reputation: 53
I'm writing a UWP application which contains the MediaPlayer object. I am wanting to program a stop button which stops the media and sets the media to black however I can't find an approach to do it short of loading a short 'black' clip as an internal asset or hiding the element via the compositor.
The MediaPlayer does not have a function for stop, only play/pause and further, setting the media source to null simply pauses playback and freezes on the last frame shown rather than dropping to black.
Any suggestions would be much appreciated.
Upvotes: 3
Views: 2222
Reputation: 3243
I recently had a related problem that boiled down to unloading media from a MediaPlayerElement
.
If you navigate away from a page when playback is active, the MediaPlayerElement
would not suspend or unload a playback in progress so the user would keep hearing the audio. If you re-open player's page, a new instance of a player control will be created and user may hear multiple audio tracks at once (although this requires different media sources). So it was essential to unload the media at some place like player's Unloaded
event handler, page Unloaded
handler, or OnNavigatingFrom
/OnNavigatedFrom
overrides.
Anyway, the code below stops and unloads a video/clip/media from the MediaPlayerElement
.
MyMediaPlayerElement.MediaPlayer.Pause();
MyMediaPlayerElement.Source = null;
Actually, a null assignment line works just fine as a stop-and-unload, but since as of now (3/19/2018), MS documentation doesn't seem to cover how nullifying the source supposed to work, I prefer to keep an otherwise unnecessary call to Pause() here as well.
Upvotes: 2
Reputation: 10627
The MediaPlayer does not have a function for stop, only play/pause
As you known, MediaPlayerElement
doesn't have stop method at default. Since MediaPlayerElement
is newly for version 1607, for earlier version of Windows 10 we use MediaElement
instead, so you can simply use MediaElement
instead. MediaElement
has Stop
method, and after you invoke the stop method, the MediaElement
will show black and set the media to begin which is just what you want. Code as follows:
<MediaElement x:Name="mediaelement" AreTransportControlsEnabled="True" Height="400" Width="400" AutoPlay="True" Source="http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4"></MediaElement>
<Button x:Name="btnstop" Click='btnstop_Click' Content="stop"></Button>
Code behind:
private void btnstop_Click(object sender, RoutedEventArgs e)
{
mediaelement.Stop();
}
Upvotes: -1