Temporary fix for audio delay

This commit is contained in:
Ali 2019-11-08 15:07:53 +04:00
parent ee64afdf1a
commit eac7b8d948

View File

@ -19,7 +19,7 @@ public enum ManagedAudioSessionType: Equatable {
} }
} }
private func nativeCategoryForType(_ type: ManagedAudioSessionType, headphones: Bool) -> AVAudioSession.Category { private func nativeCategoryForType(_ type: ManagedAudioSessionType, headphones: Bool, outputMode: AudioSessionOutputMode) -> AVAudioSession.Category {
switch type { switch type {
case .play: case .play:
return .playback return .playback
@ -29,7 +29,12 @@ private func nativeCategoryForType(_ type: ManagedAudioSessionType, headphones:
if headphones { if headphones {
return .playback return .playback
} else { } else {
switch outputMode {
case .custom(.speaker), .system:
return .playAndRecord return .playAndRecord
default:
return .playback
}
} }
} }
} }
@ -638,25 +643,29 @@ public final class ManagedAudioSession {
self.currentTypeAndOutputMode = (type, outputMode) self.currentTypeAndOutputMode = (type, outputMode)
do { do {
print("ManagedAudioSession setting category for \(type)") let nativeCategory = nativeCategoryForType(type, headphones: self.isHeadsetPluggedInValue, outputMode: outputMode)
print("ManagedAudioSession setting category for \(type) (native: \(nativeCategory))")
var options: AVAudioSession.CategoryOptions = [] var options: AVAudioSession.CategoryOptions = []
switch type { switch type {
case .play: case .play:
break break
case .playWithPossiblePortOverride: case .playWithPossiblePortOverride:
if case .playAndRecord = nativeCategory {
if #available(iOSApplicationExtension 10.0, iOS 10.0, *) { if #available(iOSApplicationExtension 10.0, iOS 10.0, *) {
options.insert(.allowBluetoothA2DP) options.insert(.allowBluetoothA2DP)
} else { } else {
options.insert(.allowBluetooth) options.insert(.allowBluetooth)
} }
}
case .record, .voiceCall: case .record, .voiceCall:
options.insert(.allowBluetooth) options.insert(.allowBluetooth)
} }
print("ManagedAudioSession setting active true") print("ManagedAudioSession setting active true")
if #available(iOSApplicationExtension 11.0, iOS 11.0, *) { if #available(iOSApplicationExtension 11.0, iOS 11.0, *) {
try AVAudioSession.sharedInstance().setCategory(nativeCategoryForType(type, headphones: self.isHeadsetPluggedInValue), mode: type == .voiceCall ? .voiceChat : .default, policy: .default, options: options) try AVAudioSession.sharedInstance().setCategory(nativeCategory, mode: type == .voiceCall ? .voiceChat : .default, policy: .default, options: options)
} else { } else {
AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:error:"), with: nativeCategoryForType(type, headphones: self.isHeadsetPluggedInValue)) AVAudioSession.sharedInstance().perform(NSSelectorFromString("setCategory:error:"), with: nativeCategory)
try AVAudioSession.sharedInstance().setMode(type == .voiceCall ? .voiceChat : .default) try AVAudioSession.sharedInstance().setMode(type == .voiceCall ? .voiceChat : .default)
} }
} catch let error { } catch let error {
@ -716,12 +725,8 @@ public final class ManagedAudioSession {
} }
} }
case .speakerIfNoHeadphones: case .speakerIfNoHeadphones:
if !self.isHeadsetPluggedInValue {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
} else {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(.none) try AVAudioSession.sharedInstance().overrideOutputAudioPort(.none)
} }
}
if resetToBuiltin { if resetToBuiltin {
var updatedType = type var updatedType = type
@ -778,9 +783,11 @@ public final class ManagedAudioSession {
private func updateOutputMode(_ outputMode: AudioSessionOutputMode) { private func updateOutputMode(_ outputMode: AudioSessionOutputMode) {
if let (type, currentOutputMode) = self.currentTypeAndOutputMode, currentOutputMode != outputMode { if let (type, currentOutputMode) = self.currentTypeAndOutputMode, currentOutputMode != outputMode {
self.currentTypeAndOutputMode = (type, outputMode) //self.currentTypeAndOutputMode = (type, outputMode)
do { do {
try self.setupOutputMode(outputMode, type: type) try self.setup(type: type, outputMode: outputMode, activateNow: true)
//try self.setupOutputMode(outputMode, type: type)
//try self.activate()
} catch let error { } catch let error {
print("ManagedAudioSession overrideOutputAudioPort error \(error)") print("ManagedAudioSession overrideOutputAudioPort error \(error)")
} }