带有 API 21 的 TextToSpeech [英] TextToSpeech with API 21

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

问题描述

有人可以帮助我使用带有 API 21 的 TTS.版本 21 已弃用所有可用示例

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

这是我在最后一行给出错误的代码:

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 开发者中,它说这个方法已被弃用并被替换为:

In Android developers it says that this method is deprecated and replaced by this:

speak(String text, int queueMode, HashMap params)此方法在 API 级别 21 中已弃用.从 API 级别 21 开始,替换为 speak(CharSequence, int, Bundle, String).

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

有人可以帮我编写应用程序代码吗.

Can someone help to code my app.

推荐答案

我搜索了各种网站.最后,我想我可以得到你的问题的答案...

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

我使用 Genymotion VM Android 5.0 和 Android 4.4.4 确认了上述代码.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆