在Android开发中,实现语音转换成文字(Speech-to-Text, STT)通常依赖于Google提供的SpeechRecognizer类,它是Android Speech Recognition API的一部分。以下是一个简单的示例代码,展示了如何在Android应用中实现语音转换成文字的功能。
首先,你需要在你的AndroidManifest.xml文件中添加必要的权限,以便应用能够访问麦克风:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
然后,你可以在你的Activity中实现语音转换功能。以下是一个基本的示例:
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.TtsUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;
public class SpeechToTextActivity extends AppCompatActivity {
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
private static final int REQUEST_CODE_SPEECH_INPUT = 100;
private TextView textViewResult;
private SpeechRecognizer speechRecognizer;
private Intent recognizerIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speech_to_text);
textViewResult = findViewById(R.id.textViewResult);
Button buttonSpeak = findViewById(R.id.buttonSpeak);
// 初始化SpeechRecognizer和Intent
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
// 设置按钮点击事件监听器
buttonSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
askForAudioPermissionAndStartRecognition();
}
});
}
private void askForAudioPermissionAndStartRecognition() {
// 检查录音权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_RECORD_AUDIO_PERMISSION);
} else {
startSpeechRecognition();
}
}
private void startSpeechRecognition() {
speechRecognizer.startListening(recognizerIntent);
}
// 处理权限请求结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_RECORD_AUDIO_PERMISSION) {
if (grantResults.length > 0 && grantResults == PackageManager.PERMISSION_GRANTED) {
startSpeechRecognition();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
// 处理语音识别结果
@Override
protected void onActivityResult(int requestCode, int resultCode, @NonNull Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SPEECH_INPUT && resultCode == RESULT_OK && data != null) {
List<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (result != null && !result.isEmpty()) {
textViewResult.setText(result.get(0));
} else {
textViewResult.setText("No speech recognized");
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (speechRecognizer != null) {
speechRecognizer.destroy();
}
}
}
在这个示例中,我们创建了一个简单的Activity,其中包含一个按钮和一个TextView来显示识别结果。当用户点击按钮时,应用会检查录音权限,如果权限已授予,则开始语音识别。识别结果会通过onActivityResult方法返回,并显示在TextView中。
请确保你的布局文件activity_speech_to_text.xml包含了一个TextView和一个Button,它们的ID分别是textViewResult和buttonSpeak。
此外,由于语音识别功能依赖于设备的麦克风,因此在实际应用中,你还需要处理用户可能拒绝授予录音权限的情况,并适当地向用户解释为什么需要这个权限。
这个示例使用了英语(美国)作为识别语言。如果你想要支持其他语言,可以修改recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");这行代码中的语言代码。