3

I have an iOS application that uses an AVAudioSession of category playAndRecord and mode default to record and play audio from and to a Bluetooth headset.

Now I want to for start and stop the recording using the buttons on the headset via MPRemoteCommandCenter handlers.
However, the button presses on the headset especially for the pauseCommand are not detected.

UIApplication.shared().beginReceivingRemoteControlEvents()

let audioSession = AVAudioSession.sharedInstance()
audioSession.setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth])
audioSession.setActive(true)

let audioEngine = AVAudioEngine()
let audioPlayerNode = AVAudioPlayerNode()

audioEngine.attach(audioPlayerNode)
audioEngine.connect(audioPlayerNode, to: audioEngine.outputNode, format: nil)
audioEngine.inputNode.installTap(onBus: 0, bufferSize: 8000, format: nil) { buffer, time in
  // process audio data
}

let cmdCenter = MPRemoteCommandCenter.shared()

cmdCenter.playCommand.addTarget { event in
  print("registered play")
  try! audioEngine.start()
  return .success
}
cmdCenter.playCommand.isEnabled = true

cmdCenter.pauseCommand.addTarget { event in
  // ---- THIS IS NEVER CALLED ----
  print("registered pause")
  audioEngine.pause()
  return .success
}
cmdCenter.pauseCommand.isEnabled = true

I also published an example app showing the problem on GitHub:
https://github.com/erksch/ios-headset-record

erksch
  • 501
  • 4
  • 14
  • Maybe make a new small project whose only purpose is to get these button presses to register (having no audosession or player etc). Once it works, then its only a matter of what the difference is between that and your code above. – Nerdy Bunz Nov 04 '22 at 18:49
  • Thank you! But as I see it, the only way to receive button presses is by actually starting an audio player. The button presses then come through the command center that controls the player. – erksch Nov 10 '22 at 00:10

0 Answers0