@@ -149,7 +149,7 @@ index 6c81fce3dd518d3b82e3280940a001c7be84dd0a..16125c679c870e2eae48f8db953dedd3
149149 checkRecordingPermission()
150150 if (recorder.isPrepared) {
151151diff --git a/android/src/main/java/expo/modules/audio/AudioRecorder.kt b/android/src/main/java/expo/modules/audio/AudioRecorder.kt
152- index 2ff4a474fdea3efa652436a8a3960c3669ad298b..63229cea73ef0d2fe245b5d2a2675044ce621dc8 100644
152+ index 2ff4a474fdea3efa652436a8a3960c3669ad298b..1ced279991294d3d81cb20fa339ca73e5d6d6c01 100644
153153--- a/android/src/main/java/expo/modules/audio/AudioRecorder.kt
154154+++ b/android/src/main/java/expo/modules/audio/AudioRecorder.kt
155155@@ -19,6 +19,9 @@ import expo.modules.kotlin.exception.CodedException
@@ -164,7 +164,7 @@ index 2ff4a474fdea3efa652436a8a3960c3669ad298b..63229cea73ef0d2fe245b5d2a2675044
164164 import java.io.File
165165@@ -42,12 +45,19 @@ class AudioRecorder(
166166 var isPrepared = false
167-
167+
168168 private var recorder: MediaRecorder? = null
169169+ private val inputRoute = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
170170+ RecordingInputRoute(context.getSystemService(Context.AUDIO_SERVICE) as AudioManager)
@@ -181,23 +181,23 @@ index 2ff4a474fdea3efa652436a8a3960c3669ad298b..63229cea73ef0d2fe245b5d2a2675044
181181+ private var inputSelectionCanceled = false
182182 private var durationLimitMillis: Long? = null
183183 var useForegroundService = false
184-
184+
185185@@ -90,6 +100,7 @@ class AudioRecorder(
186186 throw AudioRecorderAlreadyPreparedException()
187187 }
188-
188+
189189+ synchronized(this) { inputSelectionCanceled = false }
190190 val recordingOptions = options ?: this.options
191191 val mediaRecorder = createRecorder(recordingOptions)
192192 recorder = mediaRecorder
193193@@ -245,10 +256,15 @@ class AudioRecorder(
194194 }
195-
195+
196196 private fun reset() {
197197+ cancelInputSelection()
198198 recordingTimerJob?.cancel()
199199 recordingTimerJob = null
200-
200+
201201- recorder?.release()
202202+ try {
203203+ recorder?.release()
@@ -207,18 +207,29 @@ index 2ff4a474fdea3efa652436a8a3960c3669ad298b..63229cea73ef0d2fe245b5d2a2675044
207207 recorder = null
208208 isRecording = false
209209 isPaused = false
210- @@ -475,48 +491,59 @@ class AudioRecorder(
210+ @@ -468,55 +484,68 @@ class AudioRecorder(
211+ throw DeviceInfoNotFoundException()
212+ }
213+
214+ - return getMapFromDeviceInfo(deviceInfo)
215+ + return getMapFromDeviceInfo(deviceInfo, recordingInputs(audioManager))
216+ }
217+
218+ private fun hasRecordingPermissions() =
211219 ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
212-
213- fun getAvailableInputs(audioManager: AudioManager) =
220+
221+ - fun getAvailableInputs(audioManager: AudioManager) =
214222- audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS).mapNotNull { deviceInfo ->
215223- val type = deviceInfo.type
216224- if (type == AudioDeviceInfo.TYPE_BUILTIN_MIC || type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO || type == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
217225- getMapFromDeviceInfo(deviceInfo)
218226- } else {
219227- null
220228- }
221- + recordingInputs(audioManager).map(::getMapFromDeviceInfo)
229+ + fun getAvailableInputs(audioManager: AudioManager): List<Bundle> {
230+ + val inputs = recordingInputs(audioManager)
231+ + return inputs.map { getMapFromDeviceInfo(it, inputs) }
232+ + }
222233+
223234+ suspend fun setInput(uid: String, audioManager: AudioManager) {
224235+ val job = currentCoroutineContext().job
@@ -240,17 +251,17 @@ index 2ff4a474fdea3efa652436a8a3960c3669ad298b..63229cea73ef0d2fe245b5d2a2675044
240251+ }
241252+ job?.cancel()
242253+ }
243-
254+
244255 @Suppress("DEPRECATION")
245256- fun setInput(uid: String, audioManager: AudioManager) {
246257- val deviceInfo: AudioDeviceInfo? = getDeviceInfoFromUid(uid, audioManager)
247258+ private suspend fun setInputDevice(uid: String, audioManager: AudioManager) {
248259+ val deviceInfo = getDeviceInfoFromUid(uid, audioManager) ?: throw PreferredInputNotFoundException()
249-
260+
250261 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
251262 throw SetAudioInputNotSupportedException()
252263 }
253-
264+
254265- if (deviceInfo != null && deviceInfo.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
255266- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
256267- audioManager.setCommunicationDevice(deviceInfo)
@@ -281,33 +292,36 @@ index 2ff4a474fdea3efa652436a8a3960c3669ad298b..63229cea73ef0d2fe245b5d2a2675044
281292+ throw error
282293 }
283294 }
284-
295+
285296 private fun getDeviceInfoFromUid(uid: String, audioManager: AudioManager): AudioDeviceInfo? {
286297- val id = uid.toInt()
287298- val audioDevices: Array<AudioDeviceInfo> = audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)
288299+ val audioDevices = recordingInputs(audioManager)
289300 for (device in audioDevices) {
290301- if (device.id == id) {
291- + if (recordingInputUid(device) == uid) {
302+ + if (recordingInputUid(device, audioDevices ) == uid) {
292303 return device
293304 }
294305 }
295306diff --git a/android/src/main/java/expo/modules/audio/AudioUtils.kt b/android/src/main/java/expo/modules/audio/AudioUtils.kt
296- index 81819c1d771bdeec093bb35a65a5e1e1af0cf9d7..ec3e0274d0a84fd8c7960cc0ea1b30058123be54 100644
307+ index 81819c1d771bdeec093bb35a65a5e1e1af0cf9d7..cd0c7624b4cd8a4ebf7ed3b3390cfc76802e8530 100644
297308--- a/android/src/main/java/expo/modules/audio/AudioUtils.kt
298309+++ b/android/src/main/java/expo/modules/audio/AudioUtils.kt
299310@@ -2,6 +2,8 @@ package expo.modules.audio
300-
311+
301312 import android.content.Context
302313 import android.media.AudioDeviceInfo
303314+ import android.media.AudioManager
304315+ import android.os.Build
305316 import android.os.Bundle
306317 import androidx.annotation.OptIn
307318 import androidx.media3.common.Player
308- @@ -19,20 +21,45 @@ fun ensureDirExists(dir: File): File {
309-
310- fun getMapFromDeviceInfo(deviceInfo: AudioDeviceInfo): Bundle {
319+ @@ -17,22 +19,57 @@ fun ensureDirExists(dir: File): File {
320+ return dir
321+ }
322+
323+ - fun getMapFromDeviceInfo(deviceInfo: AudioDeviceInfo): Bundle {
324+ + fun getMapFromDeviceInfo(deviceInfo: AudioDeviceInfo, inputs: List<AudioDeviceInfo>): Bundle {
311325 val map = Bundle()
312326- val type = when (deviceInfo.type) {
313327- AudioDeviceInfo.TYPE_BUILTIN_MIC -> "MicrophoneBuiltIn"
@@ -321,10 +335,10 @@ index 81819c1d771bdeec093bb35a65a5e1e1af0cf9d7..ec3e0274d0a84fd8c7960cc0ea1b3005
321335 map.putString("name", deviceInfo.productName.toString())
322336 map.putString("type", type)
323337- map.putString("uid", deviceInfo.id.toString())
324- + map.putString("uid", recordingInputUid(deviceInfo))
338+ + map.putString("uid", recordingInputUid(deviceInfo, inputs ))
325339 return map
326340 }
327-
341+
328342+ internal fun recordingInputs(manager: AudioManager): List<AudioDeviceInfo> {
329343+ val inputs = manager.getDevices(AudioManager.GET_DEVICES_INPUTS).filter {
330344+ recordingInputType(it.type) != null
@@ -335,7 +349,9 @@ index 81819c1d771bdeec093bb35a65a5e1e1af0cf9d7..ec3e0274d0a84fd8c7960cc0ea1b3005
335349+ emptyList()
336350+ }
337351+ // Some Bluetooth microphone ports appear only after the communication route is activated.
338- + return (inputs + bluetooth).distinctBy(::recordingInputUid)
352+ + return (inputs + bluetooth).distinctBy {
353+ + if (isBluetoothInput(it.type)) recordingInputUid(it) else "port:${it.id}"
354+ + }
339355+ }
340356+
341357+ internal fun isBluetoothInput(type: Int) = type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO ||
@@ -351,7 +367,15 @@ index 81819c1d771bdeec093bb35a65a5e1e1af0cf9d7..ec3e0274d0a84fd8c7960cc0ea1b3005
351367+ else -> null
352368+ }
353369+
354- + internal fun recordingInputUid(device: AudioDeviceInfo): String {
370+ + internal fun recordingInputUid(device: AudioDeviceInfo, inputs: List<AudioDeviceInfo> = emptyList()): String {
371+ + val base = recordingInputBaseUid(device)
372+ + val ambiguous = !isBluetoothInput(device.type) && inputs.any {
373+ + it.id != device.id && recordingInputBaseUid(it) == base
374+ + }
375+ + return if (ambiguous) "$base:port:${device.id}" else base
376+ + }
377+ +
378+ + private fun recordingInputBaseUid(device: AudioDeviceInfo): String {
355379+ // Port IDs and USB addresses (ALSA card numbers) change on reconnect; Bluetooth addresses don't.
356380+ val stable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && isBluetoothInput(device.type)
357381+ return "android:${device.type}:${if (stable) device.address.ifBlank { device.productName } else device.productName}"
@@ -419,3 +443,44 @@ index 0000000000000000000000000000000000000000..cdba9724318cf06afd21b4df6b895b38
419443+ }
420444+ }
421445+ }
446+ diff --git a/android/src/test/java/expo/modules/audio/RecordingInputIdentityTest.kt b/android/src/test/java/expo/modules/audio/RecordingInputIdentityTest.kt
447+ new file mode 100644
448+ index 0000000000000000000000000000000000000000..4dd0567e2170770e9b0f6a17dc4581bc769d5c6a
449+ --- /dev/null
450+ +++ b/android/src/test/java/expo/modules/audio/RecordingInputIdentityTest.kt
451+ @@ -0,0 +1,35 @@
452+ + package expo.modules.audio
453+ +
454+ + import android.media.AudioDeviceInfo
455+ + import android.media.AudioManager
456+ + import io.mockk.every
457+ + import io.mockk.mockk
458+ + import org.junit.Assert.*
459+ + import org.junit.Test
460+ +
461+ + class RecordingInputIdentityTest {
462+ + private fun device(id: Int) = mockk<AudioDeviceInfo> {
463+ + every { [email protected] } returns id464+ + every { type } returns AudioDeviceInfo.TYPE_USB_DEVICE
465+ + every { productName } returns "USB Microphone"
466+ + every { address } returns "card=$id;device=0;"
467+ + }
468+ +
469+ + @Test
470+ + fun identicalUsbMicrophonesRemainSelectable() {
471+ + val first = device(1)
472+ + val second = device(2)
473+ + val manager = mockk<AudioManager> {
474+ + every { getDevices(AudioManager.GET_DEVICES_INPUTS) } returns arrayOf(first, second)
475+ + }
476+ + val inputs = recordingInputs(manager)
477+ + assertEquals(2, inputs.size)
478+ + val firstUid = recordingInputUid(first, inputs)
479+ + val secondUid = recordingInputUid(second, inputs)
480+ + assertNotEquals(firstUid, secondUid)
481+ + assertSame(first, inputs.single { recordingInputUid(it, inputs) == firstUid })
482+ + assertSame(second, inputs.single { recordingInputUid(it, inputs) == secondUid })
483+ + val reconnected = device(99)
484+ + assertEquals(recordingInputUid(first, listOf(first)), recordingInputUid(reconnected, listOf(reconnected)))
485+ + }
486+ + }
0 commit comments