开发者

remoteControlReceivedWithEvent in AVAudio is not being called

I am working on an app playing the audio in background. I have written the code for the iPod controls as directed by app docs. I have implemented this like

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {    
    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self playButtonPressed:playButton];
                [self testing];
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                [self rewButtonReleased:(UIButton *)rewButton];
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                [self ffwButtonReleased:(UIButton *)ffwButton];
                break;

            default:
                break;
        }
    }
}
- (BOOL)canBecomeFirstResponder {
 开发者_运维百科   NSLog(@"canBecomeFirstResponder");
    return YES;
}

and

- (void) viewDidAppear:(BOOL)animated 
{
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    }
}

- (void) viewWillDisappear:(BOOL)animated 
{
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

Please suggest me what else do I need or where am I committing any mistakes. Regards!


Just make sure it can become first responder

- (BOOL)canBecomeFirstResponder {
     return YES;
}


I spent hours trying to solve this same issue until I found this similar question: How can you play music from the iPod app while still receiving remote control events in your app?

My problem was that I was enabling the mixing override in an attempt to get background audio working:

UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers,
                     sizeof (doSetProperty), &doSetProperty);

I removed those lines (turns out they aren't necessary for background audio), and remoteControlReceivedWithEvent started working. It kind of makes sense. If you specify that your app should be able to mix its audio with whatever is currently playing in some other app (e.g. mixing your app's sound effects with the iPod app's music), it follows that the other app should retain priority of your remote button presses.


Did you add the UIBackgroundModes audio key to the info.plist? See the docs.


In addition to add the UIBackgroundModes audio key to the info.plist of your app, I added:

-(void)viewDidLoad
{
    UIDevice* device = [UIDevice currentDevice];

    BOOL backgroundSupported = NO;

    if ([device respondsToSelector:@selector(isMultitaskingSupported)])
    {
        backgroundSupported = device.multitaskingSupported;
    }

... your code ...


In Swift 3 just be sure that:

override var canBecomeFirstResponder: Bool {

    get {

        return true
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜