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

Android MediaRecorder H264 编码实时视频流不能播放(readInt()值太大)以及如何将流实时上传到服务器的方法

2013-08-09 21:47 1006 查看
请先仔细阅读/article/1688675.html后再阅读本文

核心是/article/1688675.html,用博主的方法录制视频后许多人发现无法播放,主要问题是 DataInputStream的readInt()方法读取的值太大(之后会提到),readInt()是用来读取四个字节并以十进制表示这四个字节代表的数值

我们先来看看MediaRecorder使用H264编码的具体情况,问题就处在这里



PS: CSDN 的代码排版真是越来越坑爹了

//获取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());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐