用 Java 读出 Mp3 歌曲的时间/长度/持续时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3140992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read out Time/Length/Duration of an Mp3 song in Java
提问by Timothy
I wonder how I can read out the duration of an mp3 song. If I'm right it's not an ID3 Tag so I guess I have to calculate it somehow ? For the rest of the ID3 Tags I'm using this libary: http://javamusictag.sourceforge.net/index.html
我想知道如何读出 mp3 歌曲的持续时间。如果我是对的,它不是 ID3 标签,所以我想我必须以某种方式计算它?对于其余的 ID3 标签,我正在使用这个库:http://javamusictag.sourceforge.net/index.html
Cheers
干杯
回答by Mardo Del Cid
I would change the code to...
我会将代码更改为...
File file = new File("filename.mp3");
AudioFileFormat baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(file);
Map properties = baseFileFormat.properties();
Long duration = (Long) properties.get("duration");
using MP3SPIto use new MpegAudioFileReader()instead of AudioSystem, because i was getting an UnsupportedAudioFileException occured : file is not a supported file typewith mp3 files
使用MP3SPI来使用new MpegAudioFileReader(),而不是AudioSystem,因为我得到了一个UnsupportedAudioFileException occured : file is not a supported file typeMP3文件
回答by Simon
You could try using MP3SPIwhich is based on JLayer. The following code will then allow you to get the song duration:
你可以尝试使用MP3SPI这是基于JLayer。然后,以下代码将允许您获取歌曲持续时间:
File file = new File("filename.mp3");
AudioFileFormat baseFileFormat = AudioSystem.getAudioFileFormat(file);
Map properties = baseFileFormat.properties();
Long duration = (Long) properties.get("duration");
You can also use MP3SPI to get at the ID3 tags which might save you an extra dependency.
您还可以使用 MP3SPI 来获取 ID3 标签,这可能会为您节省额外的依赖。
回答by Sjoerd
Use some MP3 library. The MP3 format is not that easy that there is some simple way to read out the length of a song.
使用一些 MP3 库。MP3 格式并不容易,有一些简单的方法可以读出歌曲的长度。

