Can't beginReceivingRemoteControlEvents in iOS

前端 未结 4 1491
孤城傲影
孤城傲影 2021-02-14 17:04

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 tou

相关标签:
4条回答
  • 2021-02-14 17:53

    There is a newer mechanism for listening to remote control events. For example, to execute a block when the headphone play/pause button is pressed:

        MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
    
        [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
            NSLog(@"toggle button pressed");
            return MPRemoteCommandHandlerStatusSuccess;
        }];
    

    or, if you prefer to use a method instead of a block:

        [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];
    

    To stop:

        [commandCenter.togglePlayPauseCommand removeTarget:self];
    

    or:

        [commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];
    

    You'll need to add this to the includes area of your file:

    @import MediaPlayer;
    

    For it to work in the background, you must have the background audio mode added to your app's capabilities.

    0 讨论(0)
  • 2021-02-14 17:55

    You have

    respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
    

    but in the method signature you have

    - (void)remoteControlReceivedWithEvent:(UIEvent *)event
    

    The place where you are registering the signature is wrong. Just replace it by

    respondsToSelector:@selector(beginReceivingRemoteControlEvents:)]){
    

    You are missing the : which is making the app send the even to a non existing method I am assuming. I am surprised that your app is not crashing.

    0 讨论(0)
  • 2021-02-14 18:02

    Review this sample code from Apple for an example of usage. Likely your primary view controller is not actually becoming the first responder. An alternative usage is to place this code in your Application Delegate (which will always be the first responder) and respond to these events before they have had a chance to propagate, and potentially be consumed by other responders.

    0 讨论(0)
  • 2021-02-14 18:03

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题