简体   繁体   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. 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? 如何使用此类打开一个以上的链接,将它们一个接一个地播放并将其保存在一个名为句子.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. 我希望此类采用ArrayList或String数组,并在新URL中打开每个元素以获取声音,然后将它们全部保存在一起。
  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 要运行该类,您需要一个名为jl1.0.jar的库,您可以从下面的链接下载该库: 在此处输入链接说明

You need to use code designed to concatenate MP3 streams together. 您需要使用旨在将MP3流连接在一起的代码。 The MP3 file format doesn't just support file concatenation. MP3文件格式不仅仅支持文件串联。

Check out: What is the best way to merge mp3 files? 检出: 合并mp3文件的最佳方法是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不覆盖现有数据的情况下使用DOM附加现有XML文件? 在java中 - How to append an existing XML file using DOM without overwriting the existing data? in java 如何使用DOM附加现有XML文件而不覆盖现有数据? - How to append an existing XML file using DOM without overwriting the existing data? java - 如何在不覆盖文件中写入的数据的情况下将对象(非字符串类型)附加到现有文件中? - How to append objects (not string type) to a existing file without overwriting the written data in the file in java? 将文本附加到现有文件而不覆盖它 - Appending text to existing file without overwriting it 如何在不使用Java覆盖字节数组的情况下将字节数组追加到文件 - how to append a byte array to file without overwriting it using java 在不覆盖其现有内容的情况下向 tar 文件添加条目 - Add an entry to a tar file without overwriting its existing contents 使用 Java 创建具有相似名称的文件而不覆盖现有文件 - Create files with similar names using Java without overwriting existing file Java覆盖现有的输出文件 - Java overwriting an existing output file 使用Poi附加数据而不会覆盖标题 - Append data with Poi without overwriting headers 如何在不覆盖该文件中的信息的情况下使用 spring 批处理将标题和尾部写入现有文件? - How do I write a header and trailer to an existing file using spring batch without overwriting the information in that file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM