Android STT onResult在TTS onUtteranceCompleted之后没有触发

时间:2013-03-30 17:27:53

标签: java android text-to-speech speech-to-text onutterancecompleted

我正在尝试制作一款能为视障人士提供指导的应用。该应用程序将提供说明(使用TTS)并获取用户命令(使用STT)。这是我的MainActivity的代码

//InteractionCompletedEvent is my custom event for callback
public class MainActivity extends Activity implements InteractionCompletedEvent,TextToSpeech.OnUtteranceCompletedListener
{

TTS tts;
STT stt;
Handler mHandler;
int flag;
boolean answer = false;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    stopService(new Intent(MainActivity.this,StartServices.class));
    mHandler = new Handler();

    try
    {
        stt = new STT(this,this);
        tts = new TTS(this,this,this);
        mHandler.postDelayed(new Runnable() {
        public void run() 
        {
            tts.Speak("Welcome to my application");
        }}, 1000);
    }
    catch(Exception e)
    {

    }
}

//event button clicked on "Save Place"
public void savePlaceOnClick(View v)
{
    flag = 1;
    answer = true;
    tts.Speak("Do you want to save this place ?");
}

//event button clicked on "Start Navigation"
public void navigationOnClick(View v)
{
    flag = 2;
    answer = true;
    tts.Speak("Do you want to go to some place ?");
}

//Callback from my STT.java
public void onListeningCompleted() {
    tts.Speak("on listening completed ?");
    if(stt.matching("yes"))
    {
        if(flag == 1)
        {
            tts.Speak("Let's save this place !");
        }
        else if(flag == 2)
        {
            tts.Speak("Let's find place !");
        }
    }
    else if(stt.matching("no"))
    {
        tts.Speak("action canceled");
    }
    else
    {
        tts.Speak("Please repeat your answer");
    }
}

public void onUtteranceCompleted(String utteranceId) {
    if(answer)
    {
        try{
            answer = false;

            stt.start();
        }
        catch(Exception e)
        {
        }
    }
}
}

我的STT.java

public class STT implements RecognitionListener{

SpeechRecognizer speech;
ArrayList<String> data = null;
Intent intent;
InteractionCompletedEvent event;
private boolean dataReady;

public STT(Context con,InteractionCompletedEvent event)
{
    this.event = event;
    speech = SpeechRecognizer.createSpeechRecognizer(con);
    speech.setRecognitionListener(this);
    intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,con.getPackageName());
    intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS,2000);
}

public void start()
{
    data = null;
    final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
    tg.startTone(ToneGenerator.TONE_PROP_BEEP);
    speech.startListening(intent);
}

public void stop()
{
    speech.stopListening();
}


public ArrayList<String> getresult()
{
    return data;
}

public void onResults(Bundle hasil) {
    data = hasil.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
    tg.startTone(ToneGenerator.TONE_PROP_BEEP2);
    event.onListeningCompleted();
}

public boolean resultAvailable() {
    if(data == null) {
        return false;
    }
    else {
        return true;
    }
}
public boolean matching(String match)
{
    for(int i = 0; i< data.size() ; i++)
    {
        if( data.get(i).equalsIgnoreCase(match) )
        {
            return true;
        }
    }
    return false;
}
}

和我的TTS.java

public class TTS implements TextToSpeech.OnInitListener{
private TextToSpeech tts;
Context c;
InteractionCompletedEvent event;
HashMap<String, String> myHashAlarm;
OnUtteranceCompletedListener ouct;

public TTS(Context context, InteractionCompletedEvent event,OnUtteranceCompletedListener ouct)
{
    c = context;
    tts = new TextToSpeech(c, this);
    this.event = event;
    this.ouct = ouct;
}

public void Speak(String words)
{
    Intent intent = new Intent();
    intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);

    myHashAlarm = new HashMap<String, String>();
    myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "utteranceId");
    tts.speak(words, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
}

@Override
public void onInit(int initStatus) {
    if (initStatus == TextToSpeech.SUCCESS) 
    {
       if(tts.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
          tts.setLanguage(Locale.US);

        tts.setOnUtteranceCompletedListener(ouct);
    }
    else
    {
    }
}

public void stop()
{
    tts.stop();
} 
}

没有onUtteranceCompletedListener,它工作得很好(STT onResult被激活)。但在onUtterance完成后,我无法获得任何STT onResult被解雇。

注意:我正在使用onUtteranceCompletedListener(不建议使用),因为我的测试设备是在API级别10(Android 2.3.3)

编辑:TTS onUtteranceCompleted被解雇就好了,问题只是STT onResult无论什么都不会被解雇

2 个答案:

答案 0 :(得分:0)

回答是假的,你从未在任何代码中将其设置为true,因此sst永远不会启动。因此,开始时没有语音识别。

public void onUtteranceCompleted(String utteranceId) {
if(answer) 
{
        **// this if block is never reached.**

        try{
        answer = false;

        stt.start();
    }
    catch(Exception e)
    {
    }
}

}

答案 1 :(得分:0)

好吧我认为我发现了问题,我在Jellybean(4.2.2)上调试了应用程序并且应用程序崩溃了,然后我尝试将已弃用的接口(onUtteranceCompletedListener)更改为onUtteranceProgressListener(仅适用于API lv 15+)它工作得很好,所以这是我的结论:

这个问题可能是由于已弃用的界面(可能是错误)引起的,这可能是界面弃用的原因