2016-08-12 128 views
3

即使這個問題IST問了很多次,我沒有找到任何令人滿意的回答了這個互動,我還是想給它的另一個嘗試:MPMusicPlayerController不會與遙控器

我試圖發揮MPMediaItemsMPMusicPlayerController。到現在爲止還挺好。這是從Itunes Match或Apple Music播放文件所必需的。任何其他文件都通過AVPlayer播放。

處理爲與AVPlayer播放的歌曲的遙控器似乎使用是沒有問題的:

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

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent 

此外,也可以來檢索背景AVPlayerItemDidPlayToEndTimeNotification這個反應開始播放下一首歌。

嘗試使用MPMusicPlayerController播放歌曲時,所有這些看起來都不像是應該的。我試圖播放一首歌曲是這樣的:

self.audioPlayer = [MPMusicPlayerController applicationMusicPlayer]; 
self.audioPlayer.repeatMode = MPMusicRepeatModeNone; 
[self.audioPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:@[_playerItem]]]; 

使用給定的觀察員:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playbackStateDidChange:) name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:nil]; 
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playbackItemDidChange:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:nil]; 
[self.audioPlayer beginGeneratingPlaybackNotifications]; 

如果應用程序是在前臺的觀察家們稱爲他們應該。進入後臺觀察員不再被調用。在這種情況下,我不可能檢測歌曲的結尾以跳到下一首歌曲。

此外 - 遠程控制不會工作。我用:

[MPRemoteCommandCenter sharedCommandCenter].playCommand.enabled = YES; 
[[MPRemoteCommandCenter sharedCommandCenter].playCommand addTarget:self action:@selector(remotePlay)]; 

[MPRemoteCommandCenter sharedCommandCenter].pauseCommand.enabled = YES; 
[[MPRemoteCommandCenter sharedCommandCenter].pauseCommand addTarget:self action:@selector(remoteStop)]; 

[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand.enabled = YES; 
[[MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand addTarget:self action:@selector(loadPreviousSong)]; 

[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand.enabled = YES; 
[[MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand addTarget:self action:@selector(loadNextSong)]; 

但是,在前景和背景下這些給定的選擇器被稱爲。

有沒有人有解決我的問題?正如我所說,我需要使用MPMusicPlayerController,因爲我想播放Apple Music和iTunes Match曲目。

相關問題:

+0

這太可怕了。我認爲解決這個問題的唯一方法就是告訴蘋果。你提交了一個錯誤報告嗎? – evenodd

回答

0

因爲音樂應用程序正在處理該項目的實際播放,您自己的應用程序不會收到通知。您最好的選擇是通過bugreport.apple.com提交增強請求。

Apple Music/iTunes團隊正在傾聽關於如何改進Apple音樂API的反饋意見,我可以在WWDC上看到。

+1

似乎沒有辦法解決它。我們最好的選擇是等到他們爲這個令人困惑的API訂購。感謝你的回答 – driedel