Android有一个非常酷的功能,仍然有很多开发人员不知道。像Any.DO应用程序使用语音到文本的转换功能相当创造性。在当今世界上的Siri语音命令是非常重要的。Android的原生提供的语音到文本的功能,那么,为什么不把它用在我们的应用程序!
我会告诉你如何使用Android的语音到文本API在应用程序中。
让我们把我们的演示应用程序。
演示程序
应用程序将非常简单。这将有一个带麦克风符号的按钮。点击其中我们触发Android的语音到文本的意向,显示一个对话框,语音输入。的语音输入,然后转换成文本。在文本视图中的文本,然后显示。
第1步:在Eclipse中建立基本的Android项目
创建一个Hello World,Android的Eclipse项目中。转到“新建”>“项目> Android项目。为项目作为SpeechToTextDemo和选择Android运行时2.1 SDK 7 名。我已经给包名net.viralpatel.android.speechtotextdemo
。
一旦你完成了上述步骤,你将有一个基本的Hello World Android应用程序。
第2步:改变布局
在我们的演示,我们需要简单的布局。只有一个图像按钮来触发语音到文本的API和一个TextView的显示结果从语音文本转换。
开放式布局/在你的Android项目的main.xml中,替换现有的内容与以下:
文件:RES /布局/ main.xml中
<
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
>
|
UI是非常简单的。一个LinearLayout中组织的按钮和文本视图。注意id为的按钮:btnSpeak
和文本视图:txtText
在我们的Java代码中,我们将使用。
第3步:Android的Java代码来触发语音到文本API
的开放SpeechToTextDemoActivity类并更换代码以下。
文件“: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 API的心脏是包android.speech
和专类android.speech.RecognizerIntent
。基本上,我们触发一个的意向书(android.speech.RecognizerIntent)显示对话框来识别语音输入。本次活动将语音转换成文本并发送备份到我们的呼叫活动的结果。当我们调用android.speech.RecognizerIntent意图,我们必须使用startActivityForResult(),
因为我们必须倾听结果文本。
注意,在上面的代码中,我们箱意图android.speech.RecognizerIntent的触发它。此外,我们添加一个额外的参数使用。putExtra()
方法。当的调用RecognizerIntent,我们必须提供额外的RecognizerIntent.EXTRA_LANGUAGE_MODE。在这里,我们将其值设置EN-US。
由于我们通过startActivityForResult()触发的RecognizerIntent的,我们覆盖的方法onActivityResult(requestCode,resultCode为,意向数据),
处理结果数据。站长百科 http://www.software8.co
RecognizerIntent将文本转换成语音输入并发送回的结果,ArraList关键RecognizerIntent.EXTRA_RESULTS。一般来说,这个名单的,要责令降序语音识别的信心。目前唯一RESULT_OK,则返回时,在一个活动的结果。我们刚才设置的文本,我们得到了在结果在文本视图txtText的
使用txtText.setText()
。
这里值得注意的一件事是如何处理装置/ Android的版本不支持语音到文本的API。在这种情况下,将抛出异常ActivityNotFoundException,当我们试图启动活动。在上面的例子中,我们已经逮住了这个异常,并显示消息“哎呀!您的手机不支持语音到文本“吐司。
Android应用程序的屏幕截图
而这一切!只要执行应用程序在Android模拟器或实际设备,看看下面的输出。