Mario Gonzalez Munzon
Mario Gonzalez Munzon

Reputation: 157

iOS AVPlayer: pause video when screen locks

I am using an AVPlayerViewController to display a video with an AVPlayer.

When I lock the phone screen the video keeps playing on the background.

How can I prevent this?

Upvotes: 2

Views: 1396

Answers (2)

Amit Shelgaonkar
Amit Shelgaonkar

Reputation: 487

Try this disable background mode for Audio and Airplay

- (void)applicationDidEnterBackground:(UIApplication *)application {
     // pause the video here
 }

- (void)applicationWillEnterForeground:(UIApplication *)application      {
    // resume video when screen unlock 
 }

Upvotes: 1

Omer Waqas Khan
Omer Waqas Khan

Reputation: 2413

Go through this blog post, it will help you out

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

Upvotes: 0

Related Questions