简体   繁体   English

在java中录制流媒体音频?

[英]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). 我正在尝试设置一个程序来记录互联网音频流的一部分,并将其保存到文件(最好是mp3或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. 我找到了两个看起来像他们工作的不同的库(NativeBass和Xuggle),但是它们都不支持我需要的64位窗口。

Does anyone know of any simple ways to save a portion of an internet audio stream using java? 有没有人知道使用java保存一部分互联网音频流的简单方法? (If it's important, it's an "audio/mpeg" stream). (如果重要的话,那就是“audio / mpeg”流)。

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. 如上所示,我试图只记录5秒的间隔。

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秒长,它是16.有人知道如何更精确地限制流的长度吗?

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM