【发布时间】:2015-07-06 11:06:31
【问题描述】:
我正在使用 AVPlayerViewController 来显示带有 AVPlayer 的视频。
当我锁定手机屏幕时,视频会继续在后台播放。
如何防止这种情况发生?
【问题讨论】:
我正在使用 AVPlayerViewController 来显示带有 AVPlayer 的视频。
当我锁定手机屏幕时,视频会继续在后台播放。
如何防止这种情况发生?
【问题讨论】:
试试这个禁用音频和 Airplay 的背景模式
- (void)applicationDidEnterBackground:(UIApplication *)application {
// pause the video here
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// resume video when screen unlock
}
【讨论】:
阅读这篇博文,它会对你有所帮助
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;
}
【讨论】: