Record streaming audio in java?

I'm trying to set up a program to record a portion of an internet audio stream, and save it to a file (preferably mp3 or wav). I've looked everywhere and I can't find any decent ways to do this. I found two different libraries that seemed like they'd work (NativeBass and Xuggle), but neither supported 64-bit windows which is what I need.

Does anyone know of any simple ways to save a portion of an internet audio stream using java? (If it's important, it's an "audio/mpeg" stream).

EDIT: Okay, I found a way that seems to work. But I still have a question

import java.net.URLConnection;
import java.net.URL;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
public class Test{

    public static void main (String[] args){
        try{
            URLConnection conn = new URL("http://streamurl.com/example").openConnection();
            InputStream is = conn.getInputStream();

            OutputStream outstream = new FileOutputStream(new File("C:/Users/Me/Desktop/output.mp3"));
            byte[] buffer = new byte[4096];
            int len;
            long t = System.currentTimeMillis();
            while ((len = is.read(buffer)) > 0 && System.currentTimeMillis() - t <= 5000) {
                outstream.write(buffer, 0, len);
            }
            outstream.close();
        }
        catch(Exception e){
            System.out.print(e);
        }
    }
}

I got most of this from another answer on here after a bit more searching. However, one thing I'm trying to do is only record for a certain amount of time. As you can see above, I tried to only record a 5 second interval.

long t = System.currentTimeMillis();
while ((len = is.read(buffer)) > 0 && System.currentTimeMillis() - t <= 5000) {

However, for one reason or another, the recorded audio isn't 5 seconds long, it's 16. Does anyone know how to be more precise in limiting the length of the stream?


如果您确切需要5秒钟,您可以根据您收到的字节数和音频流的比特率自行计算。

链接地址: http://www.djcxy.com/p/49072.html

上一篇: 可以在多个gpus上运行cuda内核吗?

下一篇: 在java中记录流媒体音频?