Pergunta

I'm trying to make an app that provides direction for people with visual impairment. The app will provide instructions (using TTS) and get user commands (using STT). Here is the code for my 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)
        {
        }
    }
}
}

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

and my 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();
} 
}

Without onUtteranceCompletedListener, it worked just fine (STT onResult fired). But after onUtteranceCompleted I can't get any STT onResult fired.

Note: I'm using onUtteranceCompletedListener (deprecated) because my testing device is on API level 10 (Android 2.3.3)

Edit: TTS onUtteranceCompleted is fired just fine, the problem is only that STT onResult won't get fired no matter what

Foi útil?

Solução 2

Okay so I think I found the problem, I debugged the app on Jellybean (4.2.2) and the app crashed, then I tried to change the deprecated interface (onUtteranceCompletedListener) into onUtteranceProgressListener (only works on API lv 15+) and it worked just fine, so here is my conclusion:

This problem MIGHT be caused by the deprecated interface, possibly a bug, that could be the reason for the interface's deprecation

Outras dicas

answer is false and you never set it to true in any of the code, thus sst is never start. Thus there is no speech recognition to begin with.

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

        try{
        answer = false;

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

}

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top