记录音频流在Java中? [英] Record streaming audio in java?
问题描述
我试图建立一个节目的录制互联网音频流的一部分,并将其保存到一个文件中(preferably MP3或WAV)。我到处找,我无法找到任何像样的方法可以做到这一点。我发现,好像他们会工作(NativeBass和Xuggle)两个不同的库,但也支持64位Windows这正是我所需要的。
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.
有谁知道有什么简单的方法可以节省用java互联网音频流的一部分? (如果它是重要的,它是一个音频/ MPEG流)。
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).
编辑: 好吧,我发现,似乎工作的方式。但我仍然有一个问题
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);
}
}
}
我得到了大部分从这个多一点的搜索后,就到这里另一个答案。但是,有一点我想要做的是在一定的时间内只记录。正如你可以在上面看到,我想只记录5秒的时间间隔。
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) {
然而,由于种种原因,录制的音频是不是5秒长,它的16有谁知道如何更precise在限制流的长度?
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秒你可以自己根据已接收的字节数和音频流的比特率计算。
If you exactly want 5 seconds you can calculate it yourself based on the number of bytes you have received and the bit rate of the audio stream.
这篇关于记录音频流在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!