请先仔细阅读http://blog.youkuaiyun.com/zblue78/article/details/6083374后再阅读本文
核心是http://blog.youkuaiyun.com/zblue78/article/details/6083374,用博主的方法录制视频后许多人发现无法播放,主要问题是 DataInputStream的readInt()方法读取的值太大(之后会提到),readInt()是用来读取四个字节并以十进制表示这四个字节代表的数值
我们先来看看MediaRecorder使用H264编码的具体情况,问题就处在这里
PS: 优快云 的代码排版真是越来越坑爹了
//获取SPS和PPS
public class GetSPSAndPPS {
private byte[] SPS;
private byte[] PPS;
private final int PARA_SPS = 0;
private final int PARA_PPS = 1;
public GetSPSAndPPS(byte[] in){
int dataLength = in.length;
// 'a'=0x61, 'v'=0x76, 'c'=0x63, 'C'=0x43
byte[] avcC = new byte[] { 0x61, 0x76, 0x63, 0x43 };
// avcC的起始位置
int avcRecord = 0;
for (int ix = 0; ix < dataLength; ++ix) {
if (in[ix] == avcC[0] && in[ix + 1] == avcC[1]
&& in[ix + 2] == avcC[2]
&& in[ix + 3] == avcC[3]) {
// 找到avcC,则记录avcRecord起始位置,然后退出循环。
avcRecord = ix + 4;
break;
}
}
if (0 == avcRecord) {
handleMainThread("Cannot find avvC");
return;
}
int spsStartPos = avcRecord + 6;
byte[] spsbt = new byte[] { in[spsStartPos],
in[spsStartPos + 1] };
int spsLength = bytes2Int(spsbt);
SPS = new byte[spsLength];
spsStartPos += 2;
System.arraycopy(in, spsStartPos, SPS, 0, spsLength);
int ppsStartPos = spsStartPos + spsLength + 1;
byte[] ppsbt = new byte[] { in[ppsStartPos],
in[ppsStartPos + 1] };
int ppsLength = bytes2Int(ppsbt);
PPS = new byte[ppsLength];
ppsStartPos += 2;
System.arraycopy(in, ppsStartPos, PPS, 0, ppsLength);
handleMainThread("Find SPS and PPS!");
}
private int bytes2Int(byte[] bt) {
int ret = bt[0];
ret <<= 8;
ret |= bt[1];
return ret;
}
public byte[] getParameter(int type) {
switch(type){
case PARA_SPS:
return SPS;
case PARA_PPS:
return PPS;
default:return null;
}
}
private void handleMainThread(String s) {
Message msg = new Message();
Bundle b = new Bundle();
b.putString("String", String.valueOf(s));
msg.setData(b);
try {
RAS_MAIN.myhandle.sendMessage(msg);
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
}