我正在尝试制作一个应用程序,当使用 android tts 来电时,它会说出呼叫者的联系人姓名(你想参加吗?)。根据用户的回复,呼叫应该参加或结束。我总是在这里遇到语音识别问题(显示未知问题),我在这里给出我的代码,任何人都可以帮助我解决这个问题。从最近几天开始我就对此感到震惊,任何帮助都会很明显..
public class myPhoneStateChangeListener extends PhoneStateListener
{
speechcontact clsspcntct = new speechcontact();
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if (state == TelephonyManager.CALL_STATE_RINGING)
{
String phoneNumber = incomingNumber;
String ContactName = objUtility.getContactName2(context,phoneNumber);
mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
mAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
speakWords(ContactName);
}
if(state == TelephonyManager.CALL_STATE_IDLE && ph_state==1)
{
mAudioManager.setStreamMute(AudioManager.STREAM_RING, false);
}
}
@Override
public void onInit(int initStatus)
{
if (initStatus == TextToSpeech.SUCCESS) {
if(myTTS.isLanguageAvailable(Locale.ENGLISH)==TextToSpeech.LANG_AVAILABLE)
myTTS.setLanguage(Locale.ENGLISH);
}
else if (initStatus == TextToSpeech.ERROR) {
Log.d("speech log", "Sorry! Text To Speech failed...");
}
}
public void speakWords(String speech)
{
myTTS.speak("you have call from"+speech+"do you want to attend",TextToSpeech.QUEUE_FLUSH, null);
new Timer().schedule(new TimerTask() {
@Override
public void run()
{
startVoiceRecognitionActivity();
}
}, 5000);
}
public void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
try
{
startActivityForResult(intent, REQUEST_CODE);
}
catch (ActivityNotFoundException a)
{
Toast.makeText(getApplicationContext(),"Ops! Your device doesn't support Speech to Text",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
String spch = data.getStringExtra(RecognizerIntent.EXTRA_RESULTS);
if (spch.contains("Yes"))
{
// do smthing
}
else if(spch.contains("No"))
{
// do smething
}
}
super.onActivityResult(requestCode, resultCode, data);
}