簡體   English   中英

附加到現有文件而不覆蓋它

[英]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. 如何使用此類打開一個以上的鏈接,將它們一個接一個地播放並將其保存在一個名為句子.mp3的文件中?
  2. 我希望此類采用ArrayList或String數組,並在新URL中打開每個元素以獲取聲音,然后將它們全部保存在一起。
  3. 要運行該類,您需要一個名為jl1.0.jar的庫,您可以從下面的鏈接下載該庫: 在此處輸入鏈接說明

您需要使用旨在將MP3流連接在一起的代碼。 MP3文件格式不僅僅支持文件串聯。

檢出: 合並mp3文件的最佳方法是什么?

暫無
暫無

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

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