即使在音频文件暂停后,MPRemoteCommandCenter控制中心暂停按钮也不会更新

Dan*_*uri 2 objective-c ios

我正在研究处理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;
} ];
Run Code Online (Sandbox Code Playgroud)

}

//在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];
Run Code Online (Sandbox Code Playgroud)

}

如果需要任何其他信息,请与我们联系.

小智 8

对于iOs 9.2:

我在MPNowPlayingInfoCenter和MPRemoteCommandCenter中尝试了几乎所有设置组合.

停止更新搜索栏,只需设置即可

MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0.0
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player?.currentTime
Run Code Online (Sandbox Code Playgroud)

但是要切换暂停按钮,需要在暂停audioPlayback后停用AVAudioSession.

AVAudioSession.sharedInstance().setActive(false)
Run Code Online (Sandbox Code Playgroud)

最后我的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
}
Run Code Online (Sandbox Code Playgroud)

希望,这有助于某人.