最近很多同学问我SPS和PPS在那里设置,其实这篇文章只是我 上篇文章的一个简单实现
具体情况情看看上一篇
http://blog.csdn.net/zblue78/archive/2010/12/15/6078040.aspx
这里只用HTC的G7做了H264的程序,谅解!
csdn的资源慢了 粘代码算了
资源 http://download.csdn.net/source/2918751
欢迎大家经常访问
我的blog
http://blog.csdn.net/zblue78/
共同探讨,啥也不说的 直接上码
AndroidManifest.xml
main.xml
initializeVideo();
number = 0;
// 重新启动捕获,以获取视频流
DataInputStream dis=new DataInputStream(fis);
//读取最前面的32个自己的空头
try {
dis.read(buffer,0,32);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
File file = new File(“/sdcard/stream.h264”);
if (file.exists())
file.delete();
raf = new RandomAccessFile(file, “rw”);
} catch (Exception ex) {
Log.v(“System.out”, ex.toString());
}
//这些参数要对应我现在的视频设置,如果想变化的话需要去重新确定,
//当然不知道是不是不同的机器是不是一样,我这里只有一个HTC G7做测试。
byte[] h264sps={0x67,0x42,0x00,0x0C,(byte) 0x96,0x54,0x0B,0x04,(byte) 0xA2};
byte[] h264pps={0x68,(byte) 0xCE,0x38,(byte) 0x80};
byte[] h264head={0,0,0,1};
try {
raf.write(h264head);
raf.write(h264sps);
raf.write(h264head);
raf.write(h264pps);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true)
{
try {
//读取每场的长度
int h264length=dis.readInt();
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;
raf.write(buffer, 0, num);
}
} catch (IOException e) {
break;
}
}
}
}).start();
}
private boolean initializeVideo() {
if (mSurfaceHolder==null)
return false;
mMediaRecorderRecording = true;
if (mMediaRecorder == null)
mMediaRecorder = new MediaRecorder();
else
mMediaRecorder.reset();
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mMediaRecorder.setVideoFrameRate(20);
mMediaRecorder.setVideoSize(352, 288);
mMediaRecorder.setVideoEncoder(mVideoEncoder);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.setMaxDuration(0);
mMediaRecorder.setMaxFileSize(0);
mMediaRecorder.setOutputFile(sender.getFileDescriptor());
try {
mMediaRecorder.setOnInfoListener(this);
mMediaRecorder.setOnErrorListener(this);
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IOException exception) {
releaseMediaRecorder();
finish();
return false;
}
return true;
}
private void releaseMediaRecorder() {
Log.v(TAG, “Releasing media recorder.”);
if (mMediaRecorder != null) {
if (mMediaRecorderRecording) {
try {
mMediaRecorder.setOnErrorListener(null);
mMediaRecorder.setOnInfoListener(null);
mMediaRecorder.stop();
} catch (RuntimeException e) {
Log.e(TAG, “stop fail: ” + e.getMessage());
}
mMediaRecorderRecording = false;
}
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.d(TAG, “surfaceChanged”);
mSurfaceHolder = holder;
if (!mMediaRecorderRecording) {
initializeVideo();
startVideoRecording();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, “surfaceCreated”);
mSurfaceHolder = holder;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, “surfaceDestroyed”);
mSurfaceHolder = null;
}
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
switch (what) {
case MediaRecorder.MEDIA_RECORDER_INFO_UNKNOWN:
Log.d(TAG, “MEDIA_RECORDER_INFO_UNKNOWN”);
break;
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED:
Log.d(TAG, “MEDIA_RECORDER_INFO_MAX_DURATION_REACHED”);
break;
case MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED:
Log.d(TAG, “MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED”);
break;
}
}
@Override
public void onError(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN) {
Log.d(TAG, “MEDIA_RECORDER_ERROR_UNKNOWN”);
finish();
}
}
}