2

I am using AVPlayer to play a video in my app,now i want to let the video play in background mode.

this is what i put in the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:

[[AVAudioSession sharedInstance] setDelegate: self];    
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

and i also add to the plist: Required background modes -> App plays audio

I add this too:

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

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            NSLog(@"4");
            break;
        case UIEventSubtypeRemoteControlPlay:
            break;
        case UIEventSubtypeRemoteControlPause:
            NSLog(@"3");
            break;
        case UIEventSubtypeRemoteControlNextTrack:
            NSLog(@"2");
            break;
        case UIEventSubtypeRemoteControlPreviousTrack:
            NSLog(@"1");
            break;
        default:
            break;
    }
}

And when i move the app to the background and press the buttons the nslog print to the console

Did i need to add something else?

YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • Follow this link: http://stackoverflow.com/questions/4771105/how-do-i-get-my-avplayer-to-play-while-app-is-in-background – iMash May 07 '12 at 08:09

1 Answers1

1

Just add [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; and some other tweaks. It's all here.

Community
  • 1
  • 1
Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
  • 1
    Wait. This worked? Video works in the background? From all the noise on the internet I thought background video was impossible on iOS! @MTA, Please confirm if possible. – Dev Kanchen Jan 06 '13 at 20:05
  • @dev I can confirm that it works. You need to lock your device and not press the home button. You can also support home button registering for the `UIApplicationDidEnterBackgroundNotification` and with a timer restart the video, you get a little lag (faded) but it is better than nothing. – MacTeo Mar 20 '13 at 11:43
  • I've answered to [this](http://stackoverflow.com/a/15523272/616964) question with complete details on how to achieve the desired result. – MacTeo Mar 22 '13 at 06:57
  • link to explanation is broken – Shaybc Apr 14 '16 at 20:27