RecognizerIntent(语音识别)

过程如下:

1、启动语音识别 Activity

2、这里处理语音(传到 google 服务器处理)

3、结果以 Acitivity 的结果返回(onActivityResult)

主要用到的类为 android.speech.RecognizerIntent

package com.linc;  

    import java.util.ArrayList;  
    import java.util.List;  

    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.Button;  
    import android.widget.TextView;  

    public class VoiceRecognitionDemoActivity extends Activity {  
        private static final String TAG = "VoiceRecognition";  
        private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;  

        private TextView textView;  
        private Button button;  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  

            initWidget();  

            // Check to see if a recognition activity is present  
            PackageManager pm = getPackageManager();  
            List<ResolveInfo> activities = pm.queryIntentActivities(  
                    new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);  
            if (activities.size() != 0) {  
                button.setOnClickListener(new OnClickListener() {  
                    @Override  
                    public void onClick(View v) {  
                        startVoiceRecognitionActivity();  
                    }  
                });  
            } else {  
                button.setEnabled(false);  
                button.setText("Recognizer not present");  
            }  
        }  

        private void initWidget()  
        {  
            textView = (TextView)findViewById(R.id.tv);  
            button = (Button)findViewById(R.id.btn);  
        }  

        /** 
         * Fire an intent to start the speech recognition activity. 
         */  
        private void startVoiceRecognitionActivity() {  
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  

            // Display an hint to the user about what he should say.  
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "请说标准普通话");//注意不要硬编码  

            // Given an hint to the recognizer about what the user is going to say  
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  

            // Specify how many results you want to receive. The results will be sorted  
            // where the first result is the one with   higher confidence.  
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);//通常情况下,第一个结果是最准确的。  

            startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);  
        }  

        @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<String> matches = data.getStringArrayListExtra(  
                        RecognizerIntent.EXTRA_RESULTS);  
                StringBuilder stringBuilder = new StringBuilder();  
                int Size = matches.size();   
                for(int i=0;i<Size;++i)  
                {  
                    stringBuilder.append(matches.get(i));  
                    stringBuilder.append("\n");  
                }  
                textView.setText(stringBuilder);  
            }  

            super.onActivityResult(requestCode, resultCode, data);  
       }  
    }  

最后说明:在国内是使用必须要使用手机VPN翻墙,否则无法使用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值