在Java中播放时将mp3文件写入磁盘

发布于 2025-01-06 11:41:25 字数 535 浏览 2 评论 0原文

我有一个使用 JLayer/BasicPlayer 库通过 HTTP 播放远程 MP3 文件的应用程序。我想将播放的 mp3 文件保存到磁盘而不重新下载它们。

这是使用基于 JLayer 的 BasicPlayer 来播放 MP3 文件的代码。

String mp3Url = "http://ia600402.us.archive.org/6/items/Stockfinster.-DeadLinesutemos025/01_Push_Push.mp3";
URL url = new URL(mp3Url);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

BasicPlayer player = new BasicPlayer();
player.open(bis);
player.play();

如何将 mp3 文件保存到磁盘?

I have an application playing remote MP3 files over HTTP using the JLayer/BasicPlayer libraries. I want to save the played mp3 files to disk without re-downloading them.

This is the code using the JLayer based BasicPlayer for Playing the MP3 file.

String mp3Url = "http://ia600402.us.archive.org/6/items/Stockfinster.-DeadLinesutemos025/01_Push_Push.mp3";
URL url = new URL(mp3Url);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

BasicPlayer player = new BasicPlayer();
player.open(bis);
player.play();

How would I save the mp3 file to disk?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我家小可爱 2025-01-13 11:41:26
BufferedInputStream in = new BufferedInputStream(is);

OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(savePathAndFilename)));  
    byte[] buf = new byte[256];  
    int n = 0;  
    while ((n=in.read(buf))>=0) {  
       out.write(buf, 0, n);  
    }  
    out.flush();  
    out.close();  
BufferedInputStream in = new BufferedInputStream(is);

OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(savePathAndFilename)));  
    byte[] buf = new byte[256];  
    int n = 0;  
    while ((n=in.read(buf))>=0) {  
       out.write(buf, 0, n);  
    }  
    out.flush();  
    out.close();  
痴情换悲伤 2025-01-13 11:41:26

您可以首先使用 FileInputStream 将流写入磁盘。然后从文件重新加载流。

You can first write the stream to disk with FileInputStream. Then reload the stream from file.

为你鎻心 2025-01-13 11:41:26

包装你自己的InputStream

class myInputStream extends InputStream {

    private InputStream is;
    private FileOutputStream resFile;
    public myInputStream(InputStream is) throws FileNotFoundException {
        this.is = is;
        resFile = new FileOutputStream("path_to_result_file");
    }

    @Override
    public int read() throws IOException {
        int b = is.read();
        if (b != -1)
            resFile.write(b);
        return b;
    }

    @Override
    public void close() {
        try {
            resFile.close();
        } catch (IOException ex) {
        }
        try {
            is.close();
        } catch (IOException ex) {
        }
    }
}

并使用

InputStream is = conn.getInputStream();
myInputStream myIs = new myInputStream(is);
BufferedInputStream bis = new BufferedInputStream(myIs);

Wrap you own InputStream

class myInputStream extends InputStream {

    private InputStream is;
    private FileOutputStream resFile;
    public myInputStream(InputStream is) throws FileNotFoundException {
        this.is = is;
        resFile = new FileOutputStream("path_to_result_file");
    }

    @Override
    public int read() throws IOException {
        int b = is.read();
        if (b != -1)
            resFile.write(b);
        return b;
    }

    @Override
    public void close() {
        try {
            resFile.close();
        } catch (IOException ex) {
        }
        try {
            is.close();
        } catch (IOException ex) {
        }
    }
}

and use

InputStream is = conn.getInputStream();
myInputStream myIs = new myInputStream(is);
BufferedInputStream bis = new BufferedInputStream(myIs);
心作怪 2025-01-13 11:41:25

为了避免必须两次检查字节,您需要将来自连接的输入流包装在过滤器中,该过滤器将读取的任何数据写入输出流,即一种“tee 管道输入流”。自己编写这样的类并不困难,但您可以使用 Apache Commons IO 库中的 TeeInputStream 来节省工作。

Apache Commons IO:http://commons.apache.org/io/
TeeInputStream javadoc:http://commons.apache。 org/io/apidocs/org/apache/commons/io/input/TeeInputStream.html

编辑: 概念验证:

import java.io.*;

public class TeeInputStream extends InputStream {
    private InputStream in;
    private OutputStream out;

    public TeeInputStream(InputStream in, OutputStream branch) {
        this.in=in;
        this.out=branch;
    }
    public int read() throws IOException {
        int read = in.read();
        if (read != -1) out.write(read);
        return read;
    }
    public void close() throws IOException {
        in.close();
        out.close();
    }
}

如何使用它:

...
BufferedInputStream bis = new BufferedInputStream(is);
TeeInputStream tis = new TeeInputStream(bis,new FileOutputStream("test.mp3"));

BasicPlayer player = new BasicPlayer();
player.open(tis);
player.play();

To avoid having to go through the bytes twice, you need to wrap the input stream from the connection in a filter that writes any data that is read to an output stream, i.e. a kind of a "tee pipe input stream." Such a class is not that difficult to write yourself, but you can save the work by using TeeInputStream from the Apache Commons IO library.

Apache Commons IO: http://commons.apache.org/io/
TeeInputStream javadoc: http://commons.apache.org/io/apidocs/org/apache/commons/io/input/TeeInputStream.html

Edit: Proof-of-concept:

import java.io.*;

public class TeeInputStream extends InputStream {
    private InputStream in;
    private OutputStream out;

    public TeeInputStream(InputStream in, OutputStream branch) {
        this.in=in;
        this.out=branch;
    }
    public int read() throws IOException {
        int read = in.read();
        if (read != -1) out.write(read);
        return read;
    }
    public void close() throws IOException {
        in.close();
        out.close();
    }
}

How to use it:

...
BufferedInputStream bis = new BufferedInputStream(is);
TeeInputStream tis = new TeeInputStream(bis,new FileOutputStream("test.mp3"));

BasicPlayer player = new BasicPlayer();
player.open(tis);
player.play();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文