Android版本:使用API​​进行语音到文本的转换

来源:互联网 发布:软件详细设计说明书 编辑:程序博客网 时间:2024/05/16 19:40

Android有一个非常酷的功能,仍然有很多开发人员不知道。像Any.DO应用程序使用语音到文本的转换功能相当创造性。在当今世界上的Siri语音命令是非常重要的。Android的原生提供的语音到文本的功能,那么,为什么不把它用在我们的应用程序!

我会告诉你如何使用Android的语音到文本API在应用程序中。

让我们把我们的演示应用程序。

演示程序

应用程序将非常简单。这将有一个带麦克风符号的按钮。点击其中我们触发Android的语音到文本的意向,显示一个对话框,语音输入。的语音输入,然后转换成文本。在文本视图中的文本,然后显示。

第1步:在Eclipse中建立基本的Andr​​oid项目

创建一个Hello World,Android的Eclipse项目中。转到“新建”>“项目> Android项目为项目作为SpeechToTextDemo和选择Android运行时2.1 SDK 7 我已经给包名net.viralpatel.android.speechtotextdemo

一旦你完成了上述步骤,你将有一个基本的Hello World Android应用程序。

第2步:改变布局

在我们的演示,我们需要简单的布局。只有一个图像按钮来触发语音到文本的API和一个TextView的显示结果从语音文本转换。

开放式布局/在你的Andr​​oid项目的main.xml中,替换现有的内容与以下:

文件:RES /布局/ main.xml中

<LinearLayoutxmlns: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”

packagenet.viralpatel.android.speechtotextdemo;
 
importjava.util.ArrayList;
 
importandroid.app.Activity;
importandroid.content.ActivityNotFoundException;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.speech.RecognizerIntent;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.ImageButton;
importandroid.widget.TextView;
importandroid.widget.Toast;
 
publicclass MainActivity extendsActivity {
 
    protectedstatic final int RESULT_SPEECH = 1;
 
    privateImageButton btnSpeak;
    privateTextView txtText;
 
    @Override
    publicvoid 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(newView.OnClickListener() {
 
            @Override
            publicvoid onClick(View v) {
 
                Intent intent = newIntent(
                        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
    publicboolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        returntrue;
    }
 
    @Override
    protectedvoid onActivityResult(intrequestCode, intresultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
 
        switch(requestCode) {
        caseRESULT_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模拟器或实际设备,看看下面的输出。

Android的语音到文本的API演示

Android的语音到文本的活动

Android的语音到文本的转换

Android的语音文本

原创粉丝点击