茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672024 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00004.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

爪哇咖啡屋 : [轉貼]How do I get a sound file's total time in Java?

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]How do I get a sound file's total time in Java?
How do I get a sound file's total time in Java?

--UPDATE

Looks like this code does de work: long audioFileLength = audioFile.length();
recordedTimeInSec = audioFileLength / (frameSize * frameRate);

I know how to get the file length, but I'm not finding how to get the sound file's frame rate and frame size... Any idea or link?

-- UPDATE

One more working code (using @mdma's hints):
    AudioInputStream 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));


--------------------------------------------------------------------------------

Given a File you can write
File file = ...;
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long frames = audioInputStream.getFrameLength();
double durationInSeconds = (frames+0.0) / format.getFrameRate();


--------------------------------------------------------------------------------

This is a easy way:
FileInputStream fileInputStream = null;
long duration = 0;

try {
    fileInputStream = new FileInputStream(pathToFile);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

try {
    duration = Objects.requireNonNull(fileInputStream).getChannel().size() / 128;
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println(duration)


原文出處:How do I get a sound file's total time in Java? - Stack Overflow
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Get the Time Duration of an Audio File (.mp3)

Get the Time Duration of an Audio File (.mp3)


Here, I will show you how to get the time duration for an Audio File playing in a Java Desktop App. You should know the following things first:

You need to use

The Code:


private void playMe(){
try{
File file=new File("F:\\Net Beans Work Space\\mp3\\a.mp3");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
int duration=0;
AudioFile audioFile = AudioFileIO.read(file);
duration= audioFile.getAudioHeader().getTrackLength();
System.out.print("time in milliseconds= "+duration) ;
player.play();
}catch(Exception e){
System.out.print("ERROR "+e);
}
}

 


原文出處:Get the Time Duration of an Audio File (.mp3) | Java Development Tools-Help Blog
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|