Reputation: 5837
I am creating a CustomMediaTransportControls for MediaPlayerElement
. In it, I have added a new button to Create a window with the currently playing video in CompactOverlay Mode. I have done it using below code.
//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;
}
But one problem is I want to resume the video from the point where the user clicks CompactOverlay button. How can I do it?
Upvotes: 0
Views: 683
Reputation: 15758
I think one possible solution for your problem is using Navigate(TypeName, Object) method to pass the current playback position to your VideoPlayerPage
and in VideoPlayerPage
code-behind file, override the OnNavigatedTo method to resume the video from current playback position.
For example, let's assume there are two MediaPlayerElement
named "mediaPlayerElement" in current page and VideoPlayerPage
. In current page, we can pass the current playback position to pass the current playback position
like the following:
//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);
However, CompactOverlayButton_ClickAsync
is a function in CustomMediaTransportControls
which is a derived class from MediaTransportControls. Form here we can't get the mediaPlayerElement
. So we need do some modifications here.
When adding a custom command button in custom transport controls, we usually will use a public EventHandler
to perform the action that occurs when the button is clicked. For more info, please see Adding a custom button in Create custom transport controls.
In this case, we can add a EventHandler
like:
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;
}
And in current page, use the CustomMediaTransportControls
like:
<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);
}
And then in VideoPlayerPage
code-behind file, override the OnNavigatedTo method like the following:
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();
}
After this, the video should be able to play from the position where the user clicks CompactOverlayButton
.
Upvotes: 1