I use this code to play audio when the device is locked.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

The volume slider works fine, but the play / pause button and the rewind buttons do not work. Why?

    1 answer 1

    Most likely because the method - (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent not processed - (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent

    Example

    AppDelegate.m

     - (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent { if (theEvent.type == UIEventTypeRemoteControl) { switch(theEvent.subtype) { case UIEventSubtypeRemoteControlPlay: [[NSNotificationCenter defaultCenter] postNotificationName:@"PlayNotification" object:theEvent]; break; case UIEventSubtypeRemoteControlPause: [[NSNotificationCenter defaultCenter] postNotificationName:@"PauseNotification" object:theEvent]; break; case UIEventSubtypeRemoteControlTogglePlayPause: [[NSNotificationCenter defaultCenter] postNotificationName:@"TogglePlayPauseNotification" object:theEvent]; break; default: return; } } } 

    RadioViewController.m

     @property (nonatomic, strong) AVPlayer *player; @property (nonatomic, getter=isPlaying) BOOL playing; - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewDidLoad { ... NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; [notificationCenter addObserver:self selector:@selector(play) name:@"PlayNotification" object:nil]; [notificationCenter addObserver:self selector:@selector(pause) name:@"PauseNotification" object:nil]; [notificationCenter addObserver:self selector:@selector(togglePlayPause) name:@"TogglePlayPauseNotification" object:nil]; } - (void)play { [self.player play]; self.playing = YES; } - (void)pause { [self.player pause]; self.playing = NO; } - (void)togglePlayPause { (self.isPlaying) ? [self pause] : [self play]; }