如何加快文本到语音的初始化

时间:2015-05-13 14:49:40

标签: java android performance initialization text-to-speech

我正在开发一个应用程序,它需要使用Text to speech类才能读取文本。事情是初始化非常慢,大约4秒。我遵循了本教程:http://code.tutsplus.com/tutorials/use-text-to-speech-on-android-to-read-out-incoming-messages--cms-22524。我做了一些研究,但我没有找到真正好的答案,或者我想念它们。

例如,我通过在一个线程中调用我的checkTTS函数来尝试这个solution但没有结果可见。 Herehere,他们说我的具有意图的checkTTS功能​​可能没那么有用,我可以看看我所需要的语言是否可用。

所以我的问题:

  • 我真的可以检查一下我的语言是否存在,ACTION_CHECK_TTS_DATA之间的真正区别是什么?只检查语言是否可用?
  • 我可以改进别的东西吗?

这是我的活动代码:

public class MyActivity extends Activity {
    private Speaker _speaker;
    private final int CHECK_CODE = 0x1;
    private final int SHORT_DURATION = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        System.out.println("I'm just before checkTTS");
        checkTTS();
    }


    private void checkTTS(){
        System.out.println("I'm in checkTTS");
        Intent check = new Intent();
        check.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(check, CHECK_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == CHECK_CODE){
            if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
                System.out.println("I'm in onActivityResult");
                _speaker = new Speaker(this);
            }else {
                Intent install = new Intent();
                install.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(install);
            }
        }
    }

    @Override
    protected void onDestroy() {    
        super.onDestroy();
        _speaker.destroy();
    }
}

这是我的演讲班:

public class Speaker implements OnInitListener {
    private TextToSpeech _tts;
    private boolean _ready = false;
    private boolean _allowed = false;

    public Speaker(Context context){
        _tts = new TextToSpeech(context, this);
    }   

    public void setSpeedRate(float speechrate) {
        _tts.setSpeechRate(speechrate);
    }

    public boolean isAllowed(){
        return _allowed;
    }

    public void allow(boolean allowed){
        _allowed = allowed;
    }

    @Override
    public void onInit(int status) {
        System.out.println("I'm in onInit from Speaker");
        if(status == TextToSpeech.SUCCESS){
            _tts.setLanguage(Locale.UK);
            _ready = true;
        } else{
            _ready = false;
        }
    }

    public void speak(String text){
        if(_ready && _allowed) {
            HashMap<String, String> hash = new HashMap<String,String>();
            hash.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
            _tts.speak(text, TextToSpeech.QUEUE_ADD, hash);
        }
    }

    public boolean isSpeaking() {
        return _tts.isSpeaking();
    }

    public void pause(int duration){
        _tts.playSilence(duration, TextToSpeech.QUEUE_ADD, null);
    }

    public void stop() {
        _tts.stop();
    }

    public void destroy(){
        _tts.shutdown();
    }
}

这是日食的日志:

15:14:58.897: D/dalvikvm(23966): GC_CONCURRENT freed ...
15:15:00.014: I/System.out(23966): I'm just before checkTTS
15:15:00.014: I/System.out(23966): I'm in checkTTS
15:15:00.046: D/AbsListView(23966): unregisterIRListener() is called 
15-13 15:15:01.272: I/System.out(23966): I'm in onActivityResult
15:15:01.311: I/TextToSpeech(23966): Sucessfully bound to com.samsung.SMT
15:15:01.663: D/AbsListView(23966): onVisibilityChanged() is called, visibility : 4
15:15:01.663: D/AbsListView(23966): unregisterIRListener() is called 
15:15:02.600: I/TextToSpeech(23966): Connected to ComponentInfo{com.samsung.SMT/...}
15:15:02.608: I/TextToSpeech(23966): onServiceConnected but waiting
15:15:02.608: I/System.out(23966): I'm in onInit from Speaker
15:15:02.616: I/System.out(23966): Speaker ready
15:15:02.616: I/TextToSpeech(23966): onServiceConnected waiting end

感谢您的帮助, 最好的祝福, Zed13

Ps:抱歉错误......

编辑:有趣的事实,它似乎用三星的声音比用谷歌的声音更快地启动......

1 个答案:

答案 0 :(得分:0)

  1. 根据Android API,ACTION_CHECK_TTS_DATA&#34;从平台TextToSpeech引擎启动活动,以验证系统上资源文件的正确安装和可用性&#34;。在这种情况下,在收到CHECK_VOICE_DATA_PASS之前创建一个TTS实例是没有意义的。或者,如果由于某种原因(状态标志等)您确信系统已安装所需的TTS资源,您可以使用&#34;乐观方法&#34;,即跳过此步骤,初始化TTS实例(例如,在您的onCreate方法中)并在检查available languagesif some language is supported后。由于您正在跳过一个步骤(ACTION_CHECK_TTS_DATA),如果一切都成功,您的初始化将更快。
  2. 您可以检查系统looking to this code中安装的引擎,并使用该信息启动更准确的ACTION_CHECK_TTS_DATA意图。

    1. 从性能的角度来看,其余代码看起来很不错。但是,请记住,您可以启动ACTION_CHECK_TTS_DATA意图并同时初始化TTS实例 !如果缺少某些内容,则意图已经启动,TTS将无法按预期工作。如果设备具有所有TTS资源,则意图将通过并且TTS已经初始化。您可以查看Audio News app作为此行为的示例;)