Reputation: 282
I have an avassetwriter
capture session to record a video and 2 avplayerqueues
to playback the immediately recorded video and the other, to playback past saved videos.
My problem is the audio input does not use my bluetooth earphones, and performs playback and record via the iphone device inputs/outputs.
I did not implement any override to default to the speaker, understand I need to handle a route change via notification observer in the case of toggling between bluetooth and device, and also tried setting the AVAudioSession.sharedInstance()
.playbackAndRecording
category to .allowBluetooth
to no avail.
Any guidance would be appreciated as I have not come across an existing answer online..
let audioSession = AVAudioSession.sharedInstance()
//Executed right before playing avqueueplayer media
do {
try audioSession.setCategory(.playback)
try audioSession.setActive(true)
} catch {
fatalError("Error Setting Up Audio Session")
}
//Executed right after avqueueplayer finishes media
do {
try audioSession.setCategory(.recording, options: [.allowBluetooth])
try audioSession.setActive(true)
} catch {
fatalError("Error Setting Up Audio Session")
}
Upvotes: 0
Views: 976
Reputation: 299355
Did you read carefully the documentation for .allowBluetooth
and .allowBluetoothA2DP
? These do not mean what you likely think they mean, since passing both is rarely what developers mean. Do not guess what AVFoundation will do based on the names of things; you must read the documentation. The names of things are not obvious, and are actively misleading in several places.
If you want to record from Bluetooth headphones, you cannot support A2DP. A2DP is a unidirectional protocol (playback only). You will need to use HFP, which is what .allowBluetooth
means. Remove .allowBluetoothA2DP
. Note that this is significantly reduce your audio quality.
If you have distinct periods of recording vs playback, then you want to change your category when you change modes. Do not just set .playAndRecord
because you will record at some point and playback at another. If you switch to a playback-only situation, switch to .playback
. It is legal to change categories while the session is active (again, see the docs; there are many subtle rules).
You haven't listed what your Bluetooth earphones are, so it's not clear whether they support both A2DP and HFP. That has significant impact on how routing occurs. See the docs for .allowBluetoothA2DP
on this.
Upvotes: 2