您的位置:首页 > 移动开发 > Android开发

Android 获取amr音频文件时长

2017-11-07 16:47 976 查看
一个常量方法
/**
* 得到amr的时长
*
* @param file
* @return amr文件时间长度
* @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 += packedSize[packedPos] + 1;
frameCount++;
}
// 帧数*20
duration += frameCount * 20;
} finally {
if (randomAccessFile != null) {
randomAccessFile.close();
}
}
return (int)((duration/1000)+1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: