为什么视频保存到本地是空的文件,是不是与Camera冲突还是什么的,搞了几天了,求帮助,该如何解决呢?
public class SocketCamera extends SurfaceView implements
Camera.PreviewCallback, SurfaceHolder.Callback {
public SocketCamera(Context arg0) {
super(arg0);
}
private SurfaceHolder mSurfaceHolder = null;
private Camera mCamera = null;
private File myRecAudioFile;
private File dir;
private MediaRecorder recorder;
public String sdcardDir = "/mnt/extsd/video/";
/** 服务器名称 */
private String pUsername = "XZY";
/** 视频质量 */
private int VideoQuality = 85;
/** 发送视频宽度比例 */
private float VideoWidthRatio = 100;
/** 发送视频高度比例 */
private float VideoHeightRatio = 100;
/** 发送视频分辨率宽度 */
private int VideoWidth = 0;
/** 发送视频分辨率高度 */
private int VideoHeight = 0;
/** 视频格式索引 */
private int VideoFormatIndex = 0;
public void init(int Width, int Height) {
VideoHeight = Height;
VideoWidth = Width;
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
VideoWidthRatio = VideoWidthRatio / 100f;
VideoHeightRatio = VideoHeightRatio / 100f;
new MySendCommondThread("PHONECONNECT|" + pUsername + "|").start();
if (recorder == null) {
recorder = new MediaRecorder();
}
InitCamera();
recorder();
}
private void InitCamera() {
try {
mCamera = Camera.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void recorder() {
dir = new File(sdcardDir);
if (!dir.exists()) {
dir.mkdir();
}
try {
myRecAudioFile = File
.createTempFile("SP"+ new SimpleDateFormat("yyyyMMdd_hhmmss").format(new Date(System.currentTimeMillis())),".3gp", dir);// 创建临时文件
// recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //
// 录音源为麦克风
// recorder.setMaxDuration(10000);// 最大期限
recorder.setCamera(mCamera);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // 从照相机采集视频
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);// 设置视频编码方式
recorder.setVideoSize(320, 240);
recorder.setVideoFrameRate(20); // 每秒20帧
// recorder.setPreviewDisplay(mSurfaceHolder.getSurface());// 预览
recorder.setOutputFile(myRecAudioFile.getAbsolutePath());// 保存路径
recorder.prepare();
recorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (mCamera == null) {
return;
}
mCamera.stopPreview();
mCamera.setPreviewCallback(this);
Camera.Parameters parameters = mCamera.getParameters();
Size size = parameters.getPreviewSize();
VideoWidth = size.width;
VideoHeight = size.height;
VideoFormatIndex = parameters.getPreviewFormat();
mCamera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
if (mCamera != null) {
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
if (null != mCamera) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
// TODO Auto-generated method stub
try {
if (data != null) {
YuvImage image = new YuvImage(data, VideoFormatIndex,
VideoWidth, VideoHeight, null);
if (image != null) {
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0,
(int) (VideoWidthRatio * VideoWidth),
(int) (VideoHeightRatio * VideoHeight)),
VideoQuality, outstream);
outstream.flush();
Thread th = new MySendFileThread(outstream, pUsername,
Constant.SRVIP, 8511);
th.start();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}