更改语音识别的音频源 - Android

问题描述 投票:0回答:1

问题是我们使用 HDMI 捕获设备来渲染帧和音频。 该设备使用 UAC 协议,该协议注册为输入音频设备并选择作为语音引擎的默认输入源。 我们想要更改语音引擎的输入音频源以使用内置底部麦克风或后置摄像机麦克风。

互联网上可用的资源有限。一些相关问题

  1. Android语音识别自定义音频源
  2. Android同时进行语音识别和录音

我们尝试了以下代码。以下解决方案的灵感来自 Github.com/KieronQuinn/AmbientMusicMod

此代码使语音引擎的源静音,但不起作用!

val SAMPLE_RATE = 44100
        val CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO
        val AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT
//        val bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT)

        val audioRecord = AudioRecord.Builder()
            .setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
            .setAudioFormat(AudioFormat.Builder()
                .setEncoding(AUDIO_FORMAT)
                .setSampleRate(SAMPLE_RATE)
                .setChannelMask(CHANNEL_CONFIG)
                .build())
//            .setBufferSizeInBytes(bufferSize)
            .build()

        val fileDescriptors = ParcelFileDescriptor.createReliablePipe()
        val clientRead = fileDescriptors[0]
        audioRecord.startRecording()

        Thread {

            try {
//                        datagramSocket.send(datagramPacket)
                val audioSink = fileDescriptors[1]
                ParcelFileDescriptor.AutoCloseOutputStream(audioSink).use { fos ->
                    val buffer = ByteArray(audioRecord.bufferSizeInFrames)
                    while (isListening) {
                        val bytesRead =
                            audioRecord.read(buffer, 0, buffer.size, AudioRecord.READ_BLOCKING)
                        if (bytesRead > 0) {

//                        datagramSocket.send(datagramPacket)

                            fos.write(buffer)

                        }
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }


            audioRecord.stop()
            audioRecord.release()
        }.start()

        speechIntent.putExtra("android.speech.extra.AUDIO_SOURCE", clientRead)
        speechIntent.putExtra("android.speech.extra.AUDIO_SOURCE_ENCODING", AUDIO_FORMAT)
        speechIntent.putExtra("android.speech.extra.AUDIO_SOURCE_SAMPLING_RATE", SAMPLE_RATE)
        speechIntent.putExtra("android.speech.extra.AUDIO_SOURCE_CHANNEL_COUNT", CHANNEL_CONFIG)
        speechRecognizer.startListening(speechIntent)

安卓13 谷歌语音识别 v20230807 Google 群组主题

或者

有任何以编程方式解决

disable automatic routing to USB audio peripherals
的解决方法吗!?

android audio usb libusb uvc
1个回答
0
投票

您的应用程序应已授予 WRITE_SECURE_SETTINGS 权限。然后拨打

Settings.Secure.putInt(context.getContentResolver(), "usb_audio_automatic_routing_disabled", 1)

伊尔凡·拉蒂夫

我们可以使用以下 ADB 命令授予 WRITE_SECURE_SETTINGS 权限。

adb shell pm grant app.package.name android.permission.WRITE_SECURE_SETTINGS

旁注:目前,这是解决我们问题的唯一方法。我希望 Google/AOSP 能够考虑这个案例。

© www.soinside.com 2019 - 2024. All rights reserved.