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 PlayRecordVC: UIViewController, AVAudioRecorderDelegate {
let audioSession = AVAudioSession.sharedInstance()
let player = AVAudioPlayerNode()
let engine = AVAudioEngine()
override func viewDidLoad() {
super.viewDidLoad()
do{
try audioSession.setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth])
try audioSession.overrideOutputAudioPort(.speaker)
try audioSession.setActive(true)
} catch{
print(error.localizedDescription)
}
let input = engine.inputNode
engine.attach(player)
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
self.player.scheduleBuffer(buffer)
print(buffer)
}
}
@IBAction func start(_ sender: UIButton) {
try! engine.start()
player.play()
}
@IBAction func stop(_ sender: UIButton) {
engine.stop()
player.stop()
}
}
UPDATE:
When I add the line audioSession.overrideOutputAudioPort(.speaker)
no audio plays at all (neither from the Bluetooth earbuds nor the phone's internal speaker)