使用自定义语音(男性语音)将TTS保存为文件

时间:2019-10-28 09:58:57

标签: android text-to-speech

我创建了一个工作正常的tts男性语音,我的工作代码是

    tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int i) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                Voice voiceobj = new Voice("en-us-x-sfg#male_1-local",
                        Locale.getDefault(), 1, 1, false, null);

                tts.setVoice(voiceobj);
                String text = "Hai buddy, how are you?";
                tts.speak(text, TextToSpeech.QUEUE_FLUSH, null,null);

            }

        }


    });

但是我无法将此tts保存到.mp3或.wav之类的文件中。有没有人知道如何实现这一目标??

1 个答案:

答案 0 :(得分:2)

您必须使用synthesizeToFile()

Voice voiceobj = new Voice("en-us-x-sfg#male_1-local",
        Locale.getDefault(), 1, 1, false, null);

tts.setVoice(voiceobj);
String text = "Hai buddy, how are you?";
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null,null);
Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);
// Define your destination file.
// Remember to create the directories in which the file will be inserted if they are not created yet.
final String dest = "/path/to/dest/file_name.wav";
// Write it to the specified file.
tts.synthesizeToFile(text, params, dest);