我用男声创建了一个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之类的文件中。有人知道如何实现这一点吗
您必须使用syntheToFile((。
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);