Jos*_*ges 18 api android text-to-speech
有人可以帮我使用带有API 21的TTS.所有可用的示例都不推荐使用版本21
这是我的代码在最后一行给出错误:
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);
Run Code Online (Sandbox Code Playgroud)
在Android开发人员中,它表示不推荐使用此方法,并将其替换为:
speak(String text,int queueMode,HashMap params)此方法在API级别21中已弃用.从API级别21开始,由speak(CharSequence,int,Bundle,String)取代.
有人可以帮我编写我的应用程序.
xan*_*291 41
我搜索了各种网站.最后,我想我可以得到你的问题的答案......
而是直接调用tts.speak(),输入以下if-else语句.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ttsGreater21(text);
} else {
ttsUnder20(text);
}
Run Code Online (Sandbox Code Playgroud)
然后按如下方式声明ttsGreater21()和ttsUnder20().
@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);
}
Run Code Online (Sandbox Code Playgroud)
我使用Genymotion VM Android 5.0和Android 4.4.4确认了上述代码.
小智 5
tts.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);
Run Code Online (Sandbox Code Playgroud)
试试这个.
所以我猜这就是诀窍:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.speak("12 e8", TextToSpeech.QUEUE_FLUSH, null, null);
}
else {
tts.speak("12 e8", TextToSpeech.QUEUE_FLUSH, null);
}
Run Code Online (Sandbox Code Playgroud)
我只需要在模拟器上测试它.
顺便说一句,@ Aditya,因为你一直很有帮助我已经陷入同一个项目,它应该说TextToSpeech并打开屏幕,但我没有设法打开屏幕.我试图使用我发现的所有例子中的唤醒锁和标志:)这是通过我设法工作的接近传感器完成的.它说文字但不显示屏幕.你能帮帮我吗?
良好的实践是成功的关键.我所有建议的答案都完全适用于我的eclipse IDE.屏幕锁定的解决方案如下
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
..screen will stay on during this section..
wl.release();
小智 0
尝试这个
tts=new TextToSpeech(getBaseContext(),new TextToSpeech.OnInitListener()
{
@Override
public void onInit(int status)
{
tts.setLanguage(Locale.getDefault());
tts.setPitch(1.3f);
tts.setSpeechRate(1f);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25040 次 |
| 最近记录: |