How do I stop playing the MediaPlayerElement after moving to another frame?

MediaPlayerElement plays audio after transition to another frame. If I go to this frame again, I can hear two instances of audio: "One" is from the audio of the currently playing video, and the other is from the last time I went to this frame. How do I stop the MediaPlayerElement after moving to another frame?

Reference:

All the code for my XAML part of the frame

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Styles/MediaPlayerDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>
<Grid Background="Transparent">
    <MediaPlayerElement  Name="YoutubePlayer" MaxWidth="640" MaxHeight="360" AreTransportControlsEnabled="True">
        <MediaPlayerElement.TransportControls>
            <video:CustomMediaTransportControls x:Name="CustomMediaControl" IsSkipBackwardButtonVisible="True" IsSkipForwardButtonVisible="True" IsSkipBackwardEnabled="True" IsSkipForwardEnabled="True" IsFullWindowButtonVisible="True" IsFullWindowEnabled="True" QualityChanged="CustomMediaControl_QualityChangedAsync"/>
        </MediaPlayerElement.TransportControls>
    </MediaPlayerElement>
</Grid>

      

All the code of my C # part of the frame

public sealed partial class VideosPage : Page
{

public VideosPage()
{
    this.InitializeComponent();
}

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    base.OnNavigatingFrom(e);
    YoutubePlayer.MediaPlayer.Dispose();
}

public async Task setVideoSourceAsync(YouTubeQuality videoQuality)
{

    try
    {
        var youtubeUrl = await YouTube.GetVideoUriAsync("QTYVJhy04rs", YouTubeQuality.Quality144P, videoQuality);
        YoutubePlayer.Source = MediaSource.CreateFromUri(youtubeUrl.Uri);
        YoutubePlayer.AutoPlay = true;
    }
    catch (Exception e)
    {
        //await setVideoSourceAsync();
    }
}

private async void Page_LoadedAsync(object sender, RoutedEventArgs e)
{
    if (ApplicationView.GetForCurrentView().IsViewModeSupported(ApplicationViewMode.CompactOverlay))
    {
        CustomMediaControl.IsCompactOverlayButtonVisible = true;
    }
    await setVideoSourceAsync(YouTubeQuality.Quality360P);
}

private async void CustomMediaControl_QualityChangedAsync(object sender, QualityChangedEventArgs e)
{
    await setVideoSourceAsync(e.NewQuality);
}
}

      

+3


source to share


1 answer


Depending on what you want to accomplish when you navigate from your page, you can, for example, remove the MediaPlayer by adding this method to your codeblock on the page that the MediaPlayer is on:

protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{  
    base.OnNavigatingFrom(e);
    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
    {
        YoutubePlayer?.MediaPlayer.Dispose();
    });
}

      



You can also just stop it and continue when you go to the page, so you should probably override the method void OnNavigatedTo(NavigationEventArgs e)

.

+2


source







All Articles