2

我的代码:

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.UtteranceProgressListener;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
@SuppressLint("NewApi")
public class MainActivity extends Activity {
    public TextToSpeech tts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tts = new TextToSpeech(this, new ttsInitListener());
    }
    @SuppressLint("TrulyRandom") class ttsInitListener implements OnInitListener {

        @SuppressWarnings("deprecation")
        @Override
        public void onInit(int status) {

            if (status == TextToSpeech.SUCCESS) {
                tts.setLanguage(Locale.getDefault());
                if (!tts.isSpeaking()) {
                    SecureRandom random = new SecureRandom();
                    String aa= new BigInteger(130, random).toString(32);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, aa);
                    tts.speak("Hello Hello HEllo HEllo HEllo",
                            TextToSpeech.QUEUE_FLUSH, map);
                    tts.setOnUtteranceProgressListener(new ttsUtteranceListener());
                }

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

    class ttsUtteranceListener extends UtteranceProgressListener {

        @Override
        public void onDone(String utteranceId) {

            Toast.makeText(getApplicationContext(), "onDone", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onError(String utteranceId) {
            Toast.makeText(getApplicationContext(), "onError", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onStart(String utteranceId) {
            Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_LONG).show();
        }
    }
}

说话的时候

tts.speak("Hello Hello HEllo HEllo HEllo",
                                TextToSpeech.QUEUE_FLUSH, map);

完成然后应该调用类onDone(String utteranceId)的方法ttsUtteranceListener。但ttsUtteranceListener并不总是达到。

为什么这个班级没有达到?

4

1 回答 1

3

你正在使用这个:

String aa= new BigInteger(130, random).toString(32);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, aa);  //aa can be anything random, not necessary a word contained in your text

UtteranceProgressListener是与通过合成队列的话语进度相关的事件的侦听器。

onDone :当话语成功完成处理时调用。

你应该尝试使用这个:

HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "Hello");
tts.speak("Hello Hello HEllo HEllo HEllo", TextToSpeech.QUEUE_FLUSH, map);
tts.setOnUtteranceProgressListener(new ttsUtteranceListener());

然后当 Hello 的话语成功完成时将被触发。

希望能帮助到你。

于 2014-12-17T08:41:30.013 回答