簡體   English   中英

使用 JFileChooser 將語音從文本保存到語音文件中

[英]Save voice from text to speech into audio file using JFileChooser

我在我的 tts 項目中使用 FreeTTS 語音。 我想使用 JFileChooser 將語音保存到用戶想要的任何位置的音頻文件中。 我想添加一個保存音頻文件的按鈕。直到現在我有一個“打開文件”,它打開文本文件並將其寫入 JTextArea 和一個“保存文件”按鈕,它將在 JTextArea 中寫入的文本保存到輸出文件中格式。我在項​​目中使用 NetBeans。

java應用程序截圖

// 保存文本文件的代碼

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


          }

         }  

同樣,打開文本文件的代碼也在那里。 我沒有太多的聲譽,為什么我不能直接添加屏幕截圖,但我已經添加了鏈接。

設置為 freetts 語音的音頻播放器的 SingleFileAudioPlayer 對象可用於寫入文件。

您可以找到有關此問題的解決方案: 如何將輸出語音存儲到 freetts 中的音頻文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM