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) }) }