Здравствуйте, я пытаюсь заставить TextToSpeech работать через Accessibility Stream, добавленный в API 26.
Я пытался использовать параметры пакета, как указано в документации, но код вызывает сбой в TTS Engine.
Вот код:
Bundle mParamsBundle = new Bundle();
if (Build.VERSION.SDK_INT >= 26) {
mParamsBundle.putString(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_ACCESSIBILITY));
}
mTextToSpeech.speak(sentence,
modeAddToQueue ? TextToSpeech.QUEUE_ADD : TextToSpeech.QUEUE_FLUSH,
mParamsBundle, null);
TTS всегда дает сбой, и этот журнал показывает:
I/TextToSpeech: Asked to disconnect from ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
I/TextToSpeech: Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
I/TextToSpeech: Set up connection to ComponentInfo{com.google.android.tts/com.google.android.tts.service.GoogleTTSService}
Что я здесь не так делаю? Помощь будет оценена
Полный код:
import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import java.util.Locale;
public class TextToSpeechHelper implements TextToSpeech.OnInitListener {
final private static float SPEECH_RATE = 0.9f;
private TextToSpeech mTextToSpeech;
private Bundle mParamsBundle;
private Locale mLocale;
public TextToSpeechHelper(Context context, Locale locale) {
mTextToSpeech = new TextToSpeech(context, this);
mLocale = locale;
mParamsBundle = new Bundle();
if (Build.VERSION.SDK_INT >= 26) {
mParamsBundle.putString(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_ACCESSIBILITY));
}
}
public void speak(String sentence) {
speak(sentence, true);
}
public void speak(String sentence, boolean modeAddToQueue) {
if (Build.VERSION.SDK_INT >= 21) {
mTextToSpeech.speak(sentence,
modeAddToQueue ? TextToSpeech.QUEUE_ADD : TextToSpeech.QUEUE_FLUSH,
mParamsBundle, null);
} else {
mTextToSpeech.speak(sentence,
modeAddToQueue ? TextToSpeech.QUEUE_ADD : TextToSpeech.QUEUE_FLUSH,null);
}
}
public boolean isSpeaking() {
return mTextToSpeech.isSpeaking();
}
public void release() {
mTextToSpeech.shutdown();
}
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
mTextToSpeech.setLanguage(mLocale);
//mTextToSpeech.setSpeechRate(SPEECH_RATE);
}
}
}