我正在尝试了解如何使用相机录制视频。但是在开始录制时总会崩溃。我不知道为什么会这样。我现在不需要使用任何预览,只需记录到文件。这是代码
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并且不要忘记将此surfaceview设置为相机。作为工作示例check this。
答案 0 :(得分:0)
rec.setOutputFormat(MediaRecorder.OutputFormat.WEBM);
是WEBM容器中的数据。尝试用
替换 rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
答案 1 :(得分:0)
对我来说问题是我没有解锁相机。在设置相机解锁之前:
private void prepareCamera(Camera camera) {
...
camera.unlock(); // Unlock the camera
rec.setCamera(camera);
...