(Android實戰)語音辨識,根據語音,執行對應操作

來源:互聯網
上載者:User

在iphone4s 發布後,siri語音功能成為了一時成為了熱點,這幾天想寫個類似於siri類似功能的android應用,下面就是關鍵的兩個技術點

 1 語音辨識:

private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

/**
* 開啟語音辨識對話表單
*/
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speech recognition demo");
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

/**
* 處理語音對話方塊返回的識別資訊.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
&& resultCode == RESULT_OK) {
// 擷取反饋的語音辨識數組,並按照匹配度反饋

ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);


}

}

2 根據指令,進行打電話,發簡訊,開啟網站操作:

if (voicekey.type == 1) { //電話

txtview_secretary.setText("正在為你接通電話...");
Thread.sleep(1000);
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("tel://" + voicekey.data));
startActivity(intent);
} else if(voicekey.type == 2){//簡訊

txtview_secretary.setText("正在為你發簡訊...");
Thread.sleep(1000);
Intent intent = new Intent(Intent.ACTION_SENDTO,Uri.parse("sms:"+voicekey.data));
startActivity(intent);
}
else if(voicekey.type == 3) //網址
{
// open kit explore ,navigate to net bank,close App
txtview_secretary.setText("正在為你開啟網站...");
Thread.sleep(1000);
Uri uri = Uri.parse(voicekey.data);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.