首页 > 解决方案 > 在 Android 中配置可用于 Google Speech To Text API 的录制文件?

问题描述

我想从 Android 手机录制音频文件,然后想将此音频文件用于 Google Speech To Text API。我已经成功测试了从网站转换的 mp3 文件,但是当我尝试使用自己的录音文件时,Google 总是给出错误"Bad Encoding"。我尝试了很多配置,但仍然无法成功。

这是在 Android 中创建 mp3 文件的代码:

            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            mediaRecorder.setAudioSamplingRate(48000);

以下是将音频文件发送到 Google API 的代码:

     try {
            mApi.recognize(
                    RecognizeRequest.newBuilder()
                            .setConfig(RecognitionConfig.newBuilder()
                                    .setEncoding(RecognitionConfig.AudioEncoding.ENCODING_UNSPECIFIED)
                                    .setLanguageCode("en-US")
                                    .setSampleRateHertz(48000)
                                    .build())
                            .setAudio(RecognitionAudio.newBuilder()
                                    .setContent(ByteString.readFrom(stream))
                                    .build())
                            .build(),
                    mFileResponseObserver);
        } catch (IOException e) {
            Log.e(TAG, "Error loading the input", e);
        }

以上代码适用于使用在线网站创建的 mp3 文件。

标签: androidaudio-recordinggoogle-cloud-speech

解决方案


推荐阅读