How to get IMU data and audio data simultaneously in App

I want to record both IMU data and Audio data from Airpods Pro. I have tried many times, and I failed. I can successfully record the IMU data and iPhone's microphone data simultaneously. When I choose Airpods Pro's microphone in the setCategory() function, the IMU data collection process stopped.

If I change recordingSession.setCategory(.playAndRecord, mode: .default, options: .allowBluetooth) to ecordingSession.setCategory(.playAndRecord, mode: .default), everything is okay except the audio is recorded from the handphone. If I add options: .allowBluetooth, the IMU update will stop. Could you give me some suggestions for this?

Below are some parts of my code.

  let My_IMU = CMHeadphoneMotionManager()
  let My_writer = CSVWriter()
  var write_state: Bool = false
  func test()
  {
    recordingSession = AVAudioSession.sharedInstance()

    do {
      try recordingSession.setCategory(.playAndRecord, mode: .default, options: .allowBluetooth)
      try recordingSession.setActive(true)
      recordingSession.requestRecordPermission() { [unowned self] allowed in
        DispatchQueue.main.async {
          if allowed == false {print("failed to record!")}
        }
      }
    } catch {
      print("failed to record!")
    }
     
     
    let audioFilename = getDocumentsDirectory().appendingPathComponent("test_motion_Audio.m4a")
    let settings = [
      AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
      AVSampleRateKey: 8000,
      AVNumberOfChannelsKey: 1,
      AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]
    do
    {
      audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
      audioRecorder.delegate = self
      audioRecorder.record()
    }
    catch
    {
      print("Fail to record!")
      finishRecording()
    }
     
    write_state.toggle()
    let dir = FileManager.default.urls(
     for: .documentDirectory,
     in: .userDomainMask
    ).first!
     
    let filename = "test_motion_Audio.csv"
    let fileUrl = dir.appendingPathComponent(filename)
    My_writer.open(fileUrl)
     
    APP.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: {[weak self] motion, error in
      guard let motion = motion, error == nil else { return }
      self?.My_writer.write(motion)
    })
  }
Post not yet marked as solved Up vote post of wang0665 Down vote post of wang0665
3.1k views

Replies

I came across the same problems recently. Did you solve the problem? If so, would you mind to tell me the reason and how to solve it?

Same issue when set playAndRecord category, cause I need motion data when my app providing voice chat. But this will work when either play only or record only. So anyone has any update for this issue? Is this a bug of airPods pro for Apple? Addition info: when change output to phone build-in mic, or disconnect then connect again several times(the repeat count cannot fixed), the motion update will resume.

Following up on this a year later - has anyone found a solution for this?

Following up, did anyone find a solution to this?