Playing using bluetooth in AVAudioSession - ios

I'm trying to play the voice using a bluetooth device using AVAudioSession. Here is my code which I've tried putting in both appdelegate.swift's didFinishLaunchingWithOptions and viewcontroller.swift (once at a time)
import AVFoundation
var audioSession:AVAudioSession = AVAudioSession.sharedInstance()
audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: AVAudioSessionCategoryOptions.AllowBluetooth, error: nil)
So the code executes perfectly but does not use the connected bluetooth device. Actually, I opened control center on iPhone which says audio source as bluetooth device when app is not opened, but bluetooth device option disappears as soon as app is opened...
The rest of code(if thats of any use) is-
var engine = AVAudioEngine()
var input = engine.inputNode
var output = engine.outputNode
var format = input.inputFormatForBus(0)
var error:NSError?
var audioSession:AVAudioSession = AVAudioSession.sharedInstance() engine.startAndReturnError(&error)
It basically is a live audio player, which takes voice from mic and plays it to AUX, speaker, (bluetooth)

You are right, setting option AllowBluetooth is one and only way to turn on bluetooth in your application. But there could be a few problems with bluetooth:
Your bluetooth device does not support protocol A2DP (Advanced Audio Distribution Profile), you can check it by playing music through standard Music app.
If you setup preferredBufferDuration or preferredSampleRate it also may affect bluetooth.
Perhaps the problem lies in this.

Related

Does iOS Reduce Speaker Volume for Apps Using a Microphone?

I am developing an Xcode/Swift/SwiftUI app for real-time music visualization. I allow the user to push a button to toggle between microphone-input and file-play input (but never both at the same time). My app runs fine on my Mac and on my iPad, but on my iPhone, the speaker audio is only at half-volume (and appears to be only coming from the back speakers) - even when I am in file-play mode. I have traced the problem to one offending line in my code - namely the declaration
let mic = engine.inputNode // where engine = AVAudioEngine()
When I comment-out this line, the iPhone speaker level (for file-play mode) is fine. But when I un-comment it, the iPhone speaker level is barely audible. Even when I wrap this line inside a conditional if(micEnabled){} construct, the sound level is fine at first; but as soon as I select the microphone and then toggle back to file-play, the volume again decreases.
I suspect that iOS detects when a microphone is declared and automatically reduces the speaker volume to avoid audio feedback. This would make sense because nobody wants music playing when they are speaking on a telephone call. But it would also make sense to provide developers a way to override this feature if they want to handle it themselves. In my case, for the microphone-input case, I purposely assign the audio stream a zero-volume after it is tapped and before going to the speaker.
My source code is available here. All of the audio code is inside the MuVis / Shared / AudioManager.swift class.
Can anyone help me to get the file-play mode to work with full volume on my iPhone - while also allowing the user the option to select microphone-input mode?
Many thanks to Rob Napier for pointing me in the right direction for solving my problem.
As a macOS-only developer, I had ignored AVAudioSession (since it caused compiler errors on macOS). When I converted my MuVis app from macOS-only to multiplatform, I simply started a new Xcode project with the appropriate multiplatform settings, and then pasted my existing code into the shared folder. After cleaning up a few errors (mostly calls to NSObject), it magically worked on all Apple platforms - except for the iPhone audio problem described in my question. After a little research and a lot of trial-and-error, I found that my audio-volume problem is solved by inserting the following code into my setupAudio() function:
#if os(iOS)
// For iOS devices, set the audioSession category, mode, and options:
let session = AVAudioSession.sharedInstance() // Get the singleton instance of an AVAudioSession.
do {
if(filePlayEnabled) {
// This is required by iOS to prevent output audio from going only to the iPhone's rear speaker.
try session.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: [.defaultToSpeaker])
}
else {
try session.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: [])
}
} catch { print("Failed to set audioSession category.") }
#endif
Again, thank you Rob.

iOS App Bluetooth Audio Coming out in "Phone Mode."

I have an iOS app which is producing text to speech (TTS) audio (AVSpeechSynthesizer). One user is saying that the audio over his car Bluetooth speaker is coming out in "phone mode" (presumably the audio when making or receiving phone calls) as opposed to "music mode" the way that apps like Youtube and the music and maps apps are. This also causes the handling of incoming phone calls not to work properly with the car Bluetooth speaker.
Unfortunately, I am at a loss to understand why, or even that there is a distinction between "phone" and "music" mode. When using the phone's speakers, there is no such problem with handling incoming phone calls. The issue is only with Bluetooth.
The AVAudioSession initialization code is as follows.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
do {
let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSession.Category.playAndRecord, options: [.defaultToSpeaker, .allowBluetooth, .allowBluetoothA2DP])
try session.overrideOutputAudioPort(AVAudioSession.PortOverride.none)
try session.setActive(true, options: .notifyOthersOnDeactivation)
} catch let error {
print("audioSession properties weren't set. Error: \(error.localizedDescription)")
}
return true
}
Also, the AVSpeechSynthesizer code is as follows
let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: newText)
synthesizer.speak(utterance)
Is there anything else this code should be doing, or perhaps is doing wrong?
Thanks in advance.
What you're calling "phone mode" is HFP (Hands Free Profile). You've included .allowBluetooth which means "prefer using HFP." (It's a very confusing enum name.)
What you're calling "music mode" is A2DP, which you're allowing via .allowBluetoothA2DP.
However, A2DP is not bidirectional, which you're requesting with .playAndRecord. So the session uses HFP.
The audio quality of HFP is notably worse than A2DP.
For TTS, there shouldn't be a need for a microphone, so you can replace .playAndRecord with .play (and I'd probably drop .allowBluetooth). If you require a microphone for some other purpose, you should drop .allowBluetoothA2DP, and there's no (standard) way to avoid using HFP to communicate over Bluetooth.
There are non-standard ways to solve this if you were the manufacturer of the car and the app. You could open a second A2DP channel to the phone, or you could implement a proprietary microphone protocol over BLE or iAP2. But there's no way to do this with standard devices while talking to an iPhone. (If both devices support aptX, there are some other options, but iPhones don't and I haven't heard any hints that they will.)
Note that you can change the category and options, and activate or deactivate the session at any time. So if you need the microphone sometimes, you can switch to .playAndRecord only when you need it and minimize the impact on users when they don't need the microphone.

AVSpeechSynthesizer uses Apple Watch's speaker and not headset as output channel

I am using AVSpeechSynthesizer inside a WatchKit App Extension.
The logic is simple, and can be summarized as the following:
let utterance = AVSpeechUtterance(string: "Hello, World")
synth.speak(utterance)
This works fine but the speech always gets relayed via the Apple Watch's onboard speakers.
I require the speech to come through my airpods which are connected to my iPhone.
Previously I had delegated the task to the iPhone via WatchConnectivity which worked well but due to delays in WatchConnectivity communication, I moved the control logic directly onto the Apple Watch.
I thought watchOS would internally hand over the audio to the BLE device but it's not going as planned.
Maybe I am missing something?
Do I need to specify the audio channel synth.outputChannels?
Do I need to show the AirPlay popup asking user to select an audio output source?
If so how do I go about this?
I am unable to find much information on this matter online so any help would be greatly appreciated.
I am just trying to find a way to get the speech over my AirPods.
You can use the following code to display an audio device picker and direct audio to the selected device:
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSession.Category.playback,
mode: .default,
policy: .longFormAudio,
options: [])
session.activate(options: []) { (success, error) in
// Check for an error and play audio.
if let err = error) {
print(err)
}
}
} catch {
print(error)
}

Can I use ReplayKit to record both microphone and system audio?

There is not much documentation online about this because it's an odd task. I am trying to record my screen, the internal microphone, and the system audio at the same time using ReplayKit.
Here is how I am recording my screen right now:
if([self.screenRecorder isAvailable]){
[self.screenRecorder setMicrophoneEnabled:YES];
[self.screenRecorder startRecordingWithHandler:nil];
}
When this runs, the user is prompted to record with the microphone, or without the microphone. Could I possibly do both? Is there a workaround? If I choose microphone, when my app plays sound, the microphone gets disabled.
If anyone could propose a possible solution that does not involve replaykit, that would be greatly appreciated too!
Thanks
yes, it's possible, you can using AVAudioEngine which provide manual rendering mode, two playerNode (audio app, audio mic) into mixerNode and render.
So after looking into this you can also just do this very simply using the AVAudioSession API:
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.mixWithOthers)

iOS - is possible to record audio from Bluetooth headset mic and play in device speaker

I am trying to record audio by Bluetooth headset mic and play by device speaker.
I have seen few stack-overflow post regrading the same but still i am not getting is this possible or not ? and if possible then how to do it? if someone have any idea please inform me.
It's possible. Use bluetooth option when setting audio session category.
let audioSession = AVAudioSession.sharedInstance()
_ = try? audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .allowBluetooth)
_ = try? audioSession.setActive(true)

Resources