Java OuttenceProgressListener不总是在文本到语音中调用?

Java OuttenceProgressListener不总是在文本到语音中调用?,java,android,text-to-speech,speech,onutterancecompleted,Java,Android,Text To Speech,Speech,Onutterancecompleted,我的代码: 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.Buil

我的代码:

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();
        }
    }
}
Finish然后应该调用
onDone(stringouttanceid)
ttsunterancelistener类的方法。但是,
ttsunterancelistener
并不总是可以到达

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

您正在使用这个:

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
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());
然后,当问候成功完成时,它将被触发


希望有帮助。

您调试了
String aa=new biginger(130,random)中创建的内容了吗?尝试使用静态参数一次。之前,我使用map.put(TextToSpeech.Engine.KEY_param_outrance_ID,“abc”);尝试放置
map.put(TextToSpeech.Engine.KEY\u PARAM\u outrance\u ID,“hello”)因为您的文本不包含abc,但包含hello。因为话语进程侦听器用于侦听特定文本的话语进程。如果该文本被说出,它将被激发。SecureRandom=new SecureRandom();字符串aa=新的BigInteger(5,随机).toString(32);字符串abc=“abc”+aa;Toast.makeText(_mContext,“value:+abc,Toast.LENGTH_LONG).show();HashMap=newHashMap();map.put(TextToSpeech.Engine.KEY_PARAM_speak_ID,abc);
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());