简体   繁体   中英

Append to an existing file without overwriting it

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. How can I use this class to open more than one link play them one by one and save them in one file called sentences.mp3?
  2. I want this class to take an ArrayList or array of String and open each element in a new URL to get the sound and then save them all together file.
  3. to be able to run this class you need a library called jl1.0.jar you can downloaded from the link below: enter link description here

You need to use code designed to concatenate MP3 streams together. The MP3 file format doesn't just support file concatenation.

Check out: What is the best way to merge mp3 files?

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