MPNowPlayingInfoCenter和MPRemoteCommandCenter在模拟器中工作,但不在设备上
问题描述:
我正在将MPNowPlayingInfoCenter整合到音乐播放应用程序中。当我在iOS模拟器中运行它时,它和MPRemoteCommandCenter的整合非常有效。当我在设备上运行相同的代码时,Control Center中的音乐控件根本不会改变。就好像该应用程序不是注册MPNow和MPRemote中心的事件。MPNowPlayingInfoCenter和MPRemoteCommandCenter在模拟器中工作,但不在设备上
MPNowPlayingInfoCenter *np = [MPNowPlayingInfoCenter defaultCenter];
np.nowPlayingInfo = @{MPMediaItemPropertyTitle:[tracks objectAtIndex:self.currentTrack],
MPMediaItemPropertyArtist:currentArtist,
MPMediaItemPropertyAlbumTitle:currentAlbum,
MPMediaItemPropertyMediaType:@(MPMediaTypeMusic),
MPMediaItemPropertyPlaybackDuration:@(self.audioHandler.audioDuration),
MPNowPlayingInfoPropertyElapsedPlaybackTime:@(self.audioHandler.position),
MPNowPlayingInfoPropertyPlaybackRate:rate
};
我已经得到了所有这些好东西在我的视图控制器生命周期方法:
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
// Turn off remote control event delivery
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
// Resign as first responder
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
一对夫妇的注意事项:
- 我不使用AVPlayer。我使用CoreAudio/Audio单元播放 音频。这部分的应用程序工作正常。
- 我只在iOS 7和iOS 8上测试。
- 我使用的是惊人的音频引擎作为CoreAudio的包装:http://theamazingaudioengine.com
答
发现了它。当初始化令人惊叹的音频引擎(顺便说一个好的框架)时,如果想要使用MPNowPlayingInfoCenter,需要告诉框架它不想与其他应用程序共享音频输出。为此我做了:
self.audioController = [[AEAudioController alloc]
initWithAudioDescription:[AEAudioController nonInterleaved16BitStereoAudioDescription]
inputEnabled:NO];
self.audioController.allowMixingWithOtherApps = NO;