蘋果的iphone 有語音識别用的是google 的技術,做為google 力推的android 自然會将其核心技術往android 系統裡面植入,并結合google 的雲端技術将其發揚光大。
是以google voice recognition在android 的實作就變得極其輕松。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnauETL0FDOxITMzdXZu9CXyEzLcN3dl5GMxAjMvw1c3VmbvwVbvNmLtVncvZWa0NmL3d3dvw1LcpDc0RHaiojIsJye.jpg)
語音識别,借助于雲端技術可以識别使用者的語音輸入,包括語音控制等技術,下面我們将利用google 提供的api 實作這一功能。
功能點為:通過使用者語音将使用者輸入的語音識别出來,并列印在清單上。
功能界面如下:
使用者通過點選speak按鈕顯示界面:
使用者說完話後,将送出到雲端搜尋:
在雲端搜尋完成後,傳回列印資料:
java代碼
* copyright (c) 2008 the android open source project
*
* licensed under the apache license, version 2.0 (the "license");
* you may not use this file except in compliance with the license.
* you may obtain a copy of the license at
* http://www.apache.org/licenses/license-2.0
* unless required by applicable law or agreed to in writing, software
* distributed under the license is distributed on an "as is" basis,
* without warranties or conditions of any kind, either express or implied.
* see the license for the specific language governing permissions and
* limitations under the license.
*/
package com.example.android.apis.app;
import com.example.android.apis.r;
import android.app.activity;
import android.content.intent;
import android.content.pm.packagemanager;
import android.content.pm.resolveinfo;
import android.os.bundle;
import android.speech.recognizerintent;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.arrayadapter;
import android.widget.button;
import android.widget.listview;
import java.util.arraylist;
import java.util.list;
/**
* sample code that invokes the speech recognition intent api.
*/
public class voicerecognition extends activity implements onclicklistener {
private static final int voice_recognition_request_code = 1234;
private listview mlist;
* called with the activity is first created.
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
// inflate our ui from its xml layout description.
setcontentview(r.layout.voice_recognition);
// get display items for later interaction
button speakbutton = (button) findviewbyid(r.id.btn_speak);
mlist = (listview) findviewbyid(r.id.list);
// check to see if a recognition activity is present
packagemanager pm = getpackagemanager();
list activities = pm.queryintentactivities(
new intent(recognizerintent.action_recognize_speech), 0);
if (activities.size() != 0) {
speakbutton.setonclicklistener(this);
} else {
speakbutton.setenabled(false);
speakbutton.settext("recognizer not present");
}
* handle the click on the start recognition button.
public void onclick(view v) {
if (v.getid() == r.id.btn_speak) {
startvoicerecognitionactivity();
* fire an intent to start the speech recognition activity.
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);
* handle the results from the recognition activity.
protected void onactivityresult(int requestcode, int resultcode, intent data) {
if (requestcode == voice_recognition_request_code && resultcode == result_ok) {
// fill the list view with the strings the recognizer thought it could have heard
arraylist matches = data.getstringarraylistextra(
recognizerintent.extra_results);
mlist.setadapter(new arrayadapter(this, android.r.layout.simple_list_item_1,
matches));
super.onactivityresult(requestcode, resultcode, data);
}