iOS AVPlayer:屏幕锁定时暂停视频
Posted
技术标签:
【中文标题】iOS AVPlayer:屏幕锁定时暂停视频【英文标题】:iOS AVPlayer: pause video when screen locks 【发布时间】:2015-07-06 11:06:31 【问题描述】:我正在使用 AVPlayerViewController 来显示带有 AVPlayer 的视频。
当我锁定手机屏幕时,视频会继续在后台播放。
如何防止这种情况发生?
【问题讨论】:
【参考方案1】:试试这个禁用音频和 Airplay 的背景模式
- (void)applicationDidEnterBackground:(UIApplication *)application
// pause the video here
- (void)applicationWillEnterForeground:(UIApplication *)application
// resume video when screen unlock
【讨论】:
【参考方案2】:阅读这篇博文,它会对你有所帮助
How to Detect When an App Is Coming From the Lockscreen or Homescreen on ios
- (void)applicationDidEnterBackground:(UIApplication *)application
CGFloat screenBrightness = [[UIScreen mainScreen] brightness];
NSLog(@"Screen brightness: %f", screenBrightness);
self.backgroundedToLockScreen = screenBrightness <= 0.0;
- (void)applicationWillEnterForeground:(UIApplication *)application
if (self.backgroundedToLockScreen)
... // App was backgrounded to lock screen
else
... // App was backgrounded on purpose by tapping the home button or switching Apps.
self.backgroundedToLockScreen = NO;
【讨论】:
以上是关于iOS AVPlayer:屏幕锁定时暂停视频的主要内容,如果未能解决你的问题,请参考以下文章
Xcode swift - 背景模式 - 锁定时AVPlayer中的音频