Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

android - TTS-UtteranceProgressListener not being called

I don't want to put all my code here, so I'm just putting the relevant pieces. If you need more, feel free to ask.

I'm using Text To Speech (TTS) which leads to a speech listener after it asks a question... I found through Log outputs that TTS's onInit is being called, but the UtteranceProgressListener is not and I can't figure out why. Any help is appreciated.

// ---Initialize TTS variables---

        // Implement Text to speech feature
        tts = new TextToSpeech(this, new ttsInitListener());

        // set listener to the TTS engine
        tts.setOnUtteranceProgressListener(new ttsUtteranceListener());

        if (!tts.isSpeaking()) {
            tts.speak("Speak to me", TextToSpeech.QUEUE_FLUSH, null);
        }

// --- TEXT TO SPEECH && SPEECH TO TEXT METHODS ---

class ttsInitListener implements OnInitListener {

    @Override
    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {
            tts.setLanguage(Locale.getDefault());

        } else {
            tts = null;
            Toast.makeText(mContext, "Failed to initialize TTS engine.",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

class ttsUtteranceListener extends UtteranceProgressListener {

    @Override
    public void onDone(String utteranceId) {
        if (processStart) {
            speech.startListening(intent);
        } else {
            ...
        }

    }

    @Override
    public void onError(String utteranceId) {
    }

    @Override
    public void onStart(String utteranceId) {
    }    
}

I added log outputs to all of the my TTS and Speech methods. UtteranceProgressListener's onStart isn't even being called:

11-30 00:38:37.299: D/OpenGLRenderer(15842): Enabling debug mode 0
11-30 00:38:39.782: I/TextToSpeech(15842): Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
11-30 00:38:39.782: I/TextToSpeech(15842): Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
11-30 00:38:39.782: D/LOOK AT ME!!!(15842): ttsInitListener - onInit
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

found the answer...

Turns out that the TTS resources I found online were using a single TTS string source, so the third parameter in tts.speak(String text, int queueMode, HashMap params) was set to null.

to anybody having this issue in the future:

if you set the third param to null, there's no ID for the UtteranceProgressListener to track. The fix was creating and initializing a hashmap, then adding to the included array for each new TTS with a new ID could be tracked. Here's the code:

HashMap<String, String> map = new HashMap<String, String>();

then, before calling tts.speak...

map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID");

then you can call speak with all params...

tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...