0

我正在尝试制作一个应用程序,当使用 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);
}
4

2 回答 2

0

I have an app that do what you want, it works fine on 2.3 to 4.0. On jelly Bean does not work. Google made changes and looks like Jelly Bean stop the voice recognition during the phone ringing.

So if you are testing on Jelly bean, try your code on another device.

于 2013-04-26T16:05:26.917 回答
0

问题是您根据文档将FLAG_ACTIVITY_NEW_TASK添加到您的意图中:

当调用者从正在启动的活动中请求结果时,不能使用此标志。

并且您正在使用它startActivityForResult,删除

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

从您的代码中解决了这个问题。

于 2013-03-30T11:07:48.780 回答