我需要从相机捕获视频并将其保存到存储卡,我正在使用谷歌教程,但调用时仍然出现错误
mRecorder.setCamera(mCamera); // 4.b.1 step from tutorial
我尝试将其放入try
并查看异常
try {
mRecorder.setCamera(mCamera);
} catch(Exception e) {
Log.d("MyVideoRecord", "Error setting the camera: " + e.getMessage());
System.out.println("CameraPreview::record() - setCamera() thrown an exception");
System.out.println("Exception: "+e.getMessage());
System.out.println("Exception: "+e.getLocalizedMessage());
System.out.println("Exception: "+e.toString());
e.printStackTrace();
return false;
}
LogCat 输出
D/MyVideoRecord(2681): Error setting the camera: null
I/System.out(2681): CameraPreview::record() - setCamera() thrown an exception
I/System.out(2681): Exception: null
I/System.out(2681): Exception: null
I/System.out(2681): Exception: java.lang.NullPointerException
W/System.err(2681): java.lang.NullPointerException
W/System.err(2681): at com.example.myvideorecord.CameraPreview.record(CameraPreview.java:101)
W/System.err(2681): at com.example.myvideorecord.MainActivity.onOptionsItemSelected(MainActivity.java:104)
W/System.err(2681): at android.app.Activity.onMenuItemSelected(Activity.java:2502)
W/System.err(2681): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:969)
W/System.err(2681): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
W/System.err(2681): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
W/System.err(2681): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
W/System.err(2681): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:468)
W/System.err(2681): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:126)
W/System.err(2681): at android.view.View$PerformClick.run(View.java:14263)
W/System.err(2681): at android.os.Handler.handleCallback(Handler.java:605)
W/System.err(2681): at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err(2681): at android.os.Looper.loop(Looper.java:137)
W/System.err(2681): at android.app.ActivityThread.main(ActivityThread.java:4441)
W/System.err(2681): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(2681): at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err(2681): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
W/System.err(2681): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
W/System.err(2681): at dalvik.system.NativeStart.main(Native Method)
D/dalvikvm(2681): Debugger has detached; object registry had 425 entries
看起来mCamera指针从日志中为空,但我不认为是这种情况,因为我之前已经解锁了 mCamera而没有错误。我查看了文档,但没有任何关于setCamera()
返回错误或抛出异常的消息。我在清单中设置了CAMERA
,WRITE_EXTERNAL_STORAGE
和权限。RECORD_AUDIO
我不知道是什么可能导致问题或如何追踪它。有人可以帮我吗?
非常感谢 !:)
这是我的所有代码:
CameraPreview.java
我已经实现record()
(这就是发生错误的地方)和stopRecord()
方法。其余文件来自谷歌教程。
package com.example.myvideorecord;
import java.io.File;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
//http://developer.android.com/guide/topics/media/camera.html#capture-video
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private boolean recording;
private MediaRecorder mRecorder;
public CameraPreview(Context context, Camera camera) {
super(context);
System.out.println("CameraPreview() - Started");
mCamera = camera;
recording = false;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
System.out.println("CameraPreview() - Ended");
}
public void surfaceCreated(SurfaceHolder holder) {
System.out.println("CameraPreview::surfaceCreated() - Started");
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("MyVideoRecord", "Error setting camera preview: " + e.getMessage());
}
System.out.println("CameraPreview::surfaceCreated() - Ended");
}
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("CameraPreview::surfaceDestroyed()");
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
System.out.println("CameraPreview::surfaceChanged() - Started");
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d("MyVideoRecord", "Error starting camera preview: " + e.getMessage());
}
System.out.println("CameraPreview::surfaceChanged() - Ended");
}
public boolean record()
{
// if already recording, return
if( recording ) return false;
// We are recording
recording = true;
// log start of the method
System.out.println("CameraPreview::record() - Method start");
//mCamera.stopPreview();
mCamera.unlock();
//
// !!! THIS IS WHERE ERROR OCCURS !!!
//
//
try {
mRecorder.setCamera(mCamera);
} catch(Exception e) {
Log.d("MyVideoRecord", "Error setting the camera: " + e.getMessage());
System.out.println("CameraPreview::record() - setCamera() thrown an exception");
System.out.println("Exception: "+e.getMessage());
System.out.println("Exception: "+e.getLocalizedMessage());
System.out.println("Exception: "+e.toString());
e.printStackTrace();
return false;
}
// Set media recorder properties
mRecorder.setAudioSamplingRate(MediaRecorder.AudioSource.CAMCORDER);
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mRecorder.setProfile( CamcorderProfile.get( CamcorderProfile.QUALITY_LOW ) );
//mRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
mRecorder.setOutputFile("/sdcard/MVR_video");
// probably not needed
//mRecorder.setPreviewDisplay());
// Prepare media recorder
try {
mRecorder.prepare();
} catch (Exception e) {
Log.d("MyVideoRecord", "Error preparing media recorder: " + e.getMessage());
System.out.println("CameraPreview::record() - prepare() thrown an exception");
return false;
}
mRecorder.start();
System.out.println("CameraPreview::record() - Method returning TRUE");
return true;
}
public void stopRecord() {
System.out.println("CameraPreview::stopRecord() - Method start");
if(recording)
{
mRecorder.stop();
mRecorder.release();
mCamera.lock();
}
}
}
MainActivity.java
package com.example.myvideorecord;
import android.app.Activity;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
// http://developer.android.com/reference/android/app/Activity.html
public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private Camera.Parameters parameters;
private FrameLayout preview;
private SharedPreferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_preview);
// Create an instance of Camera
mCamera = getCameraInstance();
// Get parameters of camera (not neccessary now)
parameters = getCameraParameters(mCamera);
parameters.getFocusMode();
// get preview reference
preview = (FrameLayout) findViewById(R.id.camera_preview);
mPreview = new CameraPreview(this, mCamera);
preferences = getPreferences(MODE_PRIVATE);
}
@Override
public void onStart() {
super.onStart();
// Create our Preview view and set it as the content of our activity.
System.out.println("MainActivity::onCreate() - adding view to preview");
preview.addView(mPreview);
System.out.println("MainActivity::onCreate() - view added");
}
@Override
public void onStop() {
super.onStop();
System.out.println("MainActivity::onStop() - Method start");
// here shoud go something like
// preview.stop
preview.removeView(mPreview);
mPreview.stopRecord();
}
@Override
public void onPause() {
super.onPause();
// Save all needed information
//preferences.edit();
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("MainActivity::onDestroy() - Method start");
mCamera.stopPreview();
releaseCameraInstance(mCamera);
}
/*
*
* MENU
*
* */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
System.out.println("MainActivity::onCreateOptionsMenu() - Method start");
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("MainActivity::onOptionsItemSelected() - Method start");
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_settings:
return true;
case R.id.menu_quit:
this.finish();
return true;
case R.id.menu_record:
mPreview.record();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/*
*
* Camera handling
*
* */
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
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)
System.out.println("getCameraInstance() - excepction");
}
System.out.println("getCameraInstance() - returning camera");
return c; // returns null if camera is unavailable
}
public static Camera.Parameters getCameraParameters(Camera c)
{
System.out.println("getCameraParameters() - getting parameters of camera");
Camera.Parameters par;
par = c.getParameters();
System.out.println("getCameraParameters() - returning parameters of camera");
return par;
}
/* release camera object and log it */
public static void releaseCameraInstance(Camera c){
System.out.println("releaseCameraInstance() - camera released");
c.release();
}
}