2

我已经设置了这样的功能

 func setupRemoteCommandCenter(enable: Bool) {
    do{
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeDefault, options: AVAudioSessionCategoryOptions.allowBluetooth)
    }
    catch {
        print("Setting category failed.")
    }
    let remoteCommandCenter = MPRemoteCommandCenter.shared()

    remoteCommandCenter.pauseCommand.isEnabled = enable
    remoteCommandCenter.playCommand.isEnabled = enable
    remoteCommandCenter.stopCommand.isEnabled = enable
    remoteCommandCenter.togglePlayPauseCommand.isEnabled = enable

    if enable {
        remoteCommandCenter.pauseCommand.addTarget(self, action: #selector(airPodsTap))
        remoteCommandCenter.playCommand.addTarget(self, action: #selector(airPodsTap))
        remoteCommandCenter.stopCommand.addTarget(self, action: #selector(airPodsTap))
        remoteCommandCenter.togglePlayPauseCommand.addTarget(self, action: #selector(airPodsTap))


    } else {

        remoteCommandCenter.pauseCommand.removeTarget(self, action: #selector(airPodsTap))
        remoteCommandCenter.playCommand.removeTarget(self, action: #selector(airPodsTap))
        remoteCommandCenter.stopCommand.removeTarget(self, action: #selector(airPodsTap))
        remoteCommandCenter.togglePlayPauseCommand.removeTarget(self, action: #selector(airPodsTap))

    }


}

遵循苹果文档,但我无法始终如一地接收来自外部耳机(有线/或蓝牙)的事件。

我已经在我的项目设置中启用了音频背景模式。

有任何想法吗?

4

0 回答 0