I am making a UWP program with a Media Player Element in it. My code is the following:
XAML
<Grid>
<Grid.Background>
<SolidColorBrush Color="#FF000000"/>
</Grid.Background>
<MediaPlayerElement x:Name="Player"
Stretch="Uniform"
AreTransportControlsEnabled ="True"
AutoPlay="True"
IsHoldingEnabled="False"
IsRightTapEnabled="False">
<MediaPlayerElement.TransportControls>
<MediaTransportControls IsZoomButtonVisible="False" IsZoomEnabled="False" KeyDown="MediaTransportControls_KeyDown" RequiresPointer="WhenEngaged" />
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
<ProgressRing x:Name="EpisodeProgress" HorizontalAlignment="Center" VerticalAlignment="Center" Height="80" Width="80"/>
</Grid>
C#:
public sealed partial class Media_Player : Page
{
public Media_Player()
{
this.InitializeComponent();
Player.TransportControls.DoubleTapped += SingleMediaElement_DoubleTapped;
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
EpisodeProgress.IsActive = true;
Uri source = new Uri("https://mediaplatstorage1.blob.core.windows.net/windows-universal-samples-media/sintel_trailer-480p.mp4");
Player.Source = MediaSource.CreateFromUri(source);
EpisodeProgress.IsActive = false;
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
if (args.VirtualKey == Windows.System.VirtualKey.Space)
{
if (Player.MediaPlayer.PlaybackSession.PlaybackState == Windows.Media.Playback.MediaPlaybackState.Playing)
{
Player.MediaPlayer.Pause();
}
else if (Player.MediaPlayer.PlaybackSession.PlaybackState == Windows.Media.Playback.MediaPlaybackState.Paused)
{
Player.MediaPlayer.Play();
}
}
}
private void SingleMediaElement_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
var view = ApplicationView.GetForCurrentView();
if (view.IsFullScreenMode)
view.ExitFullScreenMode();
else
view.TryEnterFullScreenMode();
}
}
So what I would like to do is use the spacebar on the keyboard to pause/play the video using a KeyDown Event. What is the best place to attach this event to? Attaching it to the Core Window still only works some of the time as in fullscreen mode the event fires twice for some reason. Also, when it is attached in the current setup, the double-tapped event does not fire anymore either. Before adding the the Key handler, the double-tapped worked perfectly. What am I doing wrong and where should I attach an event to listen for the key in both windowed and fullscreen?
Thanks!