[英]Speech to text API in android
我有錄制的語音,我有一個任務,可以在不使用互聯網的情況下將錄制的語音轉換為文本。
我如何做到這一點,我嘗試如下:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
startActivityForResult(intent, REQUEST_CODE);
但是我沒有得到,如何發送記錄的文件並獲得文本作為結果。
RecognizerIntent僅適用於不適合您的情況,可以通過Internet連接進行實時語音輸入。
有一個答案在這里可能進一步幫助。
android中有一個可用的RecognizerIntent。
步驟1:啟動RecognizerIntent首先,我們需要通過設置必要的標記(例如ACTION_RECOGNIZE_SPEECH)來創建RecognizerIntent-簡單地接受用戶的語音輸入並將其返回到相同的活動LANGUAGE_MODEL_FREE_FORM-考慮以自由格式輸入英語EXTRA_PROMPT-在詢問時向用戶顯示文本提示他們說話
第2步:接收語音響應一旦完成語音輸入,此意圖便會在OnActivityResult中返回所有可能的結果。通常,我們可以將第一個結果視為最准確,並采取任何可能的措施。
請參考以下代碼段和鏈接以獲取完整參考。
鏈接: http : //www.androidhive.info/2014/07/android-speech-to-text-tutorial/
/**
* Showing google speech input dialog
* */
private void promptSpeechInput() {
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,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
/**
* Receiving speech input
* */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.