我如何在Java中的声音文件的总时间? [英] How do I get a sound file's total time in Java?
本文介绍了我如何在Java中的声音文件的总时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何获得一个声音文件的总时间在Java中?
- 更新
看起来这code确实去工作:
长audioFileLength = audioFile.length();
recordedTimeInSec = audioFileLength /(框架尺寸*帧率);
我知道如何获取该文件的长度,但我没有找到如何让声音文件的帧速率,帧大小...任何想法或链接?
- 更新
还有一个工作code(使用@摇头丸的提示):
的AudioInputStream的AudioInputStream = AudioSystem.getAudioInputStream(文件);
AudioFormat的格式= audioInputStream.getFormat();
长audioFileLength = file.length();
INT框架尺寸= format.getFrameSize();
浮帧率= format.getFrameRate();
浮动durationInSeconds =(audioFileLength /(框架尺寸*帧率));
解决方案
给定一个文件
你可以写
文件文件= ...;
的AudioInputStream的AudioInputStream = AudioSystem.getAudioInputStream(文件);
AudioFormat的格式= audioInputStream.getFormat();
长帧= audioInputStream.getFrameLength();
双durationInSeconds =(帧+ 0.0)/ format.getFrameRate();
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();
这篇关于我如何在Java中的声音文件的总时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文