android - 在 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 文件。
解决方案
推荐阅读
- java - 在 Java 流中的终端操作之后使用中间操作
- c - char * 的范围与 C 中的 char 数组比较
- javascript - 如何通过打印推断的变量类型来调试 TypeScript?
- javascript - 如何将 JSP 文件中导入的 Java 常量访问到 Javascript 函数中
- python - 通过多个 FK 属性进行查询集过滤
- sql-server - 是否可以升级 GCP Cloud Sql Server Express 实例的版本?
- javascript - 是否可以编写一个函数来使用 Angular 9 过滤嵌套数组?
- python - 缩放时 PIL 图像到 Pygame 图像错误
- java - 以最少的代码更改在数据库之间切换的设计模式
- sql-server - 如何从 Autodesk forge 的模型衍生 API 将更新的数据保存到 sql server