我正在創建一個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
你在什麼事件處理程序中調用該方法?請在問題中包含引發異常的調用代碼。 –