音乐播放器-使用jaudiotagger获得音频元信息
2017-03-18 22:31
176 查看
最近写音乐播放器,但是一直不知道怎么获取音频中的tag信息,最后通过第三方库jaudiotagger解决了问题。
使用TagExplorer查看一首MP3中的tag:
jaudiotagger官网
Tag匹配信息
可以发现大部分歌曲包含并且我们比较需要的信息只有歌名,专辑,歌手,时长,歌曲中内嵌的图片而已,下面给出读取的代码(测试支持flac,MP3,其他自测)
Tag类:
使用TagExplorer查看一首MP3中的tag:
jaudiotagger官网
Tag匹配信息
可以发现大部分歌曲包含并且我们比较需要的信息只有歌名,专辑,歌手,时长,歌曲中内嵌的图片而已,下面给出读取的代码(测试支持flac,MP3,其他自测)
package com.example.util; import java.awt.Image; import java.awt.Toolkit; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.ImageIcon; import org.jaudiotagger.audio.AudioFile; import org.jaudiotagger.audio.exceptions.CannotReadException; import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException; import org.jaudiotagger.audio.exceptions.ReadOnlyFileException; import org.jaudiotagger.audio.flac.FlacFileReader; import org.jaudiotagger.audio.mp3.MP3File; import org.jaudiotagger.tag.FieldKey; import org.jaudiotagger.tag.KeyNotFoundException; import org.jaudiotagger.tag.TagException; import org.jaudiotagger.tag.id3.AbstractID3v2Frame; import org.jaudiotagger.tag.id3.AbstractID3v2Tag; import org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC; import org.jaudiotagger.tag.images.Artwork; import com.example.entity.Tag; /** * @date 2017年3月16日 下午11:47:16 * @version 1.0 * 目前测试支持MP3、flac,其他格式有待测试 */ public class TagInfoUtil { public static Tag Mp3InfoRead(String path){ MP3File file; try { file = new MP3File(path); String songName=file.getID3v2Tag().frameMap.get("TIT2"). c092 toString(); String artist=file.getID3v2Tag().frameMap.get("TPE1").toString(); String album=file.getID3v2Tag().frameMap.get("TALB").toString(); String length=file.getMP3AudioHeader().getTrackLengthAsString(); songName=songName.substring(6, songName.length()-3); artist=artist.substring(6, artist.length()-3); album=album.substring(6, album.length()-3); Tag tag = new Tag(); tag.setSongName(songName); tag.setAlbum(album); tag.setArtist(artist); tag.setLength(length); return tag; } catch (IOException | TagException | ReadOnlyFileException | CannotReadException | InvalidAudioFrameException e) { e.printStackTrace(); throw new RuntimeException("获取Mp3 tag信息出错!"); } // System.out.println("歌名"+songName); // System.out.println("歌手"+singer); // System.out.println("专辑:"+author); } public static Image getMp3Picture(String mp3path){ Image img; try { String url = mp3path; File sourceFile = new File(url); MP3File mp3file = new MP3File(sourceFile); AbstractID3v2Tag tag = mp3file.getID3v2Tag(); AbstractID3v2Frame frame = (AbstractID3v2Frame) tag.getFrame("APIC"); FrameBodyAPIC body = (FrameBodyAPIC) frame.getBody(); byte[] imageData = body.getImageData(); img = Toolkit.getDefaultToolkit().createImage(imageData, 0,imageData.length); ImageIcon icon = new ImageIcon(img); String storePath=mp3path; storePath = storePath.substring(0, storePath.length()-3); storePath+="jpg"; FileOutputStream fos = new FileOutputStream(storePath); fos.write(imageData); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException("读取Mp3图片出错"); } return img; } public static Tag FlacInfoRead(String path){ try { FlacFileReader fileReader=new FlacFileReader(); AudioFile read = fileReader.read(new File(path)); org.jaudiotagger.tag.Tag tag = read.getTag(); String songName = tag.getFirst(FieldKey.TITLE); String artist = tag.getFirst(FieldKey.ARTIST); String album=tag.getFirst(FieldKey.ALBUM); int trackLength = read.getAudioHeader().getTrackLength(); int min=trackLength/60; int second=trackLength%60; String length=min+":"+second; System.out.println("长度:"+length); Tag tag2 = new Tag(); tag2.setSongName(songName); tag2.setArtist(artist); tag2.setAlbum(album); return tag2; // System.out.println(songName); // System.out.println(artist); // System.out.println(album); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException("读取Flac信息时出错!"); } } public static Image getFlacPicture(String flacpath){ try { FlacFileReader fileReader=new FlacFileReader(); AudioFile read = fileReader.read(new File(flacpath)); org.jaudiotagger.tag.Tag tag = read.getTag(); Artwork firstArtwork = tag.getFirstArtwork(); byte[] imageData = firstArtwork.getBinaryData(); Image image=Toolkit.getDefaultToolkit().createImage(imageData, 0,imageData.length); ImageIcon icon = new ImageIcon(image); String storePath=flacpath; storePath = storePath.substring(0, storePath.length()-4); storePath+="jpg"; System.out.println(storePath); FileOutputStream fos = new FileOutputStream(storePath); fos.write(imageData); fos.close(); return image; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("读取Flac图片时出错!"); } } }
Tag类:
package com.example.entity; /** * @date 2017年3月15日 下午2:26:49 * @version 1.0 * @Description: Tag信息实体类 */ public class Tag { private String SongName; private String Artist; private String Album; private String length; public String getSongName() { return SongName; } public String getArtist() { return Artist; } public void setArtist(String artist) { Artist = artist; } public void setSongName(String songName) { SongName = songName; } public String getAlbum() { return Album; } public void setAlbum(String album) { Album = album; } public String getLength() { return length; } public void setLength(String length) { this.length = length; } }
相关文章推荐
- 使用第三方Java类库jaudiotagger完成Flac音频文件metadata(元数据)的读和修改
- 音乐播放器,注意静态常量的使用命名要有明确具体的含义(查看API,若想获得音乐本身自带的歌手名以及图片等信息,可查看MediaMetadataRetrieve来获得详细信息)
- C++编程中,利用WINDOWS API获得系统状态信息[CPU占用率,硬盘使用情况,内存使用情况]
- 在C++中使用cpuid指令获得CPU信息(转载闲人)
- 使用sp_who获得特定处理或过程的信息
- 使用WMI获得硬盘的信息
- 使用dbms_rowid包获得rowid的详细信息
- 一个获得计算机基本信息的类库使用的实例
- 学习笔记 -- 使用WMI获得系统信息
- symbian如何获得更多错误信息内容--ErrRD在真机上的使用
- 使用webservice获得天气,基金,股票等信息
- 使用 WMI 获得计算机信息
- 使用ASP获得服务器网卡的MAC地址信息
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第十三节 获得目的地IP对应的物理地址
- 在C++中使用cpuid指令获得CPU信息
- 使用C#获得系统信息
- 使用Ajax技术获得服务端信息时的注意事项
- 使用WMI获得硬盘的信息
- 使用C#中的DirectorySearcher来获得活动目录中的组织结构与用户等信息,并在展示成树形结构(附源代码)
- 使用 StackTrace 获得更多跟 Exception 有关的信息