在本文中,语音转文本功能是在Android的应用程序中实现的。文字语音意味着用户所说的任何内容都将转换为文字。对于用户而言,此功能已成为非常普遍且有用的功能。在各种实现了搜索功能(例如Google搜索)的地方,也可以在诸如Google键盘等应用程序中实现,因为它可以为用户带来出色的体验。
方法:
步骤1:在activity_main.xml中添加以下代码。这里为麦克风图标和一个TextView表明从语音被添加转换的文本的示意图。
activity_main.xml
MainActivity.java
class MainActivity extends AppCompatActivity(){
private ImageView iv_mic;
private TextView tv_Speech_to_text;
private static final int REQUEST_CODE_SPEECH_INPUT = 1;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_mic = findViewById(R.id.iv_mic);
tv_Speech_to_text = findViewById(R.id.tv_speech_to_text);
iv_mic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent
= new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text");
try {
startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT);
}
catch (Exception e) {
Toast
.makeText(MainActivity.this, " " + e.getMessage(),
Toast.LENGTH_SHORT)
.show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SPEECH_INPUT) {
if (resultCode == RESULT_OK && data != null) {
ArrayList result = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
tv_Speech_to_text.setText(
Objects.requireNonNull(result).get(0));
}
}
}
}
步骤2:在MainActivity中添加以下代码。 Java的。在这里,onClickListener被添加了麦克风图标,因此当用户单击麦克风的图标(图像)时,它将被调用。侦听器中使用RecognizerIntent.ACTION_RECOGNIZE_SPEECH来启动一个活动,该活动提示用户语音并通过语音识别器发送。当使用startActivityForResult()启动意图时,结果将通过onActivityResult()方法中的活动结果返回。在onActivityResult()方法中,返回字符串列表,并在textview中将其替换为文本。
主要活动。Java
class MainActivity extends AppCompatActivity(){
private ImageView iv_mic;
private TextView tv_Speech_to_text;
private static final int REQUEST_CODE_SPEECH_INPUT = 1;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_mic = findViewById(R.id.iv_mic);
tv_Speech_to_text = findViewById(R.id.tv_speech_to_text);
iv_mic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent
= new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,
Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to text");
try {
startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT);
}
catch (Exception e) {
Toast
.makeText(MainActivity.this, " " + e.getMessage(),
Toast.LENGTH_SHORT)
.show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SPEECH_INPUT) {
if (resultCode == RESULT_OK && data != null) {
ArrayList result = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
tv_Speech_to_text.setText(
Objects.requireNonNull(result).get(0));
}
}
}
}
输出: