本文主要是介绍android 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);
- }
- }
这篇关于android jaudiotagger读取mp3和Flac音频操作的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!