我想了解如何使用相機錄製視頻。但開始錄製時總是崩潰。我不知道爲什麼會發生。我現在不需要使用任何預覽,只需要記錄到文件。下面是代碼MediaRecorder:啓動失敗:-19
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)
SOLUTION
貌似正常工作相機類需要有SurfaceView,SurfaceHolder並實現SurfaceHolder.Callback和不要忘記將此曲面視圖設置爲相機。作爲工作示例check this。
不,事實並非如此。看看我上面的解決方案。 – Arthur