Android TTS 使用教程

Android tts 是 text to speak 的简称,使用的时候可以讲text文本直接转换为语音播放出来。使用方法如下:


1、初始化。代码如下:

    //包含语音包
    import android.speech.tts.TextToSpeech; 
    import android.speech.tts.TextToSpeech.OnInitListener;
    TextToSpeech tts;
    //初始化
    tts = new TextToSpeech(this,new OnInitListener()
    {
        @Override
        public void onInit(int status) {
        //如果装载TTS引擎成功
        if(status == TextToSpeech.SUCCESS)
        {
            //设置使用美式英语朗读(虽然设置里有中文选项Locale.Chinese,但并不支持中文)
            int result = tts.setLanguage(Locale.US);
            //如果不支持设置的语言
            if(result != TextToSpeech.LANG_COUNTRY_AVAILABLE 
                    && result != TextToSpeech.LANG_AVAILABLE)
            {
                Toast.makeText(CodeView.this, "TTS暂时不支持这种语言朗读", 50000).show();
            }
        }
    }
    });

初始化完毕之后,直接调用speak函数即可播放语音,如下:


tts.speak("班车即将到达 "+ str, TextToSpeech.QUEUE_ADD, null);



你可能感兴趣的:(android,tts)