如何通过C#寻找MediaPlayerElement

时间:2017-04-27 10:37:25

标签: c# xaml uwp win-universal-app

我正在为MediaPlayerElement创建CustomMediaTransportControls。在其中,我添加了一个新按钮来创建一个窗口,其中包含CompactOverlay Mode中当前播放的视频。我使用下面的代码完成了它。

//Button click event for CompactOverlayButton to Create a Frame in CompactOverlay mode
public async void CompactOverlayButton_ClickAsync(object sender, RoutedEventArgs e)
{
    int compactViewId = ApplicationView.GetForCurrentView().Id;      //Initializing compactViewId to the Current View ID
    await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        var frame = new Frame();
        compactViewId = ApplicationView.GetForCurrentView().Id;
        frame.Navigate(typeof(VideoPlayerPage));
        Window.Current.Content = frame;
        Window.Current.Activate();
        ApplicationView.GetForCurrentView().Title = "";
    });
    bool viewShown = await ApplicationViewSwitcher.TryShowAsViewModeAsync(compactViewId, ApplicationViewMode.CompactOverlay);
    compactOverlayButton.Visibility = Visibility.Collapsed;
}

但有一个问题是我想从用户单击CompactOverlay按钮的位置恢复视频。我该怎么办?

1 个答案:

答案 0 :(得分:1)

我认为您的问题的一种可能解决方案是使用Navigate(TypeName, Object)方法将当前播放位置传递到VideoPlayerPageVideoPlayerPage代码隐藏文件中,覆盖{{3} }方法从当前播放位置恢复视频。

例如,我们假设有两个MediaPlayerElement名为" mediaPlayerElement"在当前页面和VideoPlayerPage。在当前页面中,我们可以将当前播放位置传递给pass the current playback position,如下所示:

//Get current playback position
var position = mediaPlayerElement.MediaPlayer.PlaybackSession.Position;

int compactViewId = 0;
await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
    var frame = new Frame();
    compactViewId = ApplicationView.GetForCurrentView().Id;
    frame.Navigate(typeof(VideoPlayerPage), position);
    Window.Current.Content = frame;
    Window.Current.Activate();
    ApplicationView.GetForCurrentView().Title = "";
});
bool viewShown = await ApplicationViewSwitcher.TryShowAsViewModeAsync(compactViewId, ApplicationViewMode.CompactOverlay);

但是,CompactOverlayButton_ClickAsyncCustomMediaTransportControls中的一个函数,它是来自OnNavigatedTo的派生类。在此处,我们无法获得mediaPlayerElement。所以我们需要在这里进行一些修改。

在自定义传输控件中添加自定义命令按钮时,我们通常会使用公共EventHandler来执行单击按钮时发生的操作。有关详细信息,请参阅MediaTransportControls中的 Adding a custom button

在这种情况下,我们可以添加EventHandler之类的内容:

public event EventHandler<EventArgs> CompactOverlaid;
...
//Button click event for CompactOverlayButton to Create a Frame in CompactOverlay mode
public void CompactOverlayButton_Click(object sender, RoutedEventArgs e)
{               
    CompactOverlaid?.Invoke(this, EventArgs.Empty);
    compactOverlayButton.Visibility = Visibility.Collapsed;
}

在当前页面中,使用CustomMediaTransportControls之类的:

<local:CustomMediaTransportControls x:Name="CustomMediaControl"  CompactOverlaid="CustomMediaControl_CompactOverlaid"/>

private async void CustomMediaControl_CompactOverlaid(object sender, EventArgs e)
{
    //Get current playback position
    var position = mediaPlayerElement.MediaPlayer.PlaybackSession.Position;

    int compactViewId = 0;
    await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        var frame = new Frame();
        compactViewId = ApplicationView.GetForCurrentView().Id;
        frame.Navigate(typeof(VideoPlayerPage), position);
        Window.Current.Content = frame;
        Window.Current.Activate();
        ApplicationView.GetForCurrentView().Title = "";
    });
    bool viewShown = await ApplicationViewSwitcher.TryShowAsViewModeAsync(compactViewId, ApplicationViewMode.CompactOverlay);
}

然后在VideoPlayerPage代码隐藏文件中,覆盖Create custom transport controls方法,如下所示:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    /// set media source
    ...
    /// set media source

    if (e.Parameter is TimeSpan position)
    {
        mediaPlayerElement.MediaPlayer.PlaybackSession.Position = position;
    }

    mediaPlayerElement.MediaPlayer.Play();
}

在此之后,视频应该可以在用户点击CompactOverlayButton的位置播放。