我想出了方法,您只需要简单地使用
SingleFileAudioPlayer传递文件名和文件类型,样本声明就应该像这样:
audioPlayer = new SingleFileAudioPlayer("output",Type.WAVE);现在您需要将
SinglefileAudioplayer对象附加到您的
VoiceManager对象:例如
helloVoice.setAudioPlayer(audioPlayer);
现在使用:
hellovoice.speak("zyxss");这样会保存文件以及任何说话内容。请记住关闭音频播放器,否则文件将不会保存。
audioPlayer.close();退出前放 。
这是完整的工作代码,它将转储到您的C目录中
import com.sun.speech.freetts.FreeTTS; import com.sun.speech.freetts.Voice; import com.sun.speech.freetts.VoiceManager; import com.sun.speech.freetts.audio.AudioPlayer; import com.sun.speech.freetts.audio.SingleFileAudioPlayer; import javax.sound.sampled.AudioFileFormat.Type; public class FreeTTSHelloWorld { public static void main(String[] args) {// listAllVoices(); FreeTTS freetts; AudioPlayer audioPlayer = null; String voiceName = "kevin16"; System.out.println(); System.out.println("Using voice: " + voiceName); VoiceManager voiceManager = VoiceManager.getInstance(); Voice helloVoice = voiceManager.getVoice(voiceName); if (helloVoice == null) { System.err.println( "Cannot find a voice named " + voiceName + ". Please specify a different voice."); System.exit(1); } helloVoice.allocate(); //create a audioplayer to dump the output fileaudioPlayer = new SingleFileAudioPlayer("C://output",Type.WAVE); //attach the audioplayer helloVoice.setAudioPlayer(audioPlayer); helloVoice.speak("Thank you for giving me a voice. " + "I'm so glad to say hello to this world."); helloVoice.deallocate();//don't forget to close the audioplayer otherwise file will not be saved audioPlayer.close(); System.exit(0); } }


