android - 从语音识别 Intent 记录/保存音频

标签 android speech-recognition speech-to-text

我想保存/录制 Google 识别服务用于语音转文本操作的音频(使用 RecognizerIntent 或 SpeechRecognizer)。

我经历了很多想法:

  1. 来自 RecognitionListener 的 onBufferReceived:我知道,这不起作用,只需对其进行测试以查看会发生什么,并且永远不会调用 onBufferReceived(在 Galaxy nexus 上使用 JB 4.3 进行测试)

  2. 使用了媒体记录器:不工作。它正在破坏语音识别。 mic 只能进行一次操作

  3. 在执行语音转文本 API 以复制临时音频文件之前,尝试查找识别服务将临时音频文件保存在何处,但没有成功

我几乎绝望了,但我才注意到 Google Keep application正在做我需要做的事!我使用 logcat 调试了一些 keep 应用程序,该应用程序还调用“RecognizerIntent.ACTION_RECOGNIZE_SPEECH”(就像我们开发人员一样)来触发语音到文本。但是,如何保存音频?可以是隐藏 API 吗?谷歌是在“作弊”吗?

最佳答案

@Kaarel 的回答几乎是完整的 - 生成的音频在 intent.getData() 中,可以使用 ContentResolver

读取

很遗憾,返回的 AMR 文件质量很差 - 我无法找到获得高质量录音的方法。在 intent.getData() 中,除了“audio/AMR”之外,我尝试的任何值都返回 null。

如果您找到获得高质量录音的方法 - 请发表评论或添加答案!

public void startSpeechRecognition() {
   // Fire an intent to start the speech recognition activity.
   Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
   // secret parameters that when added provide audio url in the result
   intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
   intent.putExtra("android.speech.extra.GET_AUDIO", true);

   startActivityForResult(intent, "<some code you choose>");
}

// handle result of speech recognition
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // the resulting text is in the getExtras:
    Bundle bundle = data.getExtras();
    ArrayList<String> matches = bundle.getStringArrayList(RecognizerIntent.EXTRA_RESULTS)
    // the recording url is in getData:
    Uri audioUri = data.getData();
    ContentResolver contentResolver = getContentResolver();
    InputStream filestream = contentResolver.openInputStream(audioUri);
    // TODO: read audio file from inputstream
}

关于android - 从语音识别 Intent 记录/保存音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23047433/

相关文章:

android - 按钮状态可绘制按下触发多个按钮

speech-recognition - 使用CMU的sphinx4转录非数字数据

node.js - 在浏览器上通过 Node.js 将音频流式传输到 Azure 语音 api

java - 如何在 Java Android Studio 中更改 EditText 的自动焦点?

android - 在 Android Firebase 数据库事务的后台线程中工作

security - 如何将人声转换为数字格式?

android - isRecognitionAvailable 在 android 上返回 false

android - 在 Android 4.0 中访问连续语音识别

Java 谷歌语音识别 API

android - 如果在 CardView 中使用,则标记 LinearLayout