Can't beginReceivingRemoteControlEvents in iOS

前端 未结 4 1480
孤城傲影
孤城傲影 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 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;
        }
    }
    

提交回复
热议问题