原文链接:http://www.javaarch.net/jiagoushi/782.htm


Android 语音输入API使用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" > <ImageButtonandroid: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" /> <TextViewandroid: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.javapackage 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; @Overridepublic 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() { @Overridepublic 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();}}}); } @Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;} @Overrideprotected 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框上。示例代码地址:http://viralpatel-net-tutorials.googlecode.com/files/SpeechToTextDemo.zip


更多相关文章

  1. 第17天android:《android从零开始》视频(1-5)
  2. Android(安卓)EditView
  3. android手机客户端上传文件,java servlet服务器端接收并保存到服
  4. Android下模拟按键输入
  5. [RK3399][Android7.1] 调试笔记 --- 设置搜狗为开机默认输入法
  6. Android(安卓)EditText不弹出软键盘
  7. Android入门学习笔记之人机用户界面
  8. Android(安卓)系统广播Action一览验证
  9. Android(安卓)AOSP输入法(LatinIME)大写判断分析

随机推荐

  1. activity的几种启动模式
  2. Android(安卓)display架构分析-SW架构分
  3. Android保存位图
  4. Android实现再图标右上角显示数字
  5. Android:SQLite数据库
  6. fatal error: libavutil/avconfig.h: No
  7. gradle批量打包Android(安卓)apk:不同的包
  8. android自定义属性三部曲
  9. Android(安卓)NDK之JNI使用例子
  10. Android显示从网络下载图片偏小的问题