2016-11-12 42 views
1

我正在創建一個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; 
       } 
      } 
     } 
    } 
+0

確保在UI線程調用DisplayRequest不是一個線程池線程。 –

+0

嗨,隊友,謝謝你的迴應,你能解釋一下你的意思嗎?我在包含mediaplayerelement的視圖的代碼後面調用它,我會說這是UI。歡呼 – hlovbeyond

+0

你在什麼事件處理程序中調用該方法?請在問題中包含引發異常的調用代碼。 –

回答

1

你需要調用UI線程RequestActivate方法。在非UI線程上調用該方法會導致異常:

不支持從MTA激活單線程類。

您已附上MediaPlayerElement_CurrentStateChanged方法,它發射的非UI線程的MediaPlayer.CurrentStateChanged事件,這導致與異常結束非UI線程調用RequestActivate

爲了解決這個問題,添加不同的方法來處理事件,並使用調度員UI線程來執行代碼:

private CoreDispatcher dispatcher; 
public MainPage() { 
    this.InitializeComponent(); 
    dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher; 
} 
private void Button_Tapped(object sender, TappedRoutedEventArgs e) { 
    .. 
    .. 
    Player.MediaPlayer.PlaybackSession.PlaybackStateChanged += PlaybackSession_PlaybackStateChanged; 
    .. 
} 
private PlaybackSession_PlaybackStateChanged(object sender, MediaPlaybackSession sender, object args) { 
    dispatcher.RunAsync(DispatcherPriority.Normal,() => { 
     // 
     // Code to keep display on 
     // 
    }); 
} 
+0

您好,先生,是一個傳奇。它像一個魅力。謝謝!!! – hlovbeyond

相關問題