本文介绍了TextToSpeech与API 21的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

有人可以提供帮助。所有可用的例子是pcated版本21日$ P $

Can someone help me using TTS with API 21.All examples available are deprecated with version 21

这是我的code提供的最后一行的错误:

here's my code giving error on last line:

Calendar cal = Calendar.getInstance();
                    cal.getTime();
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                    String text = sdf.toString();
                    btn.setText("Ouvir as Horas");

                    TextToSpeech tts = new TextToSpeech(NightClock.this,(TextToSpeech.OnInitListener) NightClock.this);
                    tts.setLanguage(Locale.US);
                    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

在Android开发它说,该方法是pcated并且由一个在底部取代德$ P $。一个人可以帮到code我的应用程序。

In Android developers it says that this method is deprecated and replaced by the one on the bottom.Can someone help to code my app.

讲(字符串文本,诠释queueMode,HashMap的PARAMS)这种方法去precated在API层面21. API级别21,通过代言取代(的CharSequence,INT,捆绑,字符串)。

speak(String text, int queueMode, HashMap params)This method was deprecated in API level 21. As of API level 21, replaced by speak(CharSequence, int, Bundle, String).

推荐答案

我搜查各种网站。最后,我想我可以得到答案的提问......

I searched various site. Finally, I think I could get the answer for your Question...

而不是调用tts.speak()直接,把下面的if-else语句。

Instead calling tts.speak() directly, put the following if-else statement.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    ttsGreater21(text);
} else {
    ttsUnder20(text);
}

然后声明ttsGreater21()和ttsUnder20()如下:

Then declare ttsGreater21() and ttsUnder20() as follows.

@SuppressWarnings("deprecation")
private void ttsUnder20(String text) {
    HashMap<String, String> map = new HashMap<>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String text) {
    String utteranceId=this.hashCode() + "";
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}

我证实了上述code与Genymotion VM的Andr​​oid 5.0和Android 4.4.4。

I confirmed above code with Genymotion VM Android 5.0 and Android 4.4.4.

这篇关于TextToSpeech与API 21的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:26