- Android 语音输入API使用 转载http://www.javaarch.net/jiagoushi/782.htm
- Android已经支持语音输入的API了,不过不知道中文输入识别效果怎么样。这里给一个怎么使用语音输入的示例
- 首先在android工程中的页面布局文件中res/layout/main.xml添加一个button和text
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_above="@+id/textView1"
- android:layout_toLeftOf="@+id/textView1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageButton
- android:id="@+id/btnSpeak"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="10dp"
- android:contentDescription="@string/speak"
- android:src="@android:drawable/ic_btn_speak_now" />
- <TextView
- android:id="@+id/txtText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="10dp"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- </LinearLayout>
- active类SpeechToTextDemoActivity.java
- package net.viralpatel.android.speechtotextdemo;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.ActivityNotFoundException;
- import android.content.Intent;
- import android.os.Bundle;
- import android.speech.RecognizerIntent;
- import android.view.Menu;
- import android.view.View;
- import android.widget.ImageButton;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- protected static final int RESULT_SPEECH = 1;
- private ImageButton btnSpeak;
- private TextView txtText;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txtText = (TextView) findViewById(R.id.txtText);
- btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
- btnSpeak.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(
- RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
- try {
- startActivityForResult(intent, RESULT_SPEECH);
- txtText.setText("");
- } catch (ActivityNotFoundException a) {
- Toast t = Toast.makeText(getApplicationContext(),
- "Opps! Your device doesn't support Speech to Text",
- Toast.LENGTH_SHORT);
- t.show();
- }
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- switch (requestCode) {
- case RESULT_SPEECH: {
- if (resultCode == RESULT_OK && null != data) {
- ArrayList<String> text = data
- .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
- txtText.setText(text.get(0));
- }
- break;
- }
- }
- }
- }
- 这里android.speech是Android语音输入的核心包,android.speech.RecognizerIntent是一个主要的类,这个active会弹出一个语音输入对话框,然后接收语音输入,识别语音内容转为文本,但我们启动语音输入active后,需要通过startActivityForResult()方法接收文本结果。在.putExtra()方法还需要输入RecognizerIntent.EXTRA_LANGUAGE_MODE语言类型,这里是en-US。
- 我们通过覆盖onActivityResult(int requestCode, int resultCode, Intent data)方法来处理结果数据,通过data获取key为RecognizerIntent.EXTRA_RESULTS来接收文本内容list,然后设置到text框上。
Android 语音输入API使用
最新推荐文章于 2024-01-28 10:23:01 发布