Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer: Background audio stops when home button is pressed, but plays with locked screen

Ok, so my app plays videos, I want the sound to continue playing in the background. But when I hit the "Home" button the playback stops. I'm testing on iPhone 4s with iOS 5.0.1

What's working?

  • If the video is playing and I lock the screen, it keeps playing,
  • I Can control the playback from the lock screen controls: next, previous, play, pause.
  • If I press "home", then go to the "remote controls" from the multitask bar, it works.

What's wrong?

  • I want the app to keep playing when I press "home" button, because when I do, audio stops.

What have I done:

  • Added "audio" to "Required background modes" in my app.plist

Added the following code to app delegate's application:didFinishLaunchingWithOptions:

NSError *setCategoryErr = nil;
NSError *activationErr  = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];    
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

In my view controller I've implemented the methods to receive remote controls:

- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    //End recieving events
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self play:nil];
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                [self actionPlaybackPreviousItem];
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                [self actionPlaybackNextItem];
                break;

            default:
                break;
        }
    }
}

How come everything works but, this?

like image 558
Emanuel Avatar asked Jul 05 '12 06:07

Emanuel


1 Answers

Stack Overflow is becoming less and less responsive each time... Hopefully it's not so bad for everybody out there. I've found nothing to respond why is it that the video is paused after hitting the Home button, however, I've come with a workaround if it's of help to anyone:

Since the player is playing until the app goes to the background, i've created a timer, to check if the app was playing while it was in the foreground, if so, resume playback:

- (void)applicationWillResignActive:(UIApplication *)application
{
    MainViewController* mainController=self.viewController;
    playerWasPlaying=mainController.player.rate!=0;
}

-(void)resumePlayback {
    MainViewController* mainController=self.viewController;
    if (mainController.player.rate==0&&playerWasPlaying)
        [mainController play:nil];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(resumePlayback) userInfo:nil repeats:NO];
}

Not the best option in my opinion, but for now it's working, with a little jump in the playback while it resumes.

like image 97
Emanuel Avatar answered Sep 28 '22 02:09

Emanuel