取得 音效檔的長度,
Wav:
可以是用[1] 的方式,計算出音效檔的長度,程式碼:
private Void GetLength(String filename) { // ex "C:\\Documents and Settings\\Administrator\\My Documents\\My Music\\my.wav"
File file = new File(filename);
AudioInputStream audioInputStream;
try {
audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));
System.out.println( " Length :"+durationInSeconds);
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
MP3:
利用 MP3SPI[2],這個LGPL 的 函式取得 mp3 檔案的標籤,其中就有音效檔的長度
參考:
[1]: How do I get a mp3 file's total time in Java?, http://stackoverflow.com
[2]: MP3SPI
沒有留言:
張貼留言