简体   繁体   中英

Save voice from text to speech into audio file using JFileChooser

I am using FreeTTS voice in my project for tts. I want to save the voice to an audio file using JFileChooser to any location which user wants. 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.

The screenshot of the java application

// 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.

You can find the solution on this question: how can i store output voice to an audio file in freetts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM