TextToSpeech 停止()不工作
TextToSpeech stop() not working
我正试图在按下后退按钮时停止 TextToSpeech
。但是即使我关闭我的应用程序,语音也不会停止。只有当我清除缓存时,语音才会停止。我该如何解决这个问题?请帮助我理解。
private boolean mShouldSpeak = true;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cat);
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.setEngineByPackageName(enginePackageName);
tts.setLanguage(Locale.getDefault());
tts.setPitch(0);
tts.setSpeechRate(1);
speak();
}
}
});
}
private void speak() {
if (mShouldSpeak == true)
{
tts.speak("Автор: " +getResources().getString(R.string.catAuthor), TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
tts.speak(getResources().getString(R.string.catName), TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
tts.speak(getResources().getString(R.string.catDesc), TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
}
}
@Override
protected void onDestroy() {
if (tts != null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onBackPressed() {
onDestroy();
super.onBackPressed();
}
public void onPause(){
if(tts !=null){
tts.stop();
tts.shutdown();
}
super.onPause();
}
在 activity 暂停时停止发言
也许您正在创建大量 TextToSpeech
对象?那么可能是你停错了tts
。
尝试简单的
if (tts == null) {
tts = new TextToSpeech(...) {...}
}
仅在对象尚不存在时才对其进行初始化。
像这样修改,这通常对我有用:我相信你正在使用多个 TextToSpeech
对象,即使你不认为你是。这是一个普遍的问题。
@Override
protected void onStop()
{
super.onStop();
if(tts != null){
tts.shutdown();
}
}
根据评论,您需要确保:
tts.setEngineByPackageName(enginePackageName)
包含安装在设备上的 Text to Speech 引擎的有效包名称,例如 com.google.android.tts
或 com.svox.pico
。
要查看有关已安装引擎的信息,请参阅我的回答here
不应用此参数将绑定在 Text to Speech 设置中选择为设备默认值的引擎。
我正试图在按下后退按钮时停止 TextToSpeech
。但是即使我关闭我的应用程序,语音也不会停止。只有当我清除缓存时,语音才会停止。我该如何解决这个问题?请帮助我理解。
private boolean mShouldSpeak = true;
TextToSpeech tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cat);
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.setEngineByPackageName(enginePackageName);
tts.setLanguage(Locale.getDefault());
tts.setPitch(0);
tts.setSpeechRate(1);
speak();
}
}
});
}
private void speak() {
if (mShouldSpeak == true)
{
tts.speak("Автор: " +getResources().getString(R.string.catAuthor), TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
tts.speak(getResources().getString(R.string.catName), TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
tts.speak(getResources().getString(R.string.catDesc), TextToSpeech.QUEUE_ADD, null);
tts.playSilence(1000, TextToSpeech.QUEUE_ADD, null);
}
}
@Override
protected void onDestroy() {
if (tts != null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onBackPressed() {
onDestroy();
super.onBackPressed();
}
public void onPause(){
if(tts !=null){
tts.stop();
tts.shutdown();
}
super.onPause();
}
在 activity 暂停时停止发言
也许您正在创建大量 TextToSpeech
对象?那么可能是你停错了tts
。
尝试简单的
if (tts == null) {
tts = new TextToSpeech(...) {...}
}
仅在对象尚不存在时才对其进行初始化。
像这样修改,这通常对我有用:我相信你正在使用多个 TextToSpeech
对象,即使你不认为你是。这是一个普遍的问题。
@Override
protected void onStop()
{
super.onStop();
if(tts != null){
tts.shutdown();
}
}
根据评论,您需要确保:
tts.setEngineByPackageName(enginePackageName)
包含安装在设备上的 Text to Speech 引擎的有效包名称,例如 com.google.android.tts
或 com.svox.pico
。
要查看有关已安装引擎的信息,请参阅我的回答here
不应用此参数将绑定在 Text to Speech 设置中选择为设备默认值的引擎。