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);
}