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

本文介绍了如何在Android应用程序中使用语音到文本API。通过创建一个简单的项目,修改布局以包含一个麦克风按钮,然后在Java代码中触发API来实现语音识别。当用户点击按钮,API将语音输入转换为文本并显示在文本视图上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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中

< 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模拟器或实际设备,看看下面的输出。

Android的语音到文本的API演示

Android的语音到文本的活动

Android的语音到文本的转换

Android的语音文本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值