如何在 Java 中获取声音文件的总时间? [英] How do I get a sound file's total time in Java?
本文介绍了如何在 Java 中获取声音文件的总时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Java 中获取声音文件的总时间?
How do I get a sound file's total time in Java?
--更新
看起来这段代码确实有效:long audioFileLength = audioFile.length();
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?
-- 更新
另外一个工作代码(使用@mdma 的提示):
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));
推荐答案
给定一个 File
你可以写
File file = ...;
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long frames = audioInputStream.getFrameLength();
double durationInSeconds = (frames+0.0) / format.getFrameRate();
这篇关于如何在 Java 中获取声音文件的总时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文