2

我想阅读 mp3 文件的详细信息,例如持续时间、比特率、艺术家等。我可以通过阅读以下某些行来找到一些信息:

File f=fc.getSelectedFile();
FileInputStream inp=null; 
  byte[] buffer=new byte[128];    
     inp= new FileInputStream(f)
     inp.skip(f.length()-128);
     inp.read(buffer);      

    }
    String str=new String(buffer);       
    tf.setText(str);   

我希望清楚地获取所有细节。我怎样才能做到这一点?

4

2 回答 2

1

使用 MP3SPI 获取持续时间:

private static void getDurationWithMp3Spi(File file) throws UnsupportedAudioFileException, IOException {

AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
if (fileFormat instanceof TAudioFileFormat) {
    Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
    String key = "duration";
    Long microseconds = (Long) properties.get(key);
    int mili = (int) (microseconds / 1000);
    int sec = (mili / 1000) % 60;
    int min = (mili / 1000) / 60;
    System.out.println("time = " + min + ":" + sec);
} else {
    throw new UnsupportedAudioFileException();
}

}

于 2015-10-26T09:09:28.410 回答
0

您可以使用 apache tika Java API 来解析 MP3 的元数据,例如标题、专辑、流派、持续时间、作曲家、艺术家等。所需的 jar 是 tika-parsers-1.4、tika-core-1.4。

示例程序:

package com.parse.mp3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.mp3.Mp3Parser;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class AudioParser {

/**
 * @param args
 */
public static void main(String[] args) {
    String fileLocation = "G:/asas/album/song.mp3";

    try {

    InputStream input = new FileInputStream(new File(fileLocation));
    ContentHandler handler = new DefaultHandler();
    Metadata metadata = new Metadata();
    Parser parser = new Mp3Parser();
    ParseContext parseCtx = new ParseContext();
    parser.parse(input, handler, metadata, parseCtx);
    input.close();

    // List all metadata
    String[] metadataNames = metadata.names();

    for(String name : metadataNames){
    System.out.println(name + ": " + metadata.get(name));
    }

    // Retrieve the necessary info from metadata
    // Names - title, xmpDM:artist etc. - mentioned below may differ based
    System.out.println("----------------------------------------------");
    System.out.println("Title: " + metadata.get("title"));
    System.out.println("Artists: " + metadata.get("xmpDM:artist"));
    System.out.println("Composer : "+metadata.get("xmpDM:composer"));
    System.out.println("Genre : "+metadata.get("xmpDM:genre"));
    System.out.println("Album : "+metadata.get("xmpDM:album"));

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (SAXException e) {
    e.printStackTrace();
    } catch (TikaException e) {
    e.printStackTrace();
    }
    }
}
于 2015-10-26T09:16:18.270 回答