gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:07 27 4
gpt4 key购买 nike

我正在开发一个处理 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];

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

最佳答案

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

希望,这对某人有帮助。

关于ios - MPRemoteCommandCenter 控制中心暂停按钮即使在音频文件暂停后也不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31069705/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com