在Android中使用百度语音识别SDK进行语音输入,获取输入的文本内容。 将获取的文本内容分析,识别出对应的姓名和电话信息。用代码呈现出来
时间: 2023-06-19 22:09:08 浏览: 124
以下是使用百度语音识别SDK进行语音输入,并获取输入的文本内容的代码示例:
```java
public class MainActivity extends AppCompatActivity implements IRecogListener {
private static final String TAG = "MainActivity";
private static final int REQUEST_RECORD_AUDIO_PERMISSION_CODE = 1;
private SpeechRecognizer speechRecognizer;
private Button startButton;
private TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = findViewById(R.id.start_button);
resultText = findViewById(R.id.result_text);
// 初始化百度语音识别SDK
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this, new AppIdSupplier(this), new InitListener() {
@Override
public void onInit(int i) {
Log.d(TAG, "SpeechRecognizer onInit: " + i);
}
});
speechRecognizer.setRecognitionListener(this);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 开始语音识别
startVoiceRecognition();
}
});
}
// 开始语音识别
private void startVoiceRecognition() {
// 检查录音权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_RECORD_AUDIO_PERMISSION_CODE);
return;
}
// 配置语音识别参数
final Map<String, Object> params = new HashMap<>();
params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);
params.put(SpeechConstant.VAD_ENDPOINT_TIMEOUT, 0);
params.put(SpeechConstant.PID, 1537);
// 开始识别
speechRecognizer.cancel();
speechRecognizer.start(params);
}
@Override
public void onResults(Bundle bundle) {
// 获取识别结果
ArrayList<String> results = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (results != null && results.size() > 0) {
String text = results.get(0);
resultText.setText("识别结果:" + text);
// 分析识别结果
analyzeResult(text);
}
}
// 分析识别结果
private void analyzeResult(String text) {
// 正则表达式匹配姓名和电话号码
Pattern namePattern = Pattern.compile("[\u4e00-\u9fa5]+");
Matcher nameMatcher = namePattern.matcher(text);
String name = "";
if (nameMatcher.find()) {
name = nameMatcher.group();
}
Pattern phonePattern = Pattern.compile("1\\d{10}");
Matcher phoneMatcher = phonePattern.matcher(text);
String phone = "";
if (phoneMatcher.find()) {
phone = phoneMatcher.group();
}
// 输出结果
Log.d(TAG, "姓名:" + name);
Log.d(TAG, "电话:" + phone);
}
@Override
public void onError(SpeechError speechError) {
Log.e(TAG, "SpeechRecognizer onError: " + speechError);
}
@Override
public void onReadyForSpeech(Bundle bundle) {
Log.d(TAG, "SpeechRecognizer onReadyForSpeech");
}
@Override
public void onBeginningOfSpeech() {
Log.d(TAG, "SpeechRecognizer onBeginningOfSpeech");
}
@Override
public void onEndOfSpeech() {
Log.d(TAG, "SpeechRecognizer onEndOfSpeech");
}
@Override
public void onEvent(int i, Bundle bundle) {
Log.d(TAG, "SpeechRecognizer onEvent: " + i);
}
@Override
public void onPartialResults(Bundle bundle) {
Log.d(TAG, "SpeechRecognizer onPartialResults");
}
@Override
public void onRmsChanged(float v) {
Log.d(TAG, "SpeechRecognizer onRmsChanged: " + v);
}
// 处理录音权限请求结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_RECORD_AUDIO_PERMISSION_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startVoiceRecognition();
} else {
Toast.makeText(this, "录音权限被拒绝", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
```
在代码中,我们使用了正则表达式匹配识别结果中的姓名和电话号码,并输出结果。你可以根据你自己的需求,修改代码中的正则表达式和输出方式。
阅读全文