Java使用Jaudiotagger读取Mp3及Flac音频操作

Jaudiotagger是jid3lib的扩展,比jlid3lib强大更多,支持更多格式。

MP3信息的读取:

 

try {
	MP3File file = new MP3File("mmt/sdcard2/Music/大艺术家.mp3");
	

	String songName=file.getID3v2Tag().frameMap.get("TIT2").toString();
	String singer=file.getID3v2Tag().frameMap.get("TPE1").toString();
	String author=file.getID3v2Tag().frameMap.get("TALB").toString();
	System.out.println(new String(songName.getBytes("ISO-8859-1"),"GB2312"));
	System.out.println(new String(singer.getBytes("ISO-8859-1"),"GB2312"));
	System.out.println(new String(author.getBytes("ISO-8859-1"),"GB2312"));
} catch (IOException e) {
	e.printStackTrace();
} catch (TagException e) {
	e.printStackTrace();
} catch (ReadOnlyFileException e) {
	e.printStackTrace();
} catch (InvalidAudioFrameException e) {
	e.printStackTrace();
}

 这样输出的格式为:

 

 

Text="大艺术家"; 
Text="蔡依林"; 
Text="MUSE"; 

 

下面是用Jaudiotagger提取MP3图片,并将图片显示出来的代码:

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC;

public class Main {

    public static void main(String args[]) throws IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {

        String url = "D:\\Paparazzi.mp3";
        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();
        Image img=Toolkit.getDefaultToolkit().createImage(imageData, 0,imageData.length);
        System.out.println("img----" + imageData);
        ImageIcon icon = new ImageIcon(img);            
        FileOutputStream fos = new FileOutputStream("D://test1.jpg");
        fos.write(imageData);
        fos.close();
    
        System.out.println("width:"+icon.getIconWidth());
        System.out.println("height:"+icon.getIconHeight());
        getImg(icon);
    }

    public static void getImg(ImageIcon img){
        JFrame f = new JFrame();
        JLabel l = new JLabel();
        l.setIcon(img);
        l.setVisible(true);
        f.add(l);
        f.setSize(500, 500);    
        f.setVisible(true);
    }

}

 关于jaudiotagger完成Flac音频文件metadata(元数据)的读和修改,参考:

http://www.cnblogs.com/once/p/3734755.html

你可能感兴趣的:(java)