我正在制作一款使用视频录制功能的应用。我在开发者网站上建议使用视频录制代码,但我的应用程序在MediaRecorder.start()
处抛出运行时异常。
请问任何人都可以找出我的代码中的错误吗?
这是我的java文件: -
public class CustomCameraActivity extends Activity implements
SurfaceHolder.Callback {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
MediaRecorder mrec = new MediaRecorder();
LayoutInflater controlInflater = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getActionBar().hide();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
// surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.custom, null);
LayoutParams layoutParamsControl = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
Button recBtn = (Button) findViewById(R.id.Button03);
recBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// releaseCamera();
// camera = getCameraInstance();
try {
releaseCamera();
startRecording();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (previewing) {
camera.stopPreview();
previewing = false;
}
if (camera != null) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
setCameraDisplayOrientation(CustomCameraActivity.this,
CameraInfo.CAMERA_FACING_BACK, camera);
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();
} catch (Exception e) {
// TODO: handle exception
Log.i("Cam open", "" + e);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
private void setCameraDisplayOrientation(Activity activity, int cameraId,
android.hardware.Camera camera) {
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay()
.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
} else { // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
}
protected void startRecording() throws IOException {
camera = getCameraInstance();
mrec = new MediaRecorder(); // Works well
// camera.unlock();
mrec.setCamera(camera);
// mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mrec.setOutputFile("/sdcard/zSe.3gp");
mrec.setPreviewDisplay(surfaceHolder.getSurface());
// camera.release();
mrec.prepare();
mrec.start();
Toast.makeText(getApplicationContext(), "Recording started", 0).show();
}
protected void stopRecording() {
mrec.stop();
mrec.release();
camera.release();
}
private void releaseMediaRecorder() {
if (mrec != null) {
mrec.reset(); // clear recorder configuration
mrec.release(); // release the recorder object
mrec = null;
// camera.lock(); // lock camera for later use
}
}
private void releaseCamera() {
if (camera != null) {
camera.release(); // release the camera for other applications
camera = null;
}
}
private Camera getCameraInstance() {
// TODO Auto-generated method stub
Camera c = null;
try {
c = Camera.open(); // attempt to get a
// Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
Log.e("camera object", "" + c);
return c; // returns null if camera is unavailable
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
releaseMediaRecorder();
camera.stopPreview();
releaseCamera();
}
}
Logcat: -
09-03 10:11:54.891: E/AndroidRuntime(1199): FATAL EXCEPTION: main
09-03 10:11:54.891: E/AndroidRuntime(1199): java.lang.RuntimeException: start failed.
09-03 10:11:54.891: E/AndroidRuntime(1199): at android.media.MediaRecorder.start(Native Method)
09-03 10:11:54.891: E/AndroidRuntime(1199): at com.coderzheaven.pack.CustomCameraActivity.startRecording(CustomCameraActivity.java:164)
09-03 10:11:54.891: E/AndroidRuntime(1199): at com.coderzheaven.pack.CustomCameraActivity$1.onClick(CustomCameraActivity.java:66)
09-03 10:11:54.891: E/AndroidRuntime(1199): at android.view.View.performClick(View.java:4084)
09-03 10:11:54.891: E/AndroidRuntime(1199): at android.view.View$PerformClick.run(View.java:16966)
09-03 10:11:54.891: E/AndroidRuntime(1199): at android.os.Handler.handleCallback(Handler.java:615)
09-03 10:11:54.891: E/AndroidRuntime(1199): at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 10:11:54.891: E/AndroidRuntime(1199): at android.os.Looper.loop(Looper.java:137)
09-03 10:11:54.891: E/AndroidRuntime(1199): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-03 10:11:54.891: E/AndroidRuntime(1199): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 10:11:54.891: E/AndroidRuntime(1199): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 10:11:54.891: E/AndroidRuntime(1199): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-03 10:11:54.891: E/AndroidRuntime(1199): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-03 10:11:54.891: E/AndroidRuntime(1199): at dalvik.system.NativeStart.main(Native Method)
提前致谢。
答案 0 :(得分:0)
如果要将其与MediaRecorder一起使用,则必须解锁相机。 所以试试这个:
camera.unlock();
将相机设置为MediaRecorder之前
mrec.setCamera(照相机);