简体   繁体   中英

Java I/O mp3 file read

I want to read details of a mp3 file such as duration, bitrate, artist etc. I am able to find some information by reading certain lines like below:

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);   

I want all the details to be fetched clearly. How can I achieve this?

Get duration Using 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();
}

}

You can use apache tika Java API for meta-data parsing from MP3 such as title, album, genre, duraion, composer, artist and etc.. required jars are tika-parsers-1.4, tika-core-1.4.

Sample Program:

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();
    }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM