我已经设置了这样的功能
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))
}
}
遵循Apple文档,但我无法始终从外部耳机(有线/蓝牙)接收事件。
我已在项目设置中启用了音频后台模式。
有什么想法吗?