服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #

Android语音识别演示代码

时间:2016-03-13 00:51来源:未知 作者:最模板 点击:
Android语音识别演示代码public class VoiceActivity extends Activity implements OnClickListener { private static final int CODE = 11; private Button voice_btn; private ListView voice_lv; private ListResolveInfo resList; private ArrayA
Android语音识别演示代码
public class VoiceActivity extends Activity implements OnClickListener {
    private static final int CODE = 11;
    private Button voice_btn;
    private ListView voice_lv;
    private List<ResolveInfo> resList;
    private ArrayAdapter<String> mAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice_layout);
        initPackages();
        initViewsAndEvents();
    }

    private void initPackages() {
        PackageManager pm = getPackageManager();
        Intent resIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        resList = pm.queryIntentActivities(resIntent, 0);
    }

    private void initViewsAndEvents() {
        this.voice_btn = (Button) findViewById(R.id.voice_btn);
        this.voice_lv = (ListView) findViewById(R.id.voice_lv);
        this.voice_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        if (null == resList || resList.size() == 0) {
            Toast.makeText(this, "暂无应用支持该功能", Toast.LENGTH_SHORT).show();
            return;
        }
        voiceRecognize();

    };

    private void voiceRecognize() {
        // 用Intent来传递语音识别的模式,并且开启语音模式
        Intent in = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        // 语言模式和自由形式的语音识别
        in.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        // 提示语言开始
        in.putExtra(RecognizerIntent.EXTRA_PROMPT, "Start Speaking...");
        startActivityForResult(in, CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_OK && requestCode == CODE) {
            // 取得语音的字符
            ArrayList<String> result = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            // ListView显示数据
            mAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, result);
            voice_lv.setAdapter(mAdapter);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/voice_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="left|center"
        android:text="点击下面按钮开始声音识别"
        android:textSize="15sp" />

    <Button
        android:id="@+id/voice_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="开始"
        android:textSize="16sp" />

    <ListView
        android:id="@+id/voice_lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#dedede"
        android:dividerHeight="1px"
        android:scrollbars="none" >
    </ListView>

</LinearLayout>
(责任编辑:最模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容