简体   繁体   中英

UWP MediaPlayerElement Keyboard events

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!

When the MediaPlayerElement is under the full window mode, the space bar already has default behavior that key down will pause/play the video that you will not need to control it. So you can just attach the key down event to core window but add a condition to judge if the player is under full window mode then determine whether to execute the custom key down event. Otherwise your custom behavior and the default will cause event twice fired. Updated CoreWindow_KeyDown code as follows.

private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
{
    if (args.VirtualKey == Windows.System.VirtualKey.Space && !Player.IsFullWindow)
    {
        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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM