MediaRecorder:启动失败:-19
Posted
技术标签:
【中文标题】MediaRecorder:启动失败:-19【英文标题】:MediaRecorder: start failed: -19 【发布时间】:2016-12-30 13:52:21 【问题描述】:我正在尝试了解如何使用相机录制视频。但它总是在开始录制时崩溃。而且我不知道为什么会这样。我现在不需要使用任何预览,只需记录到文件中。这是代码
public static final String TAG = "tag";
Button recBut;
private MediaRecorder rec;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
releaseCamera();
recBut = (Button) findViewById(R.id.rec);
camera = Camera.open(0);
prepareCamera(camera);
recBut.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if (recBut.getText().toString().equalsIgnoreCase("start"))
rec.start(); //logcat say that problem here
recBut.setText("RECING...");
else
rec.stop();
rec.reset();
rec.release();
rec = null;
recBut.setText("START");
releaseCamera();
);
private void prepareCamera(Camera camera)
List<Camera.Size> list = camera.getParameters().getSupportedVideoSizes();
Log.i(TAG, "GENERAL SIZE - " + list.size());
for (int i = 0; i < list.size(); i++)
Log.i(TAG, "SIZE| width - " + list.get(i).width + " hight - " + list.get(i).height);
rec = new MediaRecorder();
camera.getParameters().setRecordingHint(true);
rec.setCamera(camera);
rec.setAudiosource(MediaRecorder.AudioSource.DEFAULT);
rec.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
rec.setOutputFormat(MediaRecorder.OutputFormat.WEBM);
rec.setVideoSize(720, 1280);
rec.setVideoFrameRate(24);
rec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
rec.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
// rec.setProfile(CamcorderProfile.get(0, CamcorderProfile.QUALITY_HIGH));
rec.setOutputFile(
Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_MOVIES + "/test.mp4");
rec.setPreviewDisplay(null);
try
rec.prepare();
catch (IOException e)
e.printStackTrace();
private void releaseCamera()
if (camera != null)
camera.release();
camera = null;
清单,以防万一:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lexz.basiccamera">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat:
E/MediaRecorder: start failed: -19
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lexz.basiccamera, PID: 28879
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.lexz.basiccamera.MainActivity$1.onClick(MainActivity.java:35)
at android.view.View.performClick(View.java:4802)
at android.view.View$PerformClick.run(View.java:20101)
at android.os.Handler.handleCallback(Handler.java:810)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
解决方案
看起来正确的工作相机类需要有 SurfaceView、SurfaceHolder 并实现 SurfaceHolder.Callback 并且不要忘记将此表面视图设置为相机。作为工作示例check this。
【问题讨论】:
【参考方案1】:对我来说,问题是我没有解锁相机。设置相机前先解锁:
private void prepareCamera(Camera camera)
...
camera.unlock(); // Unlock the camera
rec.setCamera(camera);
...
【讨论】:
【参考方案2】:rec.setOutputFormat(MediaRecorder.OutputFormat.WEBM);
它是 WEBM 容器中的数据。尝试替换为
rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
【讨论】:
不,事实并非如此。看看我上面的解决方案。以上是关于MediaRecorder:启动失败:-19的主要内容,如果未能解决你的问题,请参考以下文章
Android 无法使用前置摄像头录制视频,MediaRecorder 启动失败:-19
从 MIC 录制并流式传输到 TCP 服务器; MediaRecorder:启动失败:-38
MediaRecorder.stop() 停止失败:-1007