如何在Java中获取声音文件的总时间?
--更新
看起来这段代码确实起作用了: long audioFileLength = audioFile.length();
recordedTimeInSec = audioFileLength / (frameSize * frameRate);
我知道如何获得文件长度,但我不知道如何获得声音文件的帧率和帧大小……有什么想法或链接吗?
--更新
另一个工作代码(使用@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));
发布于 2010-06-10 05:11:15
给定一个File
,您就可以编写
File file = ...;
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long frames = audioInputStream.getFrameLength();
double durationInSeconds = (frames+0.0) / format.getFrameRate();
发布于 2018-09-16 06:07:26
这是一种简单的方法:
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)
https://stackoverflow.com/questions/3009908
复制相似问题