Search code examples
iosswiftswift3avfoundationavaudiorecorder

Cannot record using Bluetooth speaker on iPad + Swift 3


I want to record with Bluetooth Speaker connected to iPad. However, when using AVAudioRecorder, it turns into built-in speaker.

let session = AVAudioSession.sharedInstance()
try! session.setCategory(AVAudioSessionCategoryPlayAndRecord)
try! session.setActive(true)
       
let recordSetting: [String: Any] = [AVSampleRateKey: NSNumber(value: 16000),
            AVFormatIDKey: NSNumber(value: kAudioFormatLinearPCM),
            AVLinearPCMBitDepthKey: NSNumber(value: 16),
            AVNumberOfChannelsKey: NSNumber(value: 1),
            AVEncoderAudioQualityKey: NSNumber(value: AVAudioQuality.max.rawValue)
        ];
        
do {
    self.audioRecorder = try AVAudioRecorder(url: url as URL, settings: recordSetting)
   } catch {
    fatalError(“error”)
   }

Solution

  • OK I Solved the question. If you want to AVAudioRecording with bluetooth speaker or microphone, set the options at "setCategory" method as below.

    try! session.setCategory(AVAudioSessionCategoryPlayAndRecord,
            mode:AVAudioSessionModeDefault,
            options:AVAudioSessionCategoryOptions.allowBluetoothA2DP)
    

    Thanks.