Search code examples
c#xamluwpwin-universal-appwindows-10-universal

How to stop MediaPlayerElement playing audio after navigating to another frame?


MediaPlayerElement is playing audio after navigating to another Frame. If I again navigate to this Frame, I can hear two instances of the audio, One is from the audio of the currently playing video and another is from the last time when I navigated to this Frame. How can I stop MediaPlayerElement after navigating to another Frame?

For Reference:

Entire Code of 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>

Entire 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);
}
}

Solution

  • Depending on what you want to happen when you navigate away from your Page you could for example dispose the MediaPlayer by adding this method to your codebehind in the page the MediaPlayer is in:

    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 may also want to just stop it and continue when navigated back to the Page therefore you should probably override the void OnNavigatedTo(NavigationEventArgs e) method.