码迷,mamicode.com
首页 > 其他好文 > 详细

教你实现语音识别(基于科大讯飞)

时间:2014-09-23 20:11:15      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:android   blog   http   color   io   os   java   ar   strong   

1),首先在官网下载相关jar包

2)自定义界面设计:

R.layout.activity_main.xml
bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请先到餐厅预定选择店铺"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+searchdialog/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/transparent"
            android:padding="10dp"
            android:src="@android:drawable/ic_btn_speak_now" />

        <EditText
            android:id="@+searchdialog/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:padding="10dp" >

            <requestFocus />
        </EditText>
    </LinearLayout>

</LinearLayout>
bubuko.com,布布扣

main.xml

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/content" />

</LinearLayout>
bubuko.com,布布扣

 

2) 逻辑实现

 首先添加相关jar包以及so文件,如下

bubuko.com,布布扣

权限添加:

 

    <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" />

代码实现:

bubuko.com,布布扣
package com.wangfubin.xunfenyuyin;



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.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText setedit;
    private ImageButton yuyinButton;
    private Button button1;
    private String textString = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                LinearLayout LinearLayout1 = (LinearLayout) getLayoutInflater()
                        .inflate(R.layout.activity_main, null);
                setedit = (EditText) LinearLayout1.findViewById(R.searchdialog.editText1);
                yuyinButton = (ImageButton) LinearLayout1.findViewById(R.searchdialog.imageButton1);
                new AlertDialog.Builder(MainActivity.this)
                .setTitle("请输入查询关键字")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setView(LinearLayout1)
                .setPositiveButton("取消", null)
                .setNegativeButton("确定",
                        new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0,
                            int arg1) {
                        /*// 数据获取
                        searchtext(setedit.getText().toString());*/
                        Toast.makeText(MainActivity.this, setedit.getText().toString(), Toast.LENGTH_LONG).show();
                    }
                }).show();
                yuyinButton.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        showDialog(1);
                    }
                });

            }
        });

    }
    /**
     * 语音查询
     */
    @Override
    protected Dialog onCreateDialog(int id) {
        RecognizerDialog recognizerDialog = new RecognizerDialog(
                MainActivity.this, "appid=5132fe14");// 这里应该写从科大讯飞申请到的appid
        recognizerDialog.setEngine("sms", null, null);
        recognizerDialog.setListener(new RecognizerDialogListener() {
            @Override
            public void onResults(ArrayList<RecognizerResult> results,
                    boolean arg1) {
                for (int i = 0; i < results.size(); i++) {
                    textString += results.get(i).text;
                }
            }

            @Override
            public void onEnd(SpeechError arg0) {
                // Toast.makeText(MainActivity.this, textString, 1).show();
                setedit.setText(textString.substring(0, textString.length() - 1));
textString =""; } }); return recognizerDialog; } }
bubuko.com,布布扣

教你实现语音识别(基于科大讯飞)

标签:android   blog   http   color   io   os   java   ar   strong   

原文地址:http://www.cnblogs.com/dongweiq/p/3988819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!