2014-10-26 203 views
2

我正在將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; 
} 

一對夫婦的注意事項:

  1. 我不使用AVPlayer。我使用CoreAudio/Audio單元播放 音頻。這部分的應用程序工作正常。
  2. 我只在iOS 7和iOS 8上測試。
  3. 我使用的是驚人的音頻引擎作爲CoreAudio的包裝:http://theamazingaudioengine.com

回答

4

發現了它。當初始化令人驚歎的音頻引擎(順便說一個好的框架)時,如果想要使用MPNowPlayingInfoCenter,需要告訴框架它不想與其他應用程序共享音頻輸出。爲此我做了:

self.audioController = [[AEAudioController alloc] 
      initWithAudioDescription:[AEAudioController nonInterleaved16BitStereoAudioDescription] 
         inputEnabled:NO]; 
self.audioController.allowMixingWithOtherApps = NO;