【问题标题】:UtteranceProgressListener is not called always in Text to Speech?UtteranceProgressListener 不是总是在文本到语音中调用吗?
【发布时间】:2015-02-15 16:08:20
【问题描述】:

我的代码:

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);

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

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

【问题讨论】:

  • 您是否调试过String aa= new BigInteger(130, random).toString(32); 中创建的内容?尝试使用静态参数一次。
  • 之前,我使用 map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "abc");
  • 尝试输入map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "hello");,因为您的文本不包含 abc 而是 hello。
  • 因为 UtteranceProgressListener 用于监听特定文本的话语进度。如果 文本被说出,它将被触发。
  • SecureRandom random = new SecureRandom(); String aa= new BigInteger(5, random).toString(32);字符串 abc="abc"+aa; Toast.makeText(_mContext, "value:"+abc, Toast.LENGTH_LONG).show(); HashMap map = new HashMap(); map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, abc);

标签: java android text-to-speech speech onutterancecompleted


【解决方案1】:

你正在使用这个:

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的话语成功完成时会被触发。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-07
    • 2013-11-05
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 2015-06-16
    相关资源
    最近更新 更多