在下载整个文件之前,是否可以知道MP3的持续时间? [英] Is it possible to know the duration of an MP3 before the entire file is downloaded?

查看:118
本文介绍了在下载整个文件之前,是否可以知道MP3的持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于MP3的文件格式的问题.

This is a question about the file format of MP3s.

我一直在寻找一种获得MP3持续时间的方法.由于我使用JLayer SPI解码MP3,因此我发现在音频源为文件的情况下这是可能的.

I've been hunting for a way to get an MP3 duration. Since I'm using JLayer SPI to decode the MP3 I've discovered that this is possible where the audio source is a file.

AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(source);
Long microseconds = (Long) fileFormat.properties().get("duration");
System.out.println(microseconds);

但是,当我使用相似的代码和相同的MP3并将源更改为URL时,JLayer不提供持续时间:

However when I take the similar code and the very same MP3 and change the source to a URL, JLayer does not provide the duration:

AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(source.toURI().toURL());
Long microseconds = (Long) fileFormat.properties().get("duration");
System.out.println(microseconds);

这是一个重要的区别,因为我要播放的某些音乐将来自URL,而不是本地文件.

This is an important distinction because some of the music I want to play will be sourced from URLs not local files.

我怀疑这是由于文件格式的限制所致.

I suspect that this is due to limitations of the file format.

这使我想到了一个非常简单的问题.在下载整个文件之前,是否可以知道MP3的持续时间?

This leads me to a very simple question. Is it possible to know the duration of an MP3 before the entire file has been downloaded?

推荐答案

答案是否定的. mp3文件格式没有有关文件持续时间的任何信息.要计算持续时间,您需要将文件的长度除以比特率.这也可以解释您的库的行为.解决方法是,如果您可以控制服务器上的文件,则应该可以将持续时间嵌入到id3标签中.

The answer is no. The mp3 file format doesn't have any information regarding the duration of the file. To compute the duration you would need to divide the length of the file by the bit rate. That would also explain the behavior of your library. As a workaround, if you have control of the files on the server you should be able to embed the duration in an id3 tag.

这篇关于在下载整个文件之前,是否可以知道MP3的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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