我的应用程序支持来自外部音乐应用程序(如Pandora)的音频或在应用程序内播放,使用AVPlayer从iPod库播放AVPlayerItem .

在我的AppDelegate中,我调用: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];


UInt32 otherAudioIsPlaying;
UInt32 propertySize = sizeof (otherAudioIsPlaying);

AudioSessionGetProperty (
                         kAudioSessionProperty_OtherAudioIsPlaying,
                         &propertySize,
                         &otherAudioIsPlaying
                         );

if (otherAudioIsPlaying) {
    NSLog(@"Allow Mixing for background music (like Pandora,...)");
    UInt32 allowMixing = true;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (allowMixing), &allowMixing);
}

[[AVAudioSession sharedInstance] setActive: YES error: NULL];

...

请注意,我也注册了中断 .

因此,如果应用程序在Pandora播放时启动,Pandora将继续播放 .

之后,如果USer决定从iPod库播放音乐,我会调用:[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:0 error:nil];并确保,我收到遥控事件,我打电话:[self becomeFirstResponder]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

直到这里,一切正常:应用程序现在播放iPod库中的音乐(并且在将类别设置为播放时停止Pandora) .

单击耳机上的中央按钮时出现问题 . 在上述顺序中,单击中心按钮时,将调用interruptionListenerCallback!我期待调用remoteControlReceivedWithEvent回调 .

所以,我的问题是:你怎么确定外部音乐播放器不再处理遥控事件,我的应用程序得到它们?

在Pandora以前从未玩过的不同用例中,一切正常:我的应用程序按预期接收远程控制事件;所以我知道这部分代码可行 .

谢谢