android - 共享 TTS 在 Activity 死了然后又活了之后没有工作

标签 android text-to-speech

我采用了下面的代码,它工作正常,将 TTS 对象共享给其他 Activity :

       package com.simekadam.blindguardian;

            import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

public class SpeechHelper implements OnInitListener {

private static TextToSpeech mTts;
private String text; 
private static final SpeechHelper helper = new SpeechHelper();

public static SpeechHelper getInstance(){

    return helper;
}


public void say(String text, Context context){

    if(mTts == null){
        this.text = text;
        mTts = new TextToSpeech(context, (OnInitListener) helper);

    }
    else{
        mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}


@Override
public void onInit(int status) {
    // TODO Auto-generated method stub
    if (status == TextToSpeech.SUCCESS) {
        mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}

public void stopTTS(){
    if(mTts != null){
        mTts.shutdown();
        mTts.stop();
        mTts = null;
    }
}

}

但是,如果我离开 Activity 然后又回到它,我的应用程序就不再说话了。没有错误。它只是不再说话了。

有人能帮帮我吗?

谢谢!

我刚刚在 Eclipse 中的消息中看到以下消息:“正在向死线程上的处理程序发送消息”

我可以做什么来解决这个问题? =)

最佳答案

如果您使用应用程序上下文而不是当前 Activity ,则不会显示此错误。例如:

mTts = new TextToSpeech(context.getApplicationContext(), (OnInitListener) helper);

关于android - 共享 TTS 在 Activity 死了然后又活了之后没有工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9440955/

相关文章:

android - 更新到 buildTools 27.0.3 后的错误消息

java - 同时通过蓝牙发送音乐和附加数据?

android - 无法从 Android 直接连接到 SQL Server

ios - 如何在 iOS 7 中使用 AVSpeechSynthesizer 改变说话时的语速

Android webview 4.4 css 100% 不显示任何内容

android - 克隆 git 仓库

text-to-speech - 为文本到语音的标准文本减慢 Twilio 的 TwiML “Say” 命令

javascript - Node JS 使用 APEX 进行文本转语音

ios - AVSpeechUtterance 最大音量非常安静,速度非常快

macos - 使用文字转语音作为音频文件(即mp3)存储文字?