导航到另一帧后如何停止 MediaPlayerElement 播放音频?

Vij*_*mal 3 c# xaml win-universal-app uwp windows-10-universal

MediaPlayerElement 在导航到另一个 Frame 后正在播放音频。如果我再次导航到此 Frame,我可以听到两个音频实例,一个来自当前播放的视频的音频,另一个来自我上次导航到此 Frame 时的音频。导航到另一个框架后如何停止 MediaPlayerElement?

以供参考:

我的 XAML 部分框架的完整代码

<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>
Run Code Online (Sandbox Code Playgroud)

我的 C# 部分框架的完整代码

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);
}
}
Run Code Online (Sandbox Code Playgroud)

Han*_*nes 5

根据您离开页面时想要发生的情况,您可以例如通过将此方法添加到 MediaPlayer 所在页面中的代码隐藏来处置 MediaPlayer:

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

您可能还想在导航回页面时停止它并继续,因此您可能应该覆盖该void OnNavigatedTo(NavigationEventArgs e)方法。

  • 程序因 UNHANDLED_EXCEPTION 错误而崩溃。AnimeZeno.exe!AnimeZeno.App.InitializeComponent.AnonymousMethod__5_1(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e) 第 53 行 (2认同)