Skip to content

Instantly share code, notes, and snippets.

@ekurutepe
Created January 29, 2020 14:58
Show Gist options
  • Save ekurutepe/8539e0b8e953053127198b9aca8d8703 to your computer and use it in GitHub Desktop.
Save ekurutepe/8539e0b8e953053127198b9aca8d8703 to your computer and use it in GitHub Desktop.
private func configureMicrophone() -> Bool {
session.beginConfiguration()
defer {
session.commitConfiguration()
}
session.usesApplicationAudioSession = true
session.automaticallyConfiguresApplicationAudioSession = false
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: .allowBluetooth)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print("Error messing with audio session: \(error)")
}
if let availableInputs = AVAudioSession.sharedInstance().availableInputs {
print("Found \(availableInputs.count) inputs")
let selectedInput: AVAudioSessionPortDescription
if let usbInput = availableInputs.first(where:{ $0.portType == .usbAudio }) {
selectedInput = usbInput
} else if let lineInput = availableInputs.first(where:{ $0.portType == .lineIn }) {
selectedInput = lineInput
} else if let micInput = availableInputs.first(where:{ $0.portType == .headsetMic }) {
selectedInput = micInput
} else if let bluetoothInput = availableInputs.first(where:{ $0.portType == .bluetoothHFP }) {
selectedInput = bluetoothInput
} else if let builtInInput = availableInputs.first(where:{ $0.portType == .builtInMic }) {
selectedInput = builtInInput
} else {
fatalError("could not find a suitable input")
}
print("setting preferred input \(selectedInput)")
do {
try AVAudioSession.sharedInstance().setPreferredInput(selectedInput)
} catch {
print("Error setting preferred input: \(error)")
}
}
// Find the microphone
guard let microphone = AVCaptureDevice.default(for: .audio) else {
print("Could not find the microphone")
return false
}
// Add the microphone input to the session
do {
microphoneDeviceInput = try AVCaptureDeviceInput(device: microphone)
guard let microphoneDeviceInput = microphoneDeviceInput,
session.canAddInput(microphoneDeviceInput) else {
print("Could not add microphone device input")
return false
}
session.addInputWithNoConnections(microphoneDeviceInput)
} catch {
print("Could not create microphone input: \(error)")
return false
}
// Find the audio device input's back audio port
guard let microphoneDeviceInput = microphoneDeviceInput,
let backMicrophonePort = microphoneDeviceInput.ports(
for: .audio,
sourceDeviceType: microphone.deviceType,
sourceDevicePosition: .back).first else {
print("Could not find the back camera device input's audio port")
return false
}
// Find the audio device input's front audio port
guard let frontMicrophonePort = microphoneDeviceInput.ports(
for: .audio,
sourceDeviceType: microphone.deviceType,
sourceDevicePosition: .front).first else {
print("Could not find the front camera device input's audio port")
return false
}
// Add the back microphone audio data output
guard session.canAddOutput(backMicrophoneAudioDataOutput) else {
print("Could not add the back microphone audio data output")
return false
}
if frontMicrophonePort == backMicrophonePort {
print("front and back mic ports are the SAME!")
}
session.addOutputWithNoConnections(backMicrophoneAudioDataOutput)
backMicrophoneAudioDataOutput.setSampleBufferDelegate(self, queue: dataOutputQueue)
// Add the front microphone audio data output
guard session.canAddOutput(frontMicrophoneAudioDataOutput) else {
print("Could not add the front microphone audio data output")
return false
}
session.addOutputWithNoConnections(frontMicrophoneAudioDataOutput)
frontMicrophoneAudioDataOutput.setSampleBufferDelegate(self, queue: dataOutputQueue)
// Connect the back microphone to the back audio data output
let backMicrophoneAudioDataOutputConnection = AVCaptureConnection(inputPorts: [backMicrophonePort], output: backMicrophoneAudioDataOutput)
guard session.canAddConnection(backMicrophoneAudioDataOutputConnection) else {
print("Could not add a connection to the back microphone audio data output")
return false
}
session.addConnection(backMicrophoneAudioDataOutputConnection)
// Connect the front microphone to the back audio data output
let frontMicrophoneAudioDataOutputConnection = AVCaptureConnection(inputPorts: [frontMicrophonePort], output: frontMicrophoneAudioDataOutput)
guard session.canAddConnection(frontMicrophoneAudioDataOutputConnection) else {
print("Could not add a connection to the front microphone audio data output")
return false
}
session.addConnection(frontMicrophoneAudioDataOutputConnection)
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment