Record and playback using bluetooth earphone’s mic & internal speaker in Swift


I currently have a pair of Bluetooth earbuds. From this post, I have the code needed to retrieve audio from the Bluetooth earbuds’ microphone and then playback the audio through the Bluetooth earbuds. However, I want to modify the code so that I can retrieve audio from the Bluetooth earbuds’ microphone and then playback the audio through the phone’s INTERNAL speaker / through any other pair of earbuds that may be physically connected to the phone. How would I go about doing that? This is my current code:

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioRecorderDelegate {

    let audioSession = AVAudioSession.sharedInstance()
    let player = AVAudioPlayerNode()
    let engine = AVAudioEngine()

    override func viewDidLoad() {

            try audioSession.setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth])
            try audioSession.setActive(true)
        } catch{

        let input = engine.inputNode


        let bus = 0
        let inputFormat = input.inputFormat(forBus: bus)

        engine.connect(player, to: engine.mainMixerNode, format: inputFormat)

        input.installTap(onBus: bus, bufferSize: 512, format: inputFormat) { (buffer, time) -> Void in

    @IBAction func start(_ sender: UIButton) {
        try! engine.start()

    @IBAction func stop(_ sender: UIButton) {


Source: Ios

Categorised as audio, avfoundation, bluetooth, ios, swift


I haven’t tested if this actually causes microphone to still be used by the headphones, but by default to route the audio to the speakers instead of the headphones this should work:

try! audioSession.overrideOutputAudioPort(.speaker)  

Here is the documentation of the method.

There is also this category option you could give to the audio session to have that same effect but also avoid it to be reset by gestures (like attaching new headphones).

If after your test you find out this actually moves also the recording to use the internal microphone, I think there is no other way (at least no other way that I found of).

Mr. Cordell Mohr III

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us