Я настраиваю Avaudiosession таким образом в AppDelegate сразу после начала приложения:
Код: Выделить всё
let session = AVAudioSession.sharedInstance()
try session.setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth, .defaultToSpeaker])
try session.setActive(true)
< /code>
Настройка сеанса захвата Pseudocode < /p>
let captureSession = AVCaptureSession()
let audioDevice = AVCaptureDevice.default(for: .audio)
let audioInput = try AVCaptureDeviceInput(device: audioDevice!)
if captureSession.canAddInput(audioInput) {
captureSession.addInput(audioInput)
}
// Setup output to write to file
// ...
// When user tap recording
captureSession.startRunning()
// When user taps stop
captureSession.stopRunning()
Код: Выделить всё
@objc func handleRouteChange(notification: Notification) {
guard let userInfo = notification.userInfo,
let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt,
let reason = AVAudioSession.RouteChangeReason(rawValue: reasonValue) else {
return
}
// Switch over the route change reason.
switch reason {
case .newDeviceAvailable: // New device found.
// ... how to add this new connected device as input to the AVCaptureSession?
case .oldDeviceUnavailable: // Old device removed.
// ... how to remove old device and reconnect iPhone internal mic as a input?
default: ()
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... is-running
Мобильная версия