0

我正在尝试制作一个为视力障碍者提供方向的应用程序。该应用程序将提供说明(使用 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 被触发)。但是在 onUtteranceCompleted 之后,我无法触发任何 STT onResult。

注意:我正在使用 onUtteranceCompletedListener(已弃用),因为我的测试设备处于 API 级别 10(Android 2.3.3)

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

4

2 回答 2

0

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

}

于 2013-03-30T17:36:47.393 回答
0

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

于 2013-04-05T20:01:27.443 回答