java mp3 输入流到字节数组?

发布于 2024-10-04 06:44:25 字数 1083 浏览 4 评论 0原文

在你说“谷歌它”之前,我尝试过,发现了一些有趣的文章,但没有任何效果。

我需要将 mp3 文件从网站转换为字节流,稍后可以将其保存到本地文件中。

这是我的代码(最重要的部分):

Url url = new Url("someUrl");
URLConnection conn = url.openConnection();
byte[] result = inputStreamToByteArray(conn.getInputStream());
// .... some code here
byteArrayToFile(result, "tmp.mp3");

public byte[] inputStreamToByteArray(InputStream inStream){
    InputStreamReader in = new InputStreamReader(inStream):
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int next = inStream.read();
    while (next > -1){
        baos.write(next);
        next = in.read();
    }

    byte[] result = baos.toByteArray();
    baos.flush();
    in.close();
    return result;
} 

public void byteArrayToFile(byte[] byteArray, String outFilePath){
    FileOutputStream fos = new FileOutputStream(outFilePath);
    fos.write(byteArray);
    fos.close()
}

代码编译没有错误。 与 url 的连接有效。它发送正确的响应。

问题出在转换的某个地方。 在 byteArrayToFile() 之后,我还在磁盘上获取了具有适当长度的新文件,但我无法在任何播放器中播放它。它显示长度为 00:00 并且不会播放。

顺便提一句。我想避免任何第三方库。但如果没有其他办法的话...

before you say "Google it", I tried, found a few interesting articles, but nothing worked.

I need to convert a mp3 file from a website to a byte stream, that I can later save into a file locally.

Here is my code (most important parts):

Url url = new Url("someUrl");
URLConnection conn = url.openConnection();
byte[] result = inputStreamToByteArray(conn.getInputStream());
// .... some code here
byteArrayToFile(result, "tmp.mp3");

public byte[] inputStreamToByteArray(InputStream inStream){
    InputStreamReader in = new InputStreamReader(inStream):
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int next = inStream.read();
    while (next > -1){
        baos.write(next);
        next = in.read();
    }

    byte[] result = baos.toByteArray();
    baos.flush();
    in.close();
    return result;
} 

public void byteArrayToFile(byte[] byteArray, String outFilePath){
    FileOutputStream fos = new FileOutputStream(outFilePath);
    fos.write(byteArray);
    fos.close()
}

The code compiles without errors.
Connection to the url is valid. It sends the right response.

The problem is somewhere in the conversion.
I also get the new file on disk, after byteArrayToFile(), with appropriate lenght, but I can't play it in any player. It says length 00:00 and will not play.

Btw. I'd like to avoid any 3rd party libs. But if nothing else works...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2024-10-11 06:44:25

(顺便说一句,您提供的代码不会编译时没有错误。您没有所需的异常处理,并且 inputStream 应该是 InputStream< /code>,仅适用于初学者。)

这就是问题所在:

InputStreamReader in = new InputStreamReader(inStream):

您试图从二进制流中读取数据,并将其转换为文本。它不是文本。对于 MP3 文件等二进制数据,您不应该使用与“Reader”或“Writer”相关的任何内容。

您的 inputStreamToByteArray 方法应如下所示(或使用 Guava 及其 ByteStreams.toByteArray 方法):

public byte[] inputStreamToByteArray(InputStream inStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[8192];
    int bytesRead;
    while ((bytesRead = inStream.read(buffer)) > 0) {
        baos.write(buffer, 0, bytesRead);
    }
    return baos.toByteArray();
}

请注意,我已将其留给调用者来清理输入流。通常,获取流的人也会关闭它 - 对于这样的方法来说,自行关闭它会有点奇怪,IMO。如果您愿意,您可以这样做,但可能在 finally 块中,这样您就知道即使抛出异常,它也总是关闭。

请注意,如果出现异常,您稍后“编写”的代码也不会关闭该文件。您应该养成始终在 finally 块中关闭流的习惯。

(The code you've presented wouldn't compile without errors, by the way. You haven't got required exception handling, and inputStream should be InputStream, just for starters.)

This is the problem:

InputStreamReader in = new InputStreamReader(inStream):

You're trying to read from a binary stream, and convert it into text. It isn't text. You shouldn't be using anything to do with "Reader" or "Writer" for binary data like an MP3 file.

Here's what your inputStreamToByteArray method should look like (or use Guava and its ByteStreams.toByteArray method):

public byte[] inputStreamToByteArray(InputStream inStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[8192];
    int bytesRead;
    while ((bytesRead = inStream.read(buffer)) > 0) {
        baos.write(buffer, 0, bytesRead);
    }
    return baos.toByteArray();
}

Note that I've left it for the caller to clean up the input stream. Typically whoever fetches the stream also closes it - it would be slightly odd for a method like this to close it itself, IMO. You could do so if you wanted to, but probably in a finally block so you'd know that it would always be closed even if an exception was thrown.

Note that your "writing" code later on won't close the file if there's an exception, either. You should get in the habit of always closing streams in finally blocks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文