我阅读了许多博客,但在调用shutdown后仍然面临更新文本字符串的问题。下次点击播放时,会说出旧字符串和新字符串。这是代码:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.batful);
try {
editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();
// editor.putBoolean("b", true).apply();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
value = sharedPreferences.getString("personalmessage", null);
Log.e("Voice Message create: ", value);
value = sharedPreferences.getString("personalmessage", null);
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.ENGLISH);
/* textToSpeech.setLanguage(Locale.getDefault());*/
final Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
if (Build.VERSION.SDK_INT >= 21) {
textToSpeech.speak(value, TextToSpeech.QUEUE_FLUSH, null, null);
} else {
textToSpeech.speak(value, TextToSpeech.QUEUE_FLUSH, null);
}
h.postDelayed(this, delay);
}
}, delay);
}
}
});
stopvol = (TextView)
findViewById(R.id.stop);
stopvol.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
turnoffbeep(getApplicationContext());
finish();
}
});
} catch (
Exception e)
{
e.printStackTrace();
}
}
static void turnoffbeep(Context contextforstat) {
Log.i(TAG, "Turn Off beep");
if ((textToSpeech != null)) {
// textToSpeech.stop();
textToSpeech.shutdown();
}
}
如果您看到添加了QUEUE_FLUSH但未达到结果。看看Android文档,我相信我的注意力是正确的。仍然如果有人帮助我寻找不丢弃旧文本的正确理由,请
如果我将共享首选项的输入更改为编辑文本,那么就是其他事实,它说的是最新的文本而不是旧的。
答案 0 :(得分:0)