使用JFileChooser将语音从文本转换为语音到音频文件

时间:2016-09-12 14:30:07

标签: java jfilechooser

我在我的项目中使用FreeTTS语音。 我想使用JFileChooser将语音保存到音频文件到用户想要的任何位置。我想添加一个保存音频文件的按钮。现在我有一个“打开文件”打开文本文件并将其写入JTextArea和“保存文件”按钮,该按钮将写入JTextArea的文本保存到txt中的输出文件format.I我正在为项目使用NetBeans。

The screenshot of the java application

//保存文本文件的代码

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);
         }
       }
    }  

//代码尝试保存音频文件但没有工作

     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);
                     }


          }

         }  

同样,Open文本文件的代码也在那里。 我没有太多的声誉,为什么我不能直接添加截图,但我添加了链接。

1 个答案:

答案 0 :(得分:0)

设置为freetts语音的音频播放器的SingleFileAudioPlayer对象可用于写入文件。

您可以在此问题上找到解决方案:how can i store output voice to an audio file in freetts

相关问题