Skip to content

Commit 998ec6e

Browse files
committed
- Fix crash with bluetooth headphone
1 parent 76dfa4f commit 998ec6e

2 files changed

Lines changed: 49 additions & 19 deletions

File tree

example/src/App.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function App() {
4242
}, [refRecorder]);
4343

4444
React.useEffect(() => {
45-
refPlayer?.current?.createPlayer(true, 50);
45+
refPlayer?.current?.createPlayer(true, 500);
4646
}, [refPlayer]);
4747

4848
return (
@@ -51,10 +51,10 @@ export default function App() {
5151
{!isHide && <AudioWaveformView.Recorder
5252
ref={refRecorder}
5353
style={{ width: 400, height: 200 }}
54-
gap={5}
55-
waveWidth={10}
56-
radius={5}
57-
minHeight={60}
54+
gap={3}
55+
waveWidth={3}
56+
radius={3}
57+
minHeight={1}
5858
gravity={'center'}
5959
barPgColor={'#FF0000'}
6060
barBgColor={'#0000FF'}
@@ -88,10 +88,10 @@ export default function App() {
8888
{!isHide && <AudioWaveformView.Player
8989
ref={refPlayer}
9090
style={{ width: 400, height: 200 }}
91-
gap={5}
92-
waveWidth={10}
93-
radius={5}
94-
minHeight={60}
91+
gap={3}
92+
waveWidth={6}
93+
radius={3}
94+
minHeight={1}
9595
gravity={'center'}
9696
playbackSpeed={playbackSpeed}
9797
barPgColor={'#FF0000'}

ios/audio/recorder/AudioRecorderManager.swift

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)