• 语音识别 转载 待测试


    
    
    package com.zhangke.spring.sky.yuyin;
    
    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.content.res.ColorStateList;
    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;
    /**
     * android语言识别功能
     * 特别注意:如果手机有语言设别功能,请开启网络,因为系统会根据你的声音数据到google云端获取声音数据
     * @author spring sky
     * Email vipa1888@163.com
     * QQ:840950105
     * My name :石明政
     *
     */
    public class MainActivity extends Activity implements OnClickListener{
        private Button btn ;
        private ListView listView;
        private static final int REQUEST_CODE = 1;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            btn = (Button) this.findViewById(R.id.btn);
            listView = (ListView) this.findViewById(R.id.listview);
            
            /**
             * 下面是判断当前手机是否支持语音识别功能
             */
            PackageManager pm = getPackageManager();
            List<ResolveInfo> list = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
            if(list.size()!=0)
            {
                btn.setOnClickListener(this);
            }else{
                btn.setEnabled(false);
                btn.setText("当前语音识别设备不可用...");
            }
            
        }
    
        @Override
        public void onClick(View v) {
            if(v.getId()==R.id.btn)
            {
                /**
                 * 启动手机内置的语言识别功能
                 */
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);  //设置为当前手机的语言类型
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "请说话,我识别");//出现语言识别界面上面需要显示的提示
                startActivityForResult(intent,REQUEST_CODE);
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            /**
             * 回调获取从谷歌得到的数据
             */
            if(requestCode==REQUEST_CODE&&resultCode==RESULT_OK)
            {
                
                List<String> list = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list)); //把数据显示在listview中
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
        
        
        
    }
    
    
    
    
    
      layout布局
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="实现语音识别效果"
            android:id="@+id/tvTitle"
        />
        <Button  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="实现语音识别效果"
            android:id="@+id/btn"
        />
          <ListView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:id="@+id/listview"
        />
    </LinearLayout>
  • 相关阅读:
    考研讲座笔记——张雪峰
    【极简版】OpenGL 超级宝典(第五版)环境配置 VS2010
    OpenGL编程指南(第九版) Tiangles 学习笔记
    qt学习笔记
    郑州大学2018新生训练赛第十场题解
    Win10 中将网页转换成pdf的简便方法
    Kali Linux ——在无网络情况下安装无线网卡驱动
    成环的概率dp(初级) zoj 3329
    概率dp的边界处理 POJ 2096
    node连接mysql生成接口,vue通过接口实现数据的增删改查(一)
  • 原文地址:https://www.cnblogs.com/GoAhead/p/2791100.html
Copyright © 2020-2023  润新知