An example of Speech Recognition

Source: Internet
Author: User

I. Summary

I completed the example in the SDK yesterday and tried to write one by myself according to the document. However, it would be a miracle to directly use the code in the document to call something out.

So it took some time to complete the speech dictation example. In this example, you can click the bottom button to start talking. After that, the textview on the screen will display what you just said.



To be honest, the document is really poorly written, the interface is not detailed, the demo is not described, and there are few notes... alas.

2. Preparations

First of all, create an android project and add a library as described in the document.

1. Select a job in eclipse and introduce MSC. Jar through project> Properties> JAVA buildpath> libraries> Add jars or add external jars;

2. Copy the libs folder under SDK. \ Lib to the project root directory to ensure that the. \ libs \ armeabi \ libmsc. So file exists.

3. Add the following permissions to the androidmanifest. xml file of the project:
<Uses-Permission Android: Name = "android. Permission. record_audio"/>
<Uses-Permission Android: Name = "android. Permission. Internet"/>
<Uses-Permission Android: Name = "android. Permission. access_network_state"/>
<Uses-Permission Android: Name = "android. Permission. access_wifi_state"/>
<Uses-Permission Android: Name = "android. Permission. change_network_state"/>
<Uses-Permission Android: Name = "android. Permission. read_phone_state"/>


Iii. Main Code:

package com.example.meclisener;import java.util.ArrayList;import com.iflytek.speech.RecognizerResult;import com.iflytek.speech.SpeechError;import com.iflytek.ui.RecognizerDialog;import com.iflytek.ui.RecognizerDialogListener;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements RecognizerDialogListener {private RecognizerDialog isrDialog;private TextView myTextView;private String text;private Button myButton;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myTextView=(TextView)findViewById(R.id.myTextView);myButton=(Button)findViewById(R.id.StartButton);myButton.setOnClickListener(new ButtonListener());}@Overrideprotected void onStart() {super.onStart();text="";
// Appid must apply for isrdialog = new recognizerdialog (this, "appid = XXXXXXXX"); isrdialog. setengine ("SMS", null, null); isrdialog. setlistener (this) ;}@ overridepublic void onresults (arraylist <recognizerresult> results, booleanislast) {text + = results. get (0 ). text; system. out. println ("onresult"); toast. maketext (getapplicationcontext (), "you say" + text, toast. length_short ). show () ;}@ overridepublic void onend (speecherror E Rror) {If (error! = NULL) system. out. println ("error"); mytextview. settext (text);} public void showisrdialog () {}@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true;} class buttonlistener implements onclicklistener {@ overridepublic void onclick (view v) {// todo auto-generated method stubisrdialog. show ();}}}

Layout File

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >a    <TextView        android:id="@+id/myTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="20dp"        android:text="@string/hello_world"        android:textSize="20dp"        tools:context=".MainActivity" />   <LinearLayout        android:id="@+id/bottom"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:orientation="horizontal" >        <Button            android:id="@+id/StartButton"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="2dip"            android:layout_weight="1"            android:text="Speak"/>    </LinearLayout></RelativeLayout>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.