从昨天开始,我一直在玩媒体记录器,但我无法绕过相机错误 100。我一直在
08-13 11:23:24.952: I/System.out(2418): Going to get the camera
08-13 11:23:25.092: I/System.out(2418): This is the camera android.hardware.Camera@4051b208
08-13 11:23:25.273: I/System.out(2418): Surface was created
08-13 11:23:25.282: I/System.out(2418): Stop preview worked android.hardware.Camera@4051b208
08-13 11:23:25.282: I/System.out(2418): Surface changed
08-13 11:23:31.302: I/System.out(2418): This is the camera android.hardware.Camera@40520d20
08-13 11:23:31.345: I/System.out(2418): Trying to save picture
08-13 11:23:31.352: I/System.out(2418): About to preview
08-13 11:23:31.352: I/System.out(2418): Preview worked
08-13 11:23:31.352: I/MediaRecorderJNI(2418): prepare: surface=0x2a8198 (identity=216)
08-13 11:23:31.362: I/System.out(2418): Starting to record
08-13 11:23:32.512: W/IMediaDeathNotifier(2418): media server died
08-13 11:23:32.512: W/Camera(2418): Camera server died!
08-13 11:23:32.512: W/Camera(2418): ICamera died
08-13 11:23:32.522: I/System.out(2418): Stop preview worked android.hardware.Camera@40520d20
08-13 11:23:32.522: I/System.out(2418): Surface changed
08-13 11:23:32.592: E/Camera(2418): Error 100
我不明白怎么做。我在创建媒体记录器并解锁它之前释放了相机。只是无法绕过它。这就是我所做的。
public class MainActivity extends Activity {
private Camera mCamera;
private MediaRecorder mMediaRecorder;
private CameraPreview mPreview;
private static final String TAG = "Message";
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
private boolean isRecording = false;
Button captureButton;
FrameLayout preview;
FrameLayout myCameraPreview;
private MyCameraSurfaceView myCameraSurfaceView;
private MyCameraSurfaceView myCameraView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preview);
System.out.println("Going to get the camera");
mCamera = getCameraInstance();
//mMediaRecorder = new MediaRecorder();
/* mPreview = new CameraPreview(this, mCamera);
preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);*/
// setPreviewCallback(null);
myCameraSurfaceView = new MyCameraSurfaceView(this, mCamera);
myCameraPreview = (FrameLayout)findViewById(R.id.camera_preview);
myCameraPreview.addView(myCameraSurfaceView);
captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from MediaRecorder
// inform the user that recording has stopped
captureButton.setText("Capture");
isRecording = false;
} else {
// initialize video camera
if (prepareVideoRecorder()) {
System.out.println("Starting to record");
// Camera is available and unlocked, MediaRecorder is prepared,
// now you can start recording
//releaseCamera();
mMediaRecorder.start();
// inform the user that recording has started
captureButton.setText("Stop");
isRecording = true;
} else {
// prepare didn't work, release the camera
System.out.println("Recording failed");
releaseMediaRecorder();
// inform user
}
}
}
}
);
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
System.out.println("This is the camera "+c);// attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
System.out.println("Didn't get the camera ");
}
return c; // returns null if camera is unavailable
}
private boolean prepareVideoRecorder(){
releaseCamera();
//preview.removeAllViews();
mCamera = getCameraInstance();
mMediaRecorder = new MediaRecorder();
myCameraSurfaceView.mCamera=mCamera;
//myCameraView = new MyCameraSurfaceView(this, mCamera);
//myCameraPreview = (FrameLayout)findViewById(R.id.camera_preview);
//myCameraPreview.addView(myCameraView);
// Step 1: Unlock and set camera to MediaRecorder
//mCamera.lock();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
// Step 5: Set the preview output
System.out.println("About to preview");
mMediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
System.out.println("Preview worked");
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
@Override
protected void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it first
releaseCamera(); // release the camera immediately on pause event
}
private void releaseMediaRecorder(){
if (mMediaRecorder != null) {
mMediaRecorder.reset(); // clear recorder configuration
mMediaRecorder.release(); // release the recorder object
mMediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
private void releaseCamera(){
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;
public MyCameraSurfaceView(Context context, Camera camera) {
super(context);
mCamera = camera;
// 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);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int weight,
int height) {
// 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();
System.out.println("Stop preview worked "+mCamera);
} catch (Exception e){
// ignore: tried to stop a non-existent preview
System.out.println("tried to stop a non-existent preview "+mCamera);
}
// make any resize, rotate or reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
System.out.println("Surface changed");
} catch (Exception e){
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
System.out.println("Surface was created");
} catch (IOException e) {
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
}
我现在很困惑。