【问题标题】:iOS AVPlayer: pause video when screen locksiOS AVPlayer:屏幕锁定时暂停视频
【发布时间】:2015-07-06 11:06:31
【问题描述】:

我正在使用 AVPlayerViewController 来显示带有 AVPlayer 的视频。

当我锁定手机屏幕时,视频会继续在后台播放。

如何防止这种情况发生?

【问题讨论】:

    标签: ios iphone ipad 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;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多