在全屏播放视频时,设备屏幕会自动保持常亮。但是在非全屏状态下,屏幕就会自动关闭。所以我们需要让屏幕在播放状态时,保持常亮。
微软的官方文档里面是有详细的介绍。微软官方文档链接
但是完全按照这个代码写会报错。也是真真的醉了~?

解决方法:(按照如下代码就没问题了)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| private async void PlaybackSession_PlaybackStateChanged(MediaPlaybackSession sender, object args) { if (sender is MediaPlaybackSession playbackSession && playbackSession.NaturalVideoHeight != 0) { if (playbackSession.PlaybackState == MediaPlaybackState.Playing) { // 跨线程操作UI await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { if (appDisplayRequest == null) { // This call creates an instance of the DisplayRequest object appDisplayRequest = new DisplayRequest(); appDisplayRequest.RequestActive(); } }); } else // PlaybackState is Buffering, None, Opening, or Paused. { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { if (appDisplayRequest != null) { // Deactivate the display request and set the var to null. appDisplayRequest.RequestRelease(); appDisplayRequest = null; } }); } } }
|
话说,16年微软官方博客写这个的时候下面就有人评论这个问题。官方文档对我这样的小白也太不友好了。?
链接:How to prevent screen locks in your UWP apps