我正在开发一个处理MPRemoteCommandCenter类的录音应用程序。我已经设置了命令中心来播放和暂停命令。下面添加了代码段。我面临pauseCommand的问题。

从应用程序中,我开始播放音频文件,该文件会更新控制中心以及正在播放的文件,并且播放按钮已更改为暂停按钮。现在正在播放文件时,我选择控制按钮来暂停控制中心的音频。
这是在应用程序中调用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];

}

如果需要任何其他信息,请告诉我。

最佳答案

对于iO 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
}

希望这对某人有帮助。

10-07 19:49
查看更多