要实现Android应用程序的语音/文本调用功能,你可以使用Android的语音识别和文本转语音功能。
首先,在你的Android项目中添加以下权限到AndroidManifest.xml文件中:
然后,创建一个按钮或其他用户界面元素,当用户点击时触发调用操作。
接下来,你可以使用以下代码示例来实现语音输入和文本转语音功能:
import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
private static final int REQ_CODE_SPEECH_INPUT = 100;
private TextToSpeech textToSpeech;
private Button callButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callButton = findViewById(R.id.call_button);
// 请求录音权限
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.RECORD_AUDIO}, 1);
// 初始化TextToSpeech对象
textToSpeech = new TextToSpeech(this, this);
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptSpeechInput();
}
});
}
// 启动语音识别
private void promptSpeechInput() {
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, "说出电话号码或联系人名称");
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "抱歉,您的设备不支持语音识别功能!", Toast.LENGTH_SHORT).show();
}
}
// 获取语音识别结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String spokenText = result.get(0);
makeCall(spokenText);
}
break;
}
}
}
// 拨打电话
private void makeCall(String phoneNumber) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber)));
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(this, "抱歉,您的设备不支持语音转文本功能!", Toast.LENGTH_SHORT).show();
} else {
textToSpeech.speak("请说出电话号码或联系人名称", TextToSpeech.QUEUE_FLUSH, null);
}
} else {
Toast.makeText(this, "抱歉,初始化语音转文本功能失败!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDestroy() {
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
}
这个示例代码中,首先我们在onCreate
方法中初始化了TextToSpeech对象,并在onInit
方法中设置了语音转文本功能。然后,当用户点击按钮时,调用promptSpeechInput
方法启动语音识别功能。在onActivityResult
方法中获取识别结果,并调用makeCall
方法拨打电话。
请注意,这个示例中
上一篇:Android应用程序丢失文件