Android实现语音识别 && 讯飞平台语音识别语音播报

        Android语音识别,借助于云端技术可以识别用户的语音输入,包括语音控制等技术,下面我们将利用Google提供的Api实现这一功能。功能点为:通过用户语音将用户输入的语音识别出来,并打印在列表上。

       首先建立如下的一个activity,并在button下建立一个listview控件(用来回显语言识别到的项目)。

      代码如下:

package com.example.speechreg;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;   //用于查找本地应用软件包相关的类
import android.content.pm.ResolveInfo;      //从系统IntentFilter返回的信息
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 MainActivity extends Activity implements OnClickListener {
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;   //startActivityForResult操作要求的标识码
    private ListView mList;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice_recognition);
        Button speakButton = (Button) findViewById(R.id.btn_speak);
        mList = (ListView) findViewById(R.id.listView1);    
 
        PackageManager pm = getPackageManager();
        List activities = pm.queryIntentActivities(new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);  //通过全局包管理器及特定intent,查找系统是否有语音识别的服务程序
        if (activities.size() != 0) {
            speakButton.setOnClickListener(this);	//如果存在该activity
        } else {
            speakButton.setEnabled(false);
            speakButton.setText("Recognizer not present");    //否则将BUTTON显示值修改,并设置成不可选
        }
    }
 
    public void onClick(View v) {    //OnClickListener中的要override的函数
        if (v.getId() == R.id.btn_speak) {
            startVoiceRecognitionActivity();
        }
    }
 
    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");    //设置语音识别Intent调用的特定属性参数
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);   启动一个要求有返回值的activity调用
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  //该函数非接口内也非抽象函数,为何会Override?
        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));  //在listview中显示结果
        }
 
        super.onActivityResult(requestCode, resultCode, data);
    }
}

     运行结果如下,它会自动调用谷歌语音识别的服务activity,并返回特定activity执行的结果。

=========================================讯飞平台语音识别语音播报==========================

在网上看到一篇中文语音识别、控制,语音合成的好文章。分享地址http://blog.csdn.net/gao_chun/article/details/46377505



参考原文:http://blog.csdn.net/comkingfly/article/details/7359987

参考原文:http://software.intel.com/zh-cn/articles/developing-android-applications-with-voice-recognition-features/?utm_campaign=CSDN&utm_source=intel.csdn.net&utm_medium=Link&utm_content=intelandroid-yuyinshibie

你可能感兴趣的:(Android)