DisplayRequest无法在Windows通用应用程序上运行

时间:2016-11-12 10:58:58

标签: win-universal-app

我正在创建一个Windows 10通用应用,它使用mediaPlayerElement播放托管在互联网服务器上的mp4文件。

视频效果很好但是当屏幕锁定时,应用程序暂停了。我发现我应该使用DisplayRequest来关闭屏幕。我已在https://msdn.microsoft.com/en-us/library/windows/apps/Windows.UI.Xaml.Controls.MediaPlayerElement.aspx中添加了引用的代码 但仍然无法正常工作,当我在计算机上调试应用程序时,应用程序在调用DisplayRequest后崩溃,例外是:

不支持从MTA激活单线程类

我已按原样复制并粘贴了示例,定位了x64和ARM上的周年纪念更新。

任何帮助都会非常感激

干杯, ħ

以下代码:

//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;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您需要在UI Thread上调用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
        //
    });
}