语音转文字

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(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。这里我们将其价值设定为美国。

由于我们通过startActivityForResult()触发了RecognizerIntent,所以我们重写方法onActivityResult(int requestCode, int resultCode, Intent data)来处理结果数据。该
RecognizerIntent将转换语音输入文本和发回的结果作为ArraList用关键的RecognizerIntent.EXTRA_RESULTS。一般来说,这个列表应该按照语音识别器的置信度降序排列。仅在活动结果中返回RESULT_OK时才显示。我们只是txtText使用文本视图设置我们得到的结果文本txtText.setText()

值得注意的一件事是如何处理不支持语音到文本API的设备/ android版本。在这种情况下,当我们尝试启动活动时,将抛出ActivityNotFoundException异常。在上面的例子中,我们捕获了这个异常并显示了一个消息“Opps!您的设备不支持使用Toast语音到文本。

Android应用程式的萤幕撷取画面

就这样!只需在Android模拟器或实际设备中执行应用程序,并查看以下输出。

Android的语音到文本-API演示

Android的语音到文本的活动

Android的语音到文本的转换

Android的语音文本

下载源代码

Android_SpeechToTextDemo.zip(350 KB)

参考

RecognizerIntent.html#ACTION_RECOGNIZE_SPEECH文档

 

posted on 2017-07-08 01:24  信假名如  阅读(569)  评论(0)    收藏  举报

导航