android语音播报

1)首先添加语音播报代码:


private TextToSpeech tts = null;
private boolean ttsIsInit = false;
private final static int TTS_DATA_CHECK = 100;
private void initTextToSpeech(){
    //初始化语音
    Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    //TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA
    startActivityForResult(intent, TTS_DATA_CHECK);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
 //   editText.setText(String.valueOf(requestCode) +"&"+Activity.RESULT_OK +" & "+TextToSpeech.Engine.CHECK_VOICE_DATA_PASS);
    if (null == data) {
        return;
    }
    tts = new TextToSpeech(this,new TextToSpeech.OnInitListener() {

                @Override
                public void onInit(int status) {
                    if (status==TextToSpeech.SUCCESS) {
                        ttsIsInit = true;                    
                        tts.setPitch(1.0f);
                        // 设置语速
                        tts.setSpeechRate(1.0f);
                        speak();
                        // }
                    }
                }
                private void speak() {

                    if (tts!=null&&ttsIsInit) {
                        if(!editText.getText().toString().equals("")) {
                            tts.speak(editText.getText().toString(), TextToSpeech.QUEUE_ADD, null);
                        }else{
                            tts.speak("请输入内容,谢谢", TextToSpeech.QUEUE_ADD, null);
                        }
                    }
                }
            });
	}
2)下载安装讯飞的语音包和语音引擎apk。
3)手机TTS设置:
进入系统设置–>语言和输入法–>文字转语音(TTS)输出–>勾选“讯飞语音合成”,语言“中文”。
4)效果测试。


展开阅读全文

没有更多推荐了,返回首页