栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

setOnUtteranceProgressListener根本不适用于API> 21的文本到语音

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

setOnUtteranceProgressListener根本不适用于API> 21的文本到语音

主要问题是:

1)在初始化tts之前设置进度监听器。

2)尝试用后台线程制作Toast。

我还有其他建议的更改,但不是必需的:

public class MainActivity extends AppCompatActivity {    String message = "How may I help you?";    String mostRecentUtteranceID;    private TextToSpeech myTTS;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        myTTS = new TextToSpeech(this, new TextToSpeech.onInitListener() { @Override public void onInit(int status) {     if(myTTS.getEngines().size() == 0){         Toast.makeText(MainActivity.this,"No Engines Installed",Toast.LENGTH_LONG).show();     }else{         if (status == TextToSpeech.SUCCESS){  ttsInitialized();         }     } }        });    }    private void ttsInitialized() {        // *** set UtteranceProgressListener AFTER tts is initialized ***        myTTS.setonUtteranceProgressListener(new UtteranceProgressListener() { @Override public void onStart(String utteranceId) { } @Override // this method will always called from a background thread. public void onDone(String utteranceId) {     // only respond to the most recent utterance     if (!utteranceId.equals(mostRecentUtteranceID)) {          Log.i("XXX", "onDone() blocked: utterance ID mismatch.");         return;      } // else continue...     boolean wasCalledFromBackgroundThread = (Thread.currentThread().getId() != 1);     Log.i("XXX", "was onDone() called on a background thread? : " + wasCalledFromBackgroundThread);     Log.i("XXX", "onDone working.");     // for demonstration only... avoid references to      // MainActivity (unless you use a WeakReference)     // inside the onDone() method, as it     // can cause a memory leak.     runonUiThread(new Runnable() {         @Override         public void run() {  // *** toast will not work if called from a background thread ***  Toast.makeText(MainActivity.this,"onDone working.",Toast.LENGTH_LONG).show();         }     }); } @Override public void onError(String utteranceId) { }        });        // set Language        myTTS.setLanguage(Locale.US);        // set unique utterance ID for each utterance        mostRecentUtteranceID = (new Random().nextInt() % 9999999) + ""; // "" is String force        // set params        // *** this method will work for more devices: API 19+ ***        HashMap<String, String> params = new HashMap<>();        params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, mostRecentUtteranceID);        myTTS.speak(message,TextToSpeech.QUEUE_FLUSH,params);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470051.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号