java mp3 输入流到字节数组?
在你说“谷歌它”之前,我尝试过,发现了一些有趣的文章,但没有任何效果。
我需要将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(顺便说一句,您提供的代码不会编译时没有错误。您没有所需的异常处理,并且
inputStream
应该是InputStream< /code>,仅适用于初学者。)
这就是问题所在:
您试图从二进制流中读取数据,并将其转换为文本。它不是文本。对于 MP3 文件等二进制数据,您不应该使用与“Reader”或“Writer”相关的任何内容。
您的
inputStreamToByteArray
方法应如下所示(或使用 Guava 及其ByteStreams.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 beInputStream
, just for starters.)This is the problem:
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 itsByteStreams.toByteArray
method):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.