You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MPRemoteCommandCenter锁屏远程控制不显示及音频中断功能失效问题

问题修复步骤

  • 确认后台模式配置正确:在项目的Signing & Capabilities中添加Background Modes能力,勾选Audio, AirPlay, and Picture in Picture,需和系统权限设置同时生效。

  1. 修正AVAudioSession配置错误
    你两次设置AVAudioSession的Category,第一次设置的.mixWithOthers选项会让系统优先展示其他音频App的锁屏控制,直接删除重复设置和错误选项:
// 替换buttonPressed中的音频会话配置代码
do {
    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.allowAirPlay])
    print("Playback OK")
    try AVAudioSession.sharedInstance().setActive(true)
    print("Session is Active")
} catch {
    print(error)
}
  1. 修正Remote Command处理逻辑错误
    你当前play和pause命令的判断条件写反了,修改对应判断逻辑:
func setupRemoteTransportControls() {
    let commandCenter = MPRemoteCommandCenter.shared()
    commandCenter.playCommand.addTarget { [unowned self] event in
        if self.player.rate == 0 { // 暂停状态才执行播放逻辑
            self.player.play()
            self.updateNowPlaying(isPause: false)
            return .success
        }
        return .commandFailed
    }
    commandCenter.pauseCommand.addTarget { [unowned self] event in
        if self.player.rate == 1.0 { // 播放状态才执行暂停逻辑
            self.player.pause()
            self.updateNowPlaying(isPause: true)
            return .success
        }
        return .commandFailed
    }
}
  1. 调整NowPlayingInfo设置时机
    不要在viewDidLoad阶段调用setupNowPlaying(),改成在AVPlayer调用play()之后再调用,确保播放状态激活后再向系统上报元数据。
  2. 补全NowPlayingInfo必填字段,恢复注释代码
    你注释掉了播放时间和速率的设置,这两个字段是系统判断播放状态的核心参数,必须设置:
func setupNowPlaying(title: String, artist: String, cover: UIImage) {
    var nowPlayingInfo = [String : Any]()
    nowPlayingInfo[MPMediaItemPropertyTitle] = title
    nowPlayingInfo[MPMediaItemPropertyArtist] = artist
    nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: cover.size) { size in
        return cover
    }
    nowPlayingInfo[MPNowPlayingInfoPropertyIsLiveStream] = true
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1.0
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime().seconds
    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}

func updateNowPlaying(isPause: Bool) {
    guard var nowPlayingInfo = MPNowPlayingInfoCenter.default().nowPlayingInfo else { return }
    nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime().seconds
    nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = isPause ? 0 : 1
    MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
  1. 注册音频中断通知
    你只实现了handleInterruption方法,没有添加通知监听,在viewDidLoad里添加注册代码:
NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption(notification:)), name: AVAudioSession.interruptionNotification, object: nil)
  1. 确保AVPlayer被全局持有
    检查你的player变量是否声明为ViewController的成员属性,不要在buttonPressed里声明为局部变量,否则会被ARC自动释放,导致播放停止、控制失效。

内容的提问来源于stack exchange,提问作者LewTrocki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 09:45:00