[英]Save voice from text to speech into audio file using JFileChooser
I am using FreeTTS voice in my project for tts.我在我的 tts 项目中使用 FreeTTS 语音。 I want to save the voice to an audio file using JFileChooser to any location which user wants.
我想使用 JFileChooser 将语音保存到用户想要的任何位置的音频文件中。 I want to add a button which saves the audio file.Till now i have a "Open File" which opens text file and writes it into the JTextArea and a "Save File" button which saves the text written in JTextArea to output file in txt format.I am using NetBeans for the project.
我想添加一个保存音频文件的按钮。直到现在我有一个“打开文件”,它打开文本文件并将其写入 JTextArea 和一个“保存文件”按钮,它将在 JTextArea 中写入的文本保存到输出文件中格式。我在项目中使用 NetBeans。
The screenshot of the java application java应用程序截图
// Code for Saving Text File // 保存文本文件的代码
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser saver = new JFileChooser("./");
int returnVal = saver.showSaveDialog(this);
File file = saver.getSelectedFile();
BufferedWriter writer = null;
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
writer = new BufferedWriter( new FileWriter(file.getAbsolutePath()+".txt"));
writer.write(jTextArea1.getText());
writer.close( );
JOptionPane.showMessageDialog(this, "The Message was Saved Successfully!",
"Success!", JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, "The Text could not be Saved!",
"Error!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
//Code tried for saving audio file but did not worked //代码试图保存音频文件但没有奏效
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser saver = new JFileChooser("./");
int returnVal = saver.showSaveDialog(this);
File file = saver.getSelectedFile();
VoiceManager voiceManager = VoiceManager.getInstance();
Voice saveVoice = voiceManager.getVoice(VOICENAME);
saveVoice.allocate();
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try {
audioPlayer = new SingleFileAudioPlayer(new FileWriter( file.getAbsolutePath()+AudioFileFormat.Type.WAVE));
saveVoice.setAudioPlayer(audioPlayer);
saveVoice.speak(jTextArea1.getText());
saveVoice.deallocate();
audioPlayer.close();
JOptionPane.showMessageDialog(this, "The Audio was Saved Successfully!",
"Success!", JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, "The Text could not be Saved!",
"Error!", JOptionPane.INFORMATION_MESSAGE);
}
}
}
Similarly the code for the Opening text file is there.同样,打开文本文件的代码也在那里。 I don't have much reputation that why i am not able to add screenshot directly but i have added the link to it.
我没有太多的声誉,为什么我不能直接添加屏幕截图,但我已经添加了链接。
An object of SingleFileAudioPlayer set as the audio player for the freetts voice can be used to write to a file.设置为 freetts 语音的音频播放器的 SingleFileAudioPlayer 对象可用于写入文件。
You can find the solution on this question: how can i store output voice to an audio file in freetts您可以找到有关此问题的解决方案: 如何将输出语音存储到 freetts 中的音频文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.