Search code examples
objective-ciosbackgroundavaudioplayerremote-control

Can't beginReceivingRemoteControlEvents in iOS


In my app i want let user to control audio playback in background. I set backGround modes in .plist, and in plays in bg just like i wanted.But i can't get any response from touching the control buttons.

I set the AudioSession like this

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

Then, in viewContoller where my player is placed i beginReceivingRemoteControlEvents like this

 if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    [self becomeFirstResponder];
    NSLog(@"Responds!");
}

And it prints Responds!

But the problem is that this method is never called

    - (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    NSLog(@"Where is my event?");
    if(event.type == UIEventTypeRemoteControl)
    {
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                NSLog(@"Pause");
                [self playWords:playButton];
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                NSLog(@"Next");
                [self next];
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                NSLog(@"Prev");
                [self prev];
                break;

        }
    }

I even tried to write a category on UIApplication to let it become the first responder, but it doesn't help

@implementation UIApplication (RemoteEvents)
-(BOOL)canBecomeFirstResponder
{
    return YES;
}
@end

Why can this happen?

SOLUTION Here's what solved my problem Entering background on iOS4 to play audio


Solution

  • I have done the same work in my project, it's working fine. Please follow this, perhaps it will help you. Change the event name etc. In my code _audio is the object of AVAudioPlayer.

    - (void)viewDidLoad {
        NSError *setCategoryErr = nil;
        NSError *activationErr  = nil;
        [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
        [[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
    }
    
    - (void)viewWillAppear {
    
          [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    }
    
    
    - (void)remoteControlReceivedWithEvent:(UIEvent *)event {
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlPlay:
                [_audio play];
                break;
            case UIEventSubtypeRemoteControlPause:
                [_audio pause];
                break;
            default:
                break;
        }
    }