Android使用MediaRecorder来做实时视频通讯

u014662721 2015-04-25 07:14:04
最近由于项目需要, 得实现实时的音视频传输, 今天查了一天的资料, 知道可以用MediaRecorder来捕捉视频, 然后使用mMediaRecorder.setOutputFile(...)通过LocalSocket来获得视频流, 但是到这一步之后就不知道该怎么去做了,。。。

网上查资料, 说要自己进行编码, 然后再解码, 也去看过相关的代码, 但是并没有关于如何将编码后的数据传输出去...




public void run() {
int frame_size = 1024;
byte[] buffer = new byte[1024 * 32];
int num, number = 0;
InputStream fis = null;
try {
fis = sender.getInputStream();
} catch (IOException e1) {
e1.printStackTrace();
return;
}
try {
Thread.currentThread().sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
number = 0;
// releaseMediaRecorder();
// 如果是H264或是MPEG_4_SP的就要在这里找到相应的设置参数的流
// avcC box H264的设置参数
// esds box MPEG_4_SP 的设置参数
// 其实 如果分辨率 等数值不变的话,这些参数是不会变化的,
// 那么我就只需要在第一次运行的时候确定就可以了
// while (true) {
// try {
// num = fis.read(buffer, number, frame_size);
// number += num;
// if (num < frame_size) {
// break;
// }
// } catch (IOException e) {
// break;
// }
// }

// prepareVideoRecorder();
number = 0;
// 重新启动捕获,以获取视频流
DataInputStream dis = new DataInputStream(fis);
int h264length = 0;
// 读取最前面的32个自己的空头
try {
dis.read(buffer, 0, 32);
h264length = dis.readInt();// 1718773093
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

try {
File file = new File("/sdcard/stream.mp4");
if (file.exists())
file.delete();
raf = new RandomAccessFile(file, "rw");
bab = new ByteArrayBuffer(1024 * 64);
} catch (Exception ex) {
Log.v("System.out", ex.toString());
}

// 这些参数要对应我现在的视频设置,如果想变化的话需要去重新确定,
// 当然不知道是不是不同的机器是不是一样,我这里只有一个HTC G7做测试。
byte[] h264sps = new byte[] { 0x67, 0x42, 0x00, 0x0C,
(byte) 0x96, 0x54, 0x0B, 0x04, (byte) 0xA2 };
byte[] h264pps = new byte[]{ 0x68, (byte) 0xCE, 0x38,
(byte) 0x80 };
byte[] h264head = new byte[]{ 0, 0, 0, 1 };
try {
bab.append(h264head, 0, h264head.length);
bab.append(h264sps, 0, h264sps.length);
bab.append(h264head, 0, h264head.length);
bab.append(h264pps, 0, h264pps.length);

raf.write(h264head);
raf.write(h264sps);
raf.write(h264head);
raf.write(h264pps);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{

// RTPPacketSender packetSender = new RTPPacketSender(os)
// RTPSocket rtpSocket = new RTPSocket();
// SendStreamEvent sendStreamEvent = new SendStreamEvent(from, stream, participant)
// rtpSocket.
// rtpSocket.connect();
//rtpSocket.set
//PushSourceStream pushSourceStream = rtpSocket.getOutputStream();
// pushSourceStream.read(arg0, arg1, arg2);
}catch(Exception e){
e.printStackTrace();
}
while (true) {
try {
// 读取每场的长度
number = 0;
raf.write(h264head);
while (number < h264length) {
int lost = h264length - number;
num = fis.read(buffer, 0,
frame_size < lost ? frame_size
: lost);
Log.d(TAG, String.format(
"H264 %d,%d,%d", h264length,
number, num));
number += num;
bab.append(buffer, 0, num);
byte[] bs = bab.toByteArray();
raf.write(buffer, 0, num);
}
} catch (IOException e) {
break;
}
}
}


这里的代码是将视频保存到SD卡中,但是我需要的是实时传输到另一台android手机上, 所以想请教一下各位大牛, 请问我该怎么做呢?
...全文
332 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
u014662721 2015-04-25
  • 打赏
  • 举报
回复
自顶自顶。。。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧