在java中录制流音频? [英] Record streaming audio in java?

查看:37
本文介绍了在java中录制流音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个程序来记录互联网音频流的一部分,并将其保存到一个文件(最好是 mp3 或 wav).我到处找,我找不到任何体面的方法来做到这一点.我发现两个不同的库似乎可以工作(NativeBass 和 Xuggle),但都不支持我需要的 64 位窗口.

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 秒.有谁知道如何更精确地限制流的长度?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆