语音转文字
Android:使用API向文本发音
Android有一个非常酷的功能,许多开发人员不知道。像Any.DO这样的应用程序使用语音来创造性地转换文字。在今天的Siri世界里,声音命令是非常重要的。Android本机提供语音到文本的功能,所以为什么不在我们的应用程序中使用它!
我将向您展示如何在应用程序中使用Android的Speech to Text API。
让我们演示应用程序。
演示应用
应用程序将非常简单。它将有一个带麦克风符号的按钮。点击我们触发Android的语音到文本意图,显示一个对话框来进行语音输入。然后将语音输入转换为文本。然后文本显示在文本视图中。
步骤1:在Eclipse中创建基本的Android项目
在Eclipse中创建一个Hello World Android项目。转到新建>项目> Android项目。将项目名称作为SpeechToTextDemo,并选择Android Runtime 2.1或sdk 7.我给了包名称net.viralpatel.android.speechtotextdemo。
完成上述步骤后,您将拥有一个基本的hello world Android App。
步骤2:更改布局
对于我们的演示,我们需要简单的布局。只需一个图像按钮来触发语音到文本API和一个TextView来显示从语音转换的结果文本。
在您的Android项目中打开layout / main.xml,并将其内容替换为以下内容:
文件:res / layout / 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组织按钮和文本视图。请注意按钮:btnSpeak和文本视图的ID :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(