可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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);
In Android developers it says that this method is deprecated and replaced by this:
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.
回答1:
I searched various site. Finally, I think I could get the answer for your Question...
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);
}
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);
}
I confirmed above code with Genymotion VM Android 5.0 and Android 4.4.4.
回答2:
tts.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);
Try this.
回答3:
So I guess this is the trick:
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);
}
I just need to test this on emulator.
By the way, @Aditya since you've been so helpful I've been stuck in the same project where it should speak that TextToSpeech and turns on the screen but I haven't manage to turn the screen on. I have tried to use wakelocks and flags from all the examples I've found :) This is done trough the proximity sensor that I managed to work. It says the text but doesn't show the screen. Can you help me on this?
Well practice is the key of success. All the suggested answer by me are perfectly working in my eclipse IDE. Solution of your screen lock is below
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();
回答4:
(1) My activity implements TextToSpeech.OnInitListener
(2) I play my synthesized speech in the onInit method, but I suppose (not tried it) you could play it anytime after onInit() is called. But this is the key, you have to wait for the TextToSpeech engine to initialize.
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
public TextToSpeech mTTS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTTS = new TextToSpeech(this, this);
}
@Override
public void onInit(int i) {
mTTS.setLanguage(Locale.UK);
mTTS.speak("Hello, how are you?", TextToSpeech.QUEUE_ADD, null, null);
}
}
回答5:
Try this
tts=new TextToSpeech(getBaseContext(),new TextToSpeech.OnInitListener()
{
@Override
public void onInit(int status)
{
tts.setLanguage(Locale.getDefault());
tts.setPitch(1.3f);
tts.setSpeechRate(1f);
}
});
回答6:
The wakelock, I managed to make ti work this way:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "MyWakelock");
wl.aquire();
wl.release();