【问题标题】:Write mp3 file to disk while playing in Java在 Java 中播放时将 mp3 文件写入磁盘
【发布时间】:2012-02-15 08:11:10
【问题描述】:

我有一个应用程序使用 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 文件保存到磁盘?

【问题讨论】:

    标签: java javasound bufferedinputstream jlayer


    【解决方案1】:

    为避免必须两次遍历字节,您需要将来自连接的输入流包装在一个过滤器中,该过滤器将读取的任何数据写入输出流,即一种“三通管输入流”。这样的类自己写并不难,但是你可以通过使用 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();
    

    【讨论】:

      【解决方案2】:
      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();  
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          包装你自己的 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);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-01
            • 2013-05-28
            • 2013-05-25
            相关资源
            最近更新 更多