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.
Related
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")
}
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.
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.
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)
}
I would like to insert an alarm clock function in an iOS app I am developing, and as a reference, I installed a popular App called "Alarmy."
I managed to keep my app running in the background, just using AVAudioSession properties; however, I noticed that the app consumes a lot of battery during the phone sleep.
After some testing, I think this is due to the app activating the speakers (and keeping them ON) immediately after the AVAudioSession activation.
Even if there is no actual sound playing until the audioPlayer.play(atTime: audioPlayer.deviceCurrentTime + Double(seconds)) is triggered, if I get very very close to my iPhone 7 speakers, I can hear the little buzzing sound that indicates that the speakers are ON. This implicates that the speakers are playing an "empty sound" de facto.
This buzzing sound does not exist when I set the alarm with Alarmy; it just starts playing when it is supposed to.
I did not find any other way to maintain my app in the background and play an alarm sound at a specified time. There are Local Notifications, of course, but they do not allow to play a sound when the phone is silenced.
Going back to "Alarmy," I've seen that they are not only able to play a background alarm without any need to activate the speakers first, but they are also able to put the volume at the max level in the background. Are they maybe triggering some other iOS background mode to achieve those, perhaps using Background Fetch or Processing in some clever way? Is there any known way to replicate those behaviors?
Thanks in advance!
Here is the current code I use to set the alarm:
private func setNewAlarm(audioPlayer: AVAudioPlayer, seconds: Int, ringtone: String) {
do {
self.setNotificationAlarm(audioPlayer: audioPlayer, seconds: seconds, ringtone: ringtone, result: result)
//This calls the method I use to set a secondary alarm using local notifications, just in case the user closes the app
try AVAudioSession.sharedInstance().setActive(false)
try AVAudioSession.sharedInstance().setCategory(.playback, options: [ .mixWithOthers])
try AVAudioSession.sharedInstance().setActive(true)
} catch let error as NSError {
print("AVAudioSession error: \(error.localizedDescription)")
}
audioPlayer.prepareToPlay()
audioPlayer.play(atTime: audioPlayer.deviceCurrentTime + Double(seconds))
result(true)
}
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.