我正在创建一个Windows 10通用应用程序,该应用程序使用mediaPlayerElement播放托管在Internet服务器上的mp4文件。DisplayRequest不适用于Windows通用应用程序
视频播放良好,但屏幕锁定应用程序时暂停。我发现我应该使用DisplayRequest来关闭屏幕。我有包括https://msdn.microsoft.com/en-us/library/windows/apps/Windows.UI.Xaml.Controls.MediaPlayerElement.aspx 引用的代码,但仍然无法正常工作,当我调试计算机上的应用程序,在App调用DisplayRequest后崩溃,例外的是:
激活从MTA单线程类是不支持
我已经复制并粘贴了该示例,它针对x64和ARM上的周年更新。
任何帮助将不胜感激
干杯, ^ h以下
代码:
//Video url is on the button tag, I have commented out the code as it doesn't work
private void Button_Tapped(object sender, TappedRoutedEventArgs e)
{
var source = sender;
if (source.GetType() == typeof(Button))
{
var button = source as Button;
Player.Source = MediaSource.CreateFromUri(new Uri(button.Tag.ToString()));
//if ((Player.MediaPlayer != null) && (!isPlayerSet))
//{
// Player.MediaPlayer.PlaybackSession.PlaybackStateChanged += MediaPlayerElement_CurrentStateChanged;
// isPlayerSet = true;
//}
}
}
//I have copied the below from the referenced url
private void MediaPlayerElement_CurrentStateChanged(MediaPlaybackSession sender, object args)
{
MediaPlaybackSession playbackSession = sender as MediaPlaybackSession;
if (playbackSession != null && playbackSession.NaturalVideoHeight != 0)
{
if (playbackSession.PlaybackState == MediaPlaybackState.Playing)
{
if (appDisplayRequest == null)
{
// This call creates an instance of the DisplayRequest object
//This line throws the exception when debugging
appDisplayRequest = new DisplayRequest();
appDisplayRequest.RequestActive();
}
}
else // PlaybackState is Buffering, None, Opening, or Paused.
{
if (appDisplayRequest != null)
{
// Deactivate the display request and set the var to null.
appDisplayRequest.RequestRelease();
appDisplayRequest = null;
}
}
}
}
确保在UI线程调用DisplayRequest不是一个线程池线程。 –
嗨,队友,谢谢你的回应,你能解释一下你的意思吗?我在包含mediaplayerelement的视图的代码后面调用它,我会说这是UI。欢呼 – hlovbeyond
你在什么事件处理程序中调用该方法?请在问题中包含引发异常的调用代码。 –