對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2019/1/26 4:44:20
How do I get a sound file's total time in Java?
--UPDATE
Looks like this code does de work: long audioFileLength = audioFile.length();
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):
--------------------------------------------------------------------------------
Given a File you can write
--------------------------------------------------------------------------------
This is a easy way:
原文出處:How do I get a sound file's total time in Java? - Stack Overflow
--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