导航到另一帧后如何停止 MediaPlayerElement 播放音频?
How to stop MediaPlayerElement playing audio after navigating to another frame?
MediaPlayerElement 在导航到另一个帧后正在播放音频。如果我再次导航到此帧,我可以听到两个音频实例,一个来自当前播放视频的音频,另一个来自我上次导航至此帧时的音频。如何在导航到另一个框架后停止 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>
框架的 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);
}
}
根据您离开页面时希望发生的情况,您可以通过将此方法添加到 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();
});
}
您可能还想停止它并在导航回页面时继续,因此您可能应该覆盖 void OnNavigatedTo(NavigationEventArgs e)
方法。
我 运行 也遇到过这个问题。即使等待,接受的答案也会导致未处理的异常。此处提供的示例代码中的覆盖处理程序应该如下所示。
protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
YoutubePlayer.Source = null;
}
MediaPlayerElement 在导航到另一个帧后正在播放音频。如果我再次导航到此帧,我可以听到两个音频实例,一个来自当前播放视频的音频,另一个来自我上次导航至此帧时的音频。如何在导航到另一个框架后停止 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>
框架的 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);
}
}
根据您离开页面时希望发生的情况,您可以通过将此方法添加到 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();
});
}
您可能还想停止它并在导航回页面时继续,因此您可能应该覆盖 void OnNavigatedTo(NavigationEventArgs e)
方法。
我 运行 也遇到过这个问题。即使等待,接受的答案也会导致未处理的异常。此处提供的示例代码中的覆盖处理程序应该如下所示。
protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
YoutubePlayer.Source = null;
}