【问题标题】:Append to an existing file without overwriting it附加到现有文件而不覆盖它
【发布时间】:2015-04-29 07:40:33
【问题描述】:
import java.net.*;
import java.io.*;
import javazoom.jl.player.Player;


class MP3 {
// the javazoom player
static Player player;

// this is where the audio file is saved
static String filename = "sentence.mp3";

public static void speak(String sentenses) {
    try{    
            String sentence=sentenses;

            sentence = URLEncoder.encode(sentence, "UTF-8");

            // contact Google TTS services
        URL url = new URL("http://translate.google.com/translate_tts?tl=en&q=" + sentence);

            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            urlConn.addRequestProperty("User-Agent", "Mozilla");
            InputStream audioSrc = urlConn.getInputStream();
            DataInputStream read = new DataInputStream(audioSrc);

            // create the audio file
            OutputStream outstream = new FileOutputStream(new File(filename));//cc
            byte[] buffer = new byte[1024];
            int len;
            while ((len = read.read(buffer)) > 0) {
                outstream.write(buffer, 0, len);
            }
            outstream.close();

            // javazoom takes over now
            new MP3().play(filename);

    }catch(Exception e){
           System.out.println(e.getMessage());}
}

        public static void speakFr(String sentenses) {
    try{    
            String sentence=sentenses;

            sentence = URLEncoder.encode(sentence, "UTF-8");

            // contact Google TTS services
            URL url = new URL("http://translate.google.com/translate_tts?tl=fr&q=" + sentence);

            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            urlConn.addRequestProperty("User-Agent", "Mozilla");
            InputStream audioSrc = urlConn.getInputStream();
            DataInputStream read = new DataInputStream(audioSrc);

            // create the audio file
            OutputStream outstream = new FileOutputStream(new File(filename));
            byte[] buffer = new byte[1024];
            int len;
            while ((len = read.read(buffer)) > 0) {
                outstream.write(buffer, 0, len);
            }
            outstream.close();

            // javazoom takes over now
            new MP3().play(filename);

    }catch(Exception e){
           System.out.println(e.getMessage());}
}


// play the MP3 file to the sound card
public static void play(String filename) {

    try {
        FileInputStream fis     = new FileInputStream(filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        player = new Player(bis);
    }
    catch (Exception e) {
        System.out.println("Problem playing file " + filename);
        System.out.println(e);
    }

    // run in new thread to play in background
    new Thread() {
        public void run() {
            try { player.play(); }
            catch (Exception e) { System.out.println(e); }
        }
    }.start();
}

}
  1. 如何使用这个类打开多个链接一一播放 并将它们保存在一个名为 sentences.mp3 的文件中?
  2. 我希望此类获取一个 ArrayList 或 String 数组,并在新 URL 中打开每个元素以获取声音,然后将它们一起保存到文件中。
  3. 为了能够运行这个类,您需要一个名为 jl1.0.jar 的库 您可以从以下链接下载: enter link description here

【问题讨论】:

    标签: java append outputstream fileinputstream fileoutputstream


    【解决方案1】:

    您需要使用旨在将 MP3 流连接在一起的代码。 MP3 文件格式不仅支持文件连接。

    查看: What is the best way to merge mp3 files?

    【讨论】:

      猜你喜欢
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 2012-06-18
      相关资源
      最近更新 更多