remoteControlReceivedWithEvent从未调用过

时间:2012-11-17 04:07:03

标签: iphone ios xcode audio-streaming avplayer

我正在尝试允许远程或耳塞播放并在应用处于后台或屏幕锁定时暂停音频。所以在viewDidLoad中我有:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

然后我有:

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {



if (receivedEvent.type == UIEventTypeRemoteControl) {



switch (receivedEvent.subtype) {



    case UIEventSubtypeRemoteControlTogglePlayPause:

        [self playOrStop];

        break;



    case UIEventSubtypeRemoteControlPreviousTrack:


        break;



    case UIEventSubtypeRemoteControlNextTrack:


        break;



    default:

        break;

}

}


}

但是,它永远不会被召唤。在后台运行已打开,AVSession在AppDelegate中设置。我很茫然。

2 个答案:

答案 0 :(得分:2)

您可能还需要添加:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

UIResponder的任何子类都需要实现此功能,否则它将不接受第一响应者状态。

答案 1 :(得分:1)

尝试放

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
viewWillAppear中的

:( BOOL)动画而不是viewDidLoad。