1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public static long 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++; } duration += frameCount * 20; } finally { if (randomAccessFile != null) { randomAccessFile.close(); } } return duration; }
|