2012-07-05 119 views
2

我想通過閱讀某些行類似下面讀取MP3文件的詳細信息,如時間,比特率,藝術家等 我能找到一些信息:Java的I/O讀取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); 

我希望所有的細節都能清楚地提取出來。我怎樣才能做到這一點?

+1

http://stackoverflow.com/questions/119404/time-length-of-an-mp3-file –

+0

我如何可以手動找到一個mp3文件的長度 – dpanshu

+0

http://www.autohotkey.com/community/viewtopic.php?t=29420 –

回答

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

}

0

您可以使用元數據從MP3解析如標題,專輯,流派,duraion,作曲家阿帕奇蒂卡的Java API ,藝術家等。需要的罐子是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(); 
    } 
    } 
}