2015-05-12 110 views
2

我想獲得音頻文件的持續時間(總時間),以秒爲單位。音頻文件可以是任何格式I.E. (.mp3,.wav,.wma等)。如何獲得音頻文件的持續時間?

try { 
    File file = new File("C:/Users/Administrator/AppData/Local/Temp/" + 
     "01 - Ab Tere Bin Jee Lenge Hum-(MyMp3Singer.com).mp3"); 
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file); 
    AudioFormat format = audioInputStream.getFormat(); 
    long frames = audioInputStream.getFrameLength(); 
    double durationInSeconds = (frames + 0.0)/format.getFrameRate(); 
    System.out.println("duration in seconds"+durationInSeconds); 
    AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(file); 
    Map<String, Object> properties = audioFileFormat.properties(); 
    System.out.println("properties"+properties.size()+"empy::"+properties.isEmpty()); 
    for(String key: properties.keySet()){ 
     System.out.println(key +" :: "+ properties.get(key).toString()); 
    } 
} catch (Exception e) { 
    System.out.println("Exception:::::" + e.getMessage()); 
} 

我試圖做到這一點使用上面的代碼,但它給:

Exception:::::could not get audio input stream from input file 
+0

http://stackoverflow.com/questions/3009908/how-do-i-get-a-sound-files-total-time-in-java –

回答

1

你可能錯過了MP3編解碼器。你可能想嘗試Tritonus: Open Source Java Sound

同時檢查相關話題:MP3 playback using Java Sound and Sun MP3 plugin

+0

是如果是,請給出示例程序以獲得音頻文件的持續時間 – programminglover

+0

@programminglover: - 您編寫的程序是正確的。您只需將'jl.jar mp3spi.jar和tritonus_share.jar等jar文件添加到我的類路徑到您的類路徑 –

+0

另請參閱**服務提供程序接口**&** MP3解碼支持** [ Java Sound Wiki](http://stackoverflow.com/tags/javasound/info)。 JMF有一個MP3解碼器。在運行時類路徑中包含'mp3plugin.jar'(name?),你應該設置好。我將它用於基於Java Sound的MP3自動存儲塔。 –

相關問題