我正在使用 AVplayer 从 url 流式传输音频。音频正在播放,我设置了远程命令事件。我只使用播放/暂停按钮,它们在锁定屏幕上工作。问题是每当我连接有线耳机时,耳机上的播放/暂停按钮都不起作用。他们为无线耳机工作。任何正确方向的帮助将不胜感激......
音频会话
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
UIApplication.shared.beginReceivingRemoteControlEvents()
try audioSession.setActive(true)
} catch {
print(error.localizedDescription)
}
指挥中心
func setupRemoteTransportControls() {
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.isEnabled = true
commandCenter.pauseCommand.isEnabled = true
commandCenter.playCommand.addTarget(handler: { (event) in // Begin playing the current track
if self.audioPlayer.rate == 0.0 {
self.audioPlayer.play()
//return .success
}
return MPRemoteCommandHandlerStatus.success
})
commandCenter.pauseCommand.addTarget(handler: { (event) in // Pause current track
if self.audioPlayer.rate == 1.0 {
self.audioPlayer.pause()
}
return MPRemoteCommandHandlerStatus.success
})
}