如何在Java中获得声音文件的总时间?
--update
看起来这段代码可以正常工作:long audioFileLength = audioFile.length();
recordedTimeInSec = audioFileLength / (frameSize * frameRate);
Run Code Online (Sandbox Code Playgroud)
我知道如何获取文件长度,但我没有找到如何获得声音文件的帧速率和帧大小...任何想法或链接?
- 更新
一个工作代码(使用@ mdma的提示):
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));
Run Code Online (Sandbox Code Playgroud)
mdm*_*dma 26
鉴于File你可以写
File file = ...;
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long frames = audioInputStream.getFrameLength();
double durationInSeconds = (frames+0.0) / format.getFrameRate();
Run Code Online (Sandbox Code Playgroud)