Avaudiosession .Defaulttospeaker Changes Mic Input

AVAudioSession .defaultToSpeaker changes mic input

Seems like I found a solution. Which is actually very simple. When AVAudioSession category is .defaultToSpeaker (Or overrideOutputAudioPort) apparently the tap input buffer framelength changes to 4800 from 4410.

This strangely happens regardless of which microphone is used. So using

AVAudioSession.sharedInstance().setInputDataSource(datasource);

Does not help.

This difference seems to cause problems later in my code. So mic was actually working, but later in code it was failing to do its job due to a different framelength.

Solution/workaround was I basically hardcoded the framelength in the tap. Since I use a converter I don't expect this to be a problem. This means I can set ".defaultToSpeaker" And mic is still working as expected.

capacity = 4410 (DUH!)

Maybe there are different/better ways to approach this problem. So feel free to add your answer if so.

AudioKit v5 output problems, no sound when AVAudioSession defaultToSpeaker is used

The answer was indeed to add code for AVAudioSession. However, it did not work where I first put it. It only worked for me when I put it in the App delegate didFInishLauchWithOptions. I found this in the AudioKit Cookbook. This works:

class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

#if os(iOS)
self.audioSetup()
#endif

return true
}

#if os(iOS)
func audioSetup() {
let session = AVAudioSession.sharedInstance()

do {
Settings.bufferLength = .short
try session.setPreferredIOBufferDuration(Settings.bufferLength.duration)
try session.setCategory(.playAndRecord,
options: [.defaultToSpeaker, .mixWithOthers])
try session.setActive(true)
} catch let err {
print(err)
}

// Other AudioSession stuff here

do {
try session.setActive(true)
} catch let err {
print(err)
}
}
#endif

}

Routing audio input to receive from TOP microphone on iPhone

Use AVAudioSession to get available inputs. On my iPhone 5 it looks like this.

NSLog(@"%@", [AVAudioSession sharedInstance].availableInputs);

"<AVAudioSessionPortDescription: 0x14554400, type = MicrophoneBuiltIn; name = iPhone Microphone; UID = Built-In Microphone; selectedDataSource = Back>"

Then use one of these inputs to get availableDataSources, like this.

NSLog(@"%@", [AVAudioSession sharedInstance].availableInputs[0].dataSources);

"<AVAudioSessionDataSourceDescription: 0x145afb00, ID = 1835216945; name = Bottom>",
"<AVAudioSessionDataSourceDescription: 0x145b1870, ID = 1835216946; name = Front>",
"<AVAudioSessionDataSourceDescription: 0x145b3650, ID = 1835216947; name = Back>"

We can see that there are actually THREE microphones on this iPhone: top front, top back, and bottom. Now you can set your preferred data source.

AVAudioSessionPortDescription *port = [AVAudioSession sharedInstance].availableInputs[0];
for (AVAudioSessionDataSourceDescription *source in port.dataSources) {
if ([source.dataSourceName isEqualToString:@"Back"]) {
[port setPreferredDataSource:source error:nil];
}
}

AVAudioSession's PlayAndRecord category and AVAudioSessionModeMeasurement are incompatible with defaultToSpeaker option?

While there doesn't seem to be much written about this the documentation makes this "end result of audio output being sent to the receiver rather than speaker" seem like possible intended behavior and not a bug.

let AVAudioSessionModeMeasurement: String

This mode is intended for apps that need to minimize the amount of
system-supplied signal processing to input and output signals. If
recording on devices with more than one built-in microphone, the
primary microphone is used.



Related Topics



Leave a reply



Submit