@@ -67,6 +67,29 @@ class AudioRecorderManager: RCTEventEmitter {
6767 }
6868 }
6969
70+ func isHeadsetPluggedIn( ) -> Bool {
71+ let route = AVAudioSession . sharedInstance ( ) . currentRoute
72+ for desc in route. outputs {
73+ print ( desc. portType)
74+ if desc. portType == AVAudioSession . Port. headphones || desc. portType == AVAudioSession . Port. bluetoothA2DP {
75+ return true
76+ }
77+ }
78+ return false
79+ }
80+
81+ func isHeadSetBluetooth( ) -> Bool {
82+ let arrayInputs = AVAudioSession . sharedInstance ( ) . availableInputs
83+ for port in arrayInputs ?? [ ] {
84+ print ( port. portType)
85+
86+ if port. portType == AVAudioSession . Port. bluetoothHFP {
87+ return true
88+ }
89+ }
90+ return false
91+ }
92+
7093 // MARK:- Recording
7194 func startRecording( isResume: Bool = false , with completion: @escaping ( URL ? , Error ? ) -> Void ) {
7295 askPermission { isGranted, Error in
@@ -78,9 +101,16 @@ class AudioRecorderManager: RCTEventEmitter {
78101 self . audioEngine = AVAudioEngine ( )
79102 do {
80103 let session = AVAudioSession . sharedInstance ( )
81- try session. setCategory ( . playAndRecord,
82- mode: AVAudioSession . Mode. default,
83- options: [ . mixWithOthers, . allowBluetooth, . allowBluetoothA2DP, . defaultToSpeaker] )
104+ if self . isHeadsetPluggedIn ( ) || self . isHeadSetBluetooth ( ) {
105+ try session. setCategory ( . playAndRecord,
106+ mode: AVAudioSession . Mode. default,
107+ options: [ . mixWithOthers, . allowBluetooth, . allowBluetoothA2DP] )
108+ } else {
109+ try session. setCategory ( . playAndRecord,
110+ mode: AVAudioSession . Mode. default,
111+ options: [ . mixWithOthers, . allowBluetooth, . allowBluetoothA2DP, . defaultToSpeaker] )
112+ }
113+
84114 try session. setActive ( true , options: . notifyOthersOnDeactivation)
85115 } catch let error as NSError {
86116 NotificationCenter . default. post ( name: . errorNotification, object: self , userInfo: [ errorKey: error. localizedDescription] )
@@ -94,22 +124,22 @@ class AudioRecorderManager: RCTEventEmitter {
94124 guard let strongSelf = self else {
95125 return
96126 }
97- buffer. frameLength = 2048
127+ buffer. frameLength = buffer . frameCapacity
98128 guard let channelData = buffer. floatChannelData else {
99- return
129+ return
100130 }
101131
102132 let channelDataValue = channelData. pointee
103133 // 4
104134 let channelDataValueArray = stride (
105- from: 0 ,
106- to: Int ( buffer. frameLength) ,
107- by: buffer. stride)
108- . map { channelDataValue [ $0] }
135+ from: 0 ,
136+ to: Int ( buffer. frameLength) ,
137+ by: buffer. stride)
138+ . map { channelDataValue [ $0] }
109139
110140 // 5
111141 let rms = sqrt ( channelDataValueArray. map {
112- return $0 * $0
142+ return $0 * $0
113143 }
114144 . reduce ( 0 , + ) / Float( buffer. frameLength) )
115145
0 commit comments