Android原生的TTS(语音播报功能)

android自带的TTS目前只支持英文、法文、意大利文、德文、西班牙文。不支持中文,需要借助中文TTS引擎的帮助

简单的android原生TTS开发

1.添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2.初始化textToSeepch

   TextToSpeech textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == textToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.ENGLISH);//设置语言
                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
                            && result != TextToSpeech.LANG_AVAILABLE) {
                        Toast.makeText(MainActivity.this, "TTS暂时不支持这种语音的朗读!",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        //speech按钮监听事件
        speech.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //播放
                textToSpeech.speak(input.getText().toString(),
                        TextToSpeech.QUEUE_ADD, null);
            }
        });

猜你喜欢

转载自blog.csdn.net/sinat_38892960/article/details/86216949