文字语音是一种功能,通常意味着用户说的任何内容都会转换为文字。对于用户而言,此功能已成为非常普遍且有用的功能。在各种实现了搜索功能(例如Google搜索)的地方,也可以在诸如Google键盘等应用程序中实现,因为它可以为用户带来出色的体验。因此,在本文中,我们将使用ImageView的OnTouchListener将语音实现为文本 并将输入的文本存储在EditText中。
我们将在本文中构建什么?
在本文中,我们将开发一个示例应用程序,在其MainActivity中具有麦克风的ImageView和EditText,并通过使用ImageView的OnTouchListener,以语音到文本的形式向EditText提供输入。下面给出了一个示例GIF,以使我们对本文中要做的事情有一个了解。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:使用activity_main.xml文件
现在,转到应用程序> res>布局> activity_main.xml ,并将下面编写的代码粘贴到activity_main.xml文件中。
XML
Java
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
// declare editText
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// definition of editText using method findViewById()
editText = findViewById(R.id.edit_text);
// initializing mSpeechRecognizer using SpeechRecognizer class
final SpeechRecognizer mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
final Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
// various methods of RecognitionListener class
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle bundle) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float v) {
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
}
@Override
public void onResults(Bundle bundle) {
// getting all the matches
ArrayList matches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
// displaying the first match
if (matches != null)
editText.setText(matches.get(0));
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override
public void onEvent(int i, Bundle bundle) {
}
});
// set OnTouchListener to imageView named microphone
findViewById(R.id.microphone).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
// case MotionEvent.ACTION_UP run when user will remove
// his/her finger from microphone imageView
case MotionEvent.ACTION_UP:
mSpeechRecognizer.stopListening();
editText.setHint(" ");
break;
// case MotionEvent.ACTION_UP run when user will put his/her
// finger from microphone imageView
case MotionEvent.ACTION_DOWN:
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
editText.setText("");
editText.setHint("");
break;
}
return false;
}
});
}
}
XML
步骤3:使用MainActivity。 Java文件
转到应用> Java >程序包名称> MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
// declare editText
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// definition of editText using method findViewById()
editText = findViewById(R.id.edit_text);
// initializing mSpeechRecognizer using SpeechRecognizer class
final SpeechRecognizer mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
final Intent mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
// various methods of RecognitionListener class
mSpeechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle bundle) {
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onRmsChanged(float v) {
}
@Override
public void onBufferReceived(byte[] bytes) {
}
@Override
public void onEndOfSpeech() {
}
@Override
public void onError(int i) {
}
@Override
public void onResults(Bundle bundle) {
// getting all the matches
ArrayList matches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
// displaying the first match
if (matches != null)
editText.setText(matches.get(0));
}
@Override
public void onPartialResults(Bundle bundle) {
}
@Override
public void onEvent(int i, Bundle bundle) {
}
});
// set OnTouchListener to imageView named microphone
findViewById(R.id.microphone).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
// case MotionEvent.ACTION_UP run when user will remove
// his/her finger from microphone imageView
case MotionEvent.ACTION_UP:
mSpeechRecognizer.stopListening();
editText.setHint(" ");
break;
// case MotionEvent.ACTION_UP run when user will put his/her
// finger from microphone imageView
case MotionEvent.ACTION_DOWN:
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
editText.setText("");
editText.setHint("");
break;
}
return false;
}
});
}
}
步骤4:使用AndroidManifest.xml文件
最后,转到应用程序>清单> AndroidManifest.xml文件,并在其中添加以下编写的权限。
XML格式
仅此而已,现在可以在设备上安装应用程序了。这是应用程序输出的样子。
输出: