import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class GetAmrDuration {
/**
* 得到amr的时长
*
* @param file
* @return
* @throws IOException
*/
public static int getAmrDuration(File file) throws IOException {
long duration = -1;
int[] packedSize = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0,
0, 0 };
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file, "rw");
long length = file.length();// 文件的长度
int pos = 6;// 设置初始位置
int frameCount = 0;// 初始帧数
int packedPos = -1;
byte[] datas = new byte[1];// 初始数据值
while (pos <= length) {
randomAccessFile.seek(pos);
if (randomAccessFile.read(datas, 0, 1) != 1) {
duration = length > 0 ? ((length - 6) / 650) : 0;
break;
}
packedPos = (datas[0] >> 3) & 0x0F;
pos += pack
得到音频文件的播放时间---GetAmrDuration
最新推荐文章于 2022-10-19 10:33:28 发布
该Java代码示例展示了如何通过读取AMR文件的头部信息来计算其播放时长。方法中定义了不同打包大小对应的字节长度,逐帧遍历文件并累计帧数,最后将帧数转换为总时长。主函数调用此方法并打印结果。
摘要由CSDN通过智能技术生成