曾无好事来相访,赖尔高文一起予。这篇文章主要讲述Android MediaRecorder录制视频提示start failed的解决办法相关的知识,希望能为你提供帮助。
转自https://blog.csdn.net/w815878564/article/details/49247411
今天测试MediaRecorder录制功能的时候出现一个蛋疼的BUG,按照官方提供的示例写好之后,start居然报错,打印日志一看说:start failed。
代码如下:
[java]
view plain
copy
- mCamera = Camera.open(0);
- surfaceView = new SurfaceView(this);
- setContentView(surfaceView);
- mSurfaceHolder = surfaceView.getHolder();
- mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- mRecorder = new MediaRecorder();
- mRecorder.setCamera(mCamera);
- mRecorder.setAudiosource(AudioSource.DEFAULT);
- mRecorder.setVideoSource(VideoSource.DEFAULT);
- mRecorder.setOutputFormat(OutputFormat.MPEG_4);
- mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
- mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
- mRecorder.setVideoSize(320, 240);
- mRecorder.setVideoFrameRate(20);
- mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
- File file = new File(Environment.getExternalStorageDirectory()
- .getAbsolutePath() + "/test.mp4");
- if (file.exists())
- {
- file.delete();
- }
- try
- {
- file.createNewFile();
- } catch (IOException e1)
- {
- e1.printStackTrace();
- }
- mRecorder.setOutputFile(file.getAbsolutePath());
- mSurfaceHolder.addCallback(new Callback()
- {
- @Override
- public void surfaceDestroyed(SurfaceHolder holder)
- {
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder)
- {
- try
- {
- mRecorder.prepare();
- mRecorder.start();
- } catch (Exception e)
- {
- Writer writer = new StringWriter();
- PrintWriter printWriter = new PrintWriter(writer);
- e.printStackTrace(printWriter);
- Throwable cause = e.getCause();
- while (cause != null)
- {
- cause.printStackTrace(printWriter);
- cause = cause.getCause();
- }
- String str = writer.toString();
- System.out.println(str);
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format,
- int width, int height)
- {
- }
- });
[java] view plain copy
- mCamera.getParameters().getSupportedVideoSizes()
【Android MediaRecorder录制视频提示start failed的解决办法】但是有时候即使设置了正确的参数仍旧会出现这个错误,这时候可能你就需要再start之前调用一下下面这个方法了:
[java] view plain copy
- mCamera.unlock();
[java] view plain copy
- mCamera.stopPreview();
- mCamera.setPreviewCallback(null);
- mCamera.setPreviewCallbackWithBuffer(null);
- mCamera.release();
- mCamera = null;
- mCamera = Camera.open(0);
- mCamera.lock();
- mCamera.unlock();
推荐阅读
- Android客户端与本地服务器Socket通信
- [Android] TextView 属性说明
- android studio bug : aidl is missing 解决方案
- 再杀掉app之后 删除NSUserDefault存在本地的数据
- Phalcon查询语言(PHQL)
- 环境部署(Phalcon安装详细步骤)
- Phalcon安全哈希
- Phalcon模型事务
- Phalcon模型行为