问题描述
我正在开发一个处理MPRemoteCommandCenter类的音频录制应用程序.我已经设置了命令中心来播放和暂停命令.下面添加了代码段.我遇到了pauseCommand的问题.
I’m working on an audio recording application which deals with MPRemoteCommandCenter class. I’ve setup the command center to play and pause commands. Code snippet added below. I’m facing an issue with pauseCommand.
我从应用程序开始播放音频文件,该文件还会更新控制中心以及正在播放的文件,并且播放按钮已更改为暂停按钮.现在,当文件正在播放时,我选择控制按钮来暂停控制中心的音频.这是在应用程序中调用pauseCommand处理程序,音频文件已暂停,但控制中心不断更新搜索栏,并且暂停按钮未更改为播放按钮.
From the application I start playing the audio file which updates the control center as well with the file being played and play button changed to pause button. Now when the file is being played, I pause the audio from control center choosing the pause button.This is invoking the pauseCommand handler in the application and the audio file is paused but the control centre continuous to update the seek bar and the pause button doesn’t change to play button.
-(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方法
// This method is invoked in both pauseAudioPlayback & resumeAudioPlayback methods
- (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];
}
请让我知道是否需要其他信息.
Please let me know if any additional information is required.
推荐答案
对于iO 9.2 :
我已经尝试了MPNowPlayingInfoCenter和MPRemoteCommandCenter中几乎所有设置的组合.
I've tried nearly all combinations of settings in MPNowPlayingInfoCenter and MPRemoteCommandCenter.
要停止更新,搜索栏就足以设置
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0.0
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player?.currentTime
但是要切换暂停按钮,需要在暂停audioPlayback后停用AVAudioSession.
But to toggle pause button it is needed to deactivate AVAudioSession after pausing audioPlayback.
AVAudioSession.sharedInstance().setActive(false)
最后,我的pauseCommand处理程序看起来像:
Finally my pauseCommand handler looks like:
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
}
希望,这对某人有帮助.
Hope, this helps somebody.
这篇关于MPRemoteCommandCenter控制中心的暂停按钮即使在音频文件暂停后也不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!