欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

轻松实现Android语音识别功能

程序员文章站 2024-03-04 17:23:53
苹果的iphone有语音识别用的是google的技术,做为google力推的android 自然会将其核心技术往android 系统里面植入,并结合google 的云端技术...

苹果的iphone有语音识别用的是google的技术,做为google力推的android 自然会将其核心技术往android 系统里面植入,并结合google 的云端技术将其发扬光大。

所以google voice recognition在android 的实现就变得极其轻松。

语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用google 提供的api 实现这一功能。

功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。

* 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
*
* <a href="http://www.apache.org/licenses/license-2.0" rel="nofollow" target="_blank">http://www.apache.org/licenses/license-2.0</a>
*
* 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.
*/

@override
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);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。