Android Speech broadcast, Background broadcast, speech recognition, and android Speech Recognition
Android Voice broadcast, Background broadcast, and Speech Recognition
This article describes how to use xunfei voice to implement Speech broadcast and speech recognition functions.
Xunfei Open Platform: http://www.xfyun.cn/index.php/default/index
Program:
Simple XML Layout
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <EditText android: id = "@ + id/et" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: editable = "true"/> <Button android: id = "@ + id/bt_recognize" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "identification"/> <Button android: id = "@ + id/bt_speek" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "Speek"/> <Button android: id = "@ + id/bt_speek_bg" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: gravity = "center" android: text = "background Speek"/> <TextView android: textColor = "@ android: color/white" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "voice control switch"/> <ToggleButton android: id = "@ + id/tb" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> </LinearLayout>
Recognition
Case R. id. bt_recognize: // This is the language recognition part. The most important thing is to instantiate a // RecognizerDialog and enter the appid you applied on the official website, invalid id cannot identify RecognizerDialog isrDialog = new RecognizerDialog (Voice1Activity. this, APPID);/** the setting engine currently supports five types of "sms": normal text transcription "poi": Place Name Search "vsearch": popular word search * "video ": video Music Search "asr": Command word recognition */isrDialog. setEngine ("sms", null, null); isrDialog. setListener (recoListener); isrdiener. show (); break;
// Language recognition listener. There are two methods: RecognizerDialogListener recoListener = new RecognizerDialogListener () {@ Override public void onResults (ArrayList <RecognizerResult> results, boolean isLast) {// a new ToggleButton tb is added. First, check whether the tb is pressed. if it is pressed, the language is controlled. if it is not pressed, the text is recognized if (tb. the isChecked () {// doVoice method is used to identify doVoice (results);} else {// The set is returned after the server completes recognition, here we will only get the most matched text + = results. get (0 ). text; System. out. println (text );}}// First, iterate the results, obtain each result, and compare the results. If there is a specific string, execute the corresponding Intent jump. // Pay attention to all Intent functions (send an email, jump to an installed application, dial-up, send a text message, send a multimedia message, browse the webpage, and play multimedia. Private void doVoice (ArrayList <RecognizerResult> results) {Intent I = new Intent (); for (RecognizerResult result: results) {if (result. text. contains ("Weather") {// weather page Jump I. setClass (Voice1Activity. this, Weather. class); startActivity (I);} else if (result. text. contains ("news") {// jump to the press I. setClass (Voice1Activity. this, News. class); startActivity (I);} else if (result. text. contains ("SMS") {// jump to the text message interface I. setAction (Intent. ACTION_VIEW); I. setType ("vnd. android-dir/mms-sms "); startActivity (I);} else {// if there is no corresponding command, use Toast to prompt the user Toast. makeText (Voice1Activity. this, "Unrecognized", Toast. LENGTH_SHORT ). show () ;}}@ Override public void onEnd (SpeechError error) {if (error = null) {// The result is displayed on the EditText et. setText (text );}}};
Speek
Case R. id. bt_speek: // This is the Language Synthesis part. You also need to instantiate a SynthesizerDialog and enter appid SynthesizerDialog syn = new SynthesizerDialog (Voice1Activity. this, APPID); syn. setListener (new SynthesizerDialogListener () {@ Override public void onEnd (SpeechError arg0) {}}); // implement speech synthesis syn based on the content in EditText. setText (et. getText (). toString (), null); syn. show (); break;
Backend Speek
Case R. id. bt_speek_bg: // This is the background reading. instantiate a SynthesizerPlayer player = SynthesizerPlayer. createSynthesizerPlayer (Voice1Activity. this, APPID); // you can set the voice reader to read aloud for both men and women as needed. For details, see the api documentation and the official forum player. setVoiceName ("vivixiaoyan"); // select a person who wants to set a voice broadcast here, for example, vivixiaoyan, vivixiaomei, and vivixiaoqi player. playText (et. getText (). toString (), "ent = vivi21, bft = 5", null); break;
Source code download: http://download.csdn.net/download/gao_chun/8775975
Reprinted please note.