2015-06-26 73 views
1

我正在處理MPRemoteCommandCenter類的音頻錄製應用程序。我已經設置了命令中心來播放和暫停命令。下面添加的代碼片段。我正面臨pauseCommand的問題。即使音頻文件暫停,MPRemoteCommandCenter控制中心暫停按鈕也不會更新

從應用程序我開始播放更新控制中心的音頻文件以及正在播放的文件和播放按鈕更改爲暫停按鈕。現在當文件正在播放時,我選擇暫停按鈕暫停控制中心的音頻。 這是調用應用程序中的pauseCommand處理程序,並且音頻文件已暫停,但控制中心不斷更新搜索欄並且暫停按鈕不會更改爲播放按鈕。

-(void) setupRemoteControls 
{ 
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; 

[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) { 

    if ([self pauseAudioPlayback]) 
     return MPRemoteCommandHandlerStatusSuccess; 
    else 
     return MPRemoteCommandHandlerStatusCommandFailed; 
} ]; 

[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) { 

    if ([self resumeAudioPlayback]) 
     return MPRemoteCommandHandlerStatusSuccess; 
    else 
     return MPRemoteCommandHandlerStatusCommandFailed; 
} ]; 

}

//此方法既pauseAudioPlayback & resumeAudioPlayback方法

- (void) updateRemoteControls 
{ 
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; 

if (self.audioPlayer) 
{ 
    if (self.isPlaying) 
     commandCenter.playCommand.enabled = NO; 
    else if (self.isPlayPaused) 
     commandCenter.playCommand.enabled = YES; 
} 
else 
    [self clearNowPlayingInfo]; 

}

請讓我知道是否需要任何額外的信息被調用。

+0

今天,我已經將我的iOS升級到8.4,現在當我運行相同的代碼時,現在在控制中心點擊暫停按鈕,應用程序正在接收處理程序以及控制中心中的搜索欄暫停但控制中心的暫停圖標不會變爲播放圖標。但有一個問題已經解決(尋求酒吧暫停)。這是否證實問題出現在操作系統端而不是我的代碼上,還是代碼本身至少用於更新控制中心圖標的問題。 – Dantuluri

+0

你解決了這個問題嗎?我們應該像@pgmarchenko說的那樣停用AVAudioSession?我正在尋找解決方案,但沒有使AVAudioSession.setActive(false) –

回答

0

在你updateRemoteControls方法,嘗試改變以下內容,看看是否有幫助?

- (void) updateRemoteControls 
{ 
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter]; 

if (self.audioPlayer) 
{ 
    if (self.isPlaying) 
     [commandCenter.playCommand setEnabled:NO]; 
    else if (self.isPlayPaused) 
     [commandCenter.playCommand setEnable:YES]; 
} 
else 
    [self clearNowPlayingInfo]; 
} 

而且,你不應該被更新pauseCommand?如果這樣使用與更改部分相同的邏輯。我目前有類似的問題,所以讓我知道你是否知道這一點。只是想通過修復一些可能或可能不會出錯的語法來幫助我:)

1

這個問題令人費解,因爲答案最終只能用於不同的API,MPNowPlayingInfoCenter。什麼工作對我來說是更新MPNowPlayingInfo的MPNowPlayingInfoPropertyPlaybackRate在我的版本上面的updateRemoteControls方法:

- (void) updateRemoteControls { 

    if (self.audioPlayer) { 
     MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter]; 

     // Get currently playing information from other method: 
     NSMutableDictionary *displayInfo = [NSMutableDictionary dictionaryWithDictionary:[self currentDisplayInfo]]; 

     // A playback rate of 0.0f corresponds to a "not playing" state. 
     float playbackRate = self.isPlaying ? 1.0f : 0.0f; 
     [displayInfo setObject:[NSNumber numberWithFloat:playbackRate] forKey:MPNowPlayingInfoPropertyPlaybackRate]; 

     // This will update the lock screen button and hide the progress bar if the playback rate is 0.0f: 
     infoCenter.nowPlayingInfo = displayInfo; 

    } else { 

     [self clearNowPlayingInfo]; 

    } 

} 
1

bruce1337的答案似乎是正確的

在斯威夫特以下(終於)打開播放鍵暫停&反之亦然:

let nowPlaying = [MPMediaItemPropertyAlbumTitle: "Led Zep IV", 
        MPMediaItemPropertyTitle: "Black Dog", 
        MPNowPlayingInfoPropertyPlaybackRate: NSNumber(float: 1.0)] 
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nowPlaying 
5

對於iOS 9.2

我已經嘗試了幾乎所有MPNowPlayingInfoCenter和MPRemoteCommandCenter中的設置組合。

停止更新搜索欄這足以設置

MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0.0 
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player?.currentTime 

但要切換暫停按鈕它是需要暫停audioPlayback後停用AVAudioSession。

AVAudioSession.sharedInstance().setActive(false) 

最後我pauseCommand處理程序是這樣的:

MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.addTargetWithHandler { (e) -> MPRemoteCommandHandlerStatus in 
     player?.pause() 
     let infoCenter = MPNowPlayingInfoCenter.defaultCenter() 
     infoCenter.nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0.0 
     infoCenter.nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player?.currentTime 

     _ = try? AVAudioSession.sharedInstance().setActive(false) 

     return MPRemoteCommandHandlerStatus.Success 
} 

希望,這會有所幫助別人。

+0

我的用於處理MPRemoteCommandCenter.nextTrackCommand的代碼神祕地導致所有鎖屏控件被禁用。在播放下一首曲目之前,我停用了AVAudioSession,並保持啓用狀態。 –

相關問題