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

请先仔细阅读http://blog.csdn.net/zblue78/article/details/6083374后再阅读本文

 

核心是http://blog.csdn.net/zblue78/article/details/6083374,用博主的方法录制视频后许多人发现无法播放,主要问题是 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());
        }
    }
}


 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 35
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 35
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值