java中rate_Java中的音频文件长度

我在解决基于它的字节的mp3文件中的数据时遇到问题.

输出的第一部分是正确的,我有一个长达254秒的mp3文件,我从Github的mp3解析库mp3agic获取它的信息.

但是,关于帧长度和持续时间的信息的第二部分是不正确的.

Length of this mp3 is: 254 seconds

Bitrate: 320 kbps (CBR)

Sample rate: 44100 Hz

Has ID3v1 tag?: NO

Has ID3v2 tag?: YES

Has custom tag?: NO

framelength -1

framerate 38.28125

duration -271265.06

我用来获取帧长度,帧速率和持续时间的代码是:

File file = musicFile.getFileValue();

this.audioStream.startMusicStream(file);

try {

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));

System.out.println("framelength "+frameSize);

System.out.println("framerate "+frameRate);

System.out.println("duration "+durationInSeconds);

this.setDurationLabel(durationInSeconds);

} catch (Exception e) {

e.printStackTrace();

}

所有的拳头,为什么帧长和其他测量甚至是负的?那有什么意思?如何使用audioinputstream和audioformat中的信息准确计算mp3文件的持续时间?

最佳答案 对于帧长度和持续时间,您可以使用:

long frameLen = audioInputStream.getFrameLength();

double durationInSeconds = (frameLen+0.0) / format.getFrameRate();

功能说明:

getFrameLength:Obtains the length of the stream, expressed in sample frames rather than bytes.Returns:the length in sample frames

getFrameRate:Obtains the frame rate in frames per second.Returns:the number of frames per second, or AudioSystem.NOT_SPECIFIED

参考文献:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值