安卓 - 语音识别和保存音频文件 - 在某些设备上不起作用



我有用于语音识别和保存音频文件的工作解决方案,但代码仅适用于某些设备。

我尝试在少数设备上运行此代码。似乎此解决方案适用于android 5.0,但不适用于更高版本。

我正在使用意图类来做到这一点:

System.out.println("startVoiceInput");
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hello, How can I help you?");
intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
intent.putExtra("android.speech.extra.GET_AUDIO", true);
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
a.printStackTrace();
}

识别本身适用于每个设备(没有这些行:

intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
intent.putExtra("android.speech.extra.GET_AUDIO", true);

)。

但是有了这些行,需要捕获音频流以将其保存在设备上,谷歌弹出窗口不会出现。我没有收到任何错误,但是这个日志:

D/ViewRootImpl@e232f4[RecordingActivity]: ViewPostIme pointer 0
D/ViewRootImpl@e232f4[RecordingActivity]: ViewPostIme pointer 1
I/System.out: startVoiceInput
D/ViewRootImpl@e232f4[RecordingActivity]: MSG_WINDOW_FOCUS_CHANGED 0
I/System.out: onActivityResult
D/ViewRootImpl@e232f4[RecordingActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=true 527892242432} changed=false
MSG_WINDOW_FOCUS_CHANGED 1
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@4dc5d3e nm : com.example.acces ic=null
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus

Google Keep 仍然使用这些秘密参数。您可以按照此处的说明进行检查。所以它应该有效。

我在应用程序中检查了它。如果您的应用程序没有RECORD_AUDIO权限,则不会显示 Google 弹出窗口。如果您向 AndroidManifest 添加RECORD_AUDIO权限.xml则此处描述的解决方案将起作用。

最新更新