UPDATED:
Well, after several days of testing and debugging ... I GOT THE WORK, but not the way I want.
The reason it crashed earlier was due to the camera’s “reorientation” during the screen lock, it seems that a crash often occurs.
As soon as I made him use landscape mode, it works. However, I do not want him to use landscape mode; I want him to work in portrait mode.
The code is taken directly from the sample Android Studio (Media → MediaRecorder). The sample had code working in landscape mode, and I can’t figure out how to get it to use portrait mode so that I can avoid reorientation and thus avoid crashes?
There is nothing in onPause, the onResume code and the pointer to the stack pointed to the called method.
Easy reproduction:
1) Use Android Studio to get an example MediaRecord application 2) In the manifest, change, android: screenOrientation = "landscape"> in Portrait. 3) The application does not start now.
I added mCamera.setDisplayOrientation (90), the same problem.
The code:
@TargetApi(Build.VERSION_CODES.HONEYCOMB) private boolean prepareVideoRecorder(){ // BEGIN_INCLUDE (configure_preview) mCamera = CameraHelper.getDefaultCameraInstance(); // We need to make sure that our preview and recording video size are supported by the // camera. Query camera to find all the sizes and choose the optimal size given the // dimensions of our preview surface. Camera.Parameters parameters = mCamera.getParameters(); List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes(); Camera.Size optimalSize = CameraHelper.getOptimalPreviewSize(mSupportedPreviewSizes, mPreview.getWidth(), mPreview.getHeight()); // Use the same size for recording profile. CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); profile.videoFrameWidth = optimalSize.width; profile.videoFrameHeight = optimalSize.height; // likewise for the camera object itself. parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight); mCamera.setParameters(parameters); try { // Requires API level 11+, For backward compatibility use {@link setPreviewDisplay} // with {@link SurfaceView} mCamera.setPreviewTexture(mPreview.getSurfaceTexture()); } catch (IOException e) { Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage()); return false; } // END_INCLUDE (configure_preview) // BEGIN_INCLUDE (configure_media_recorder) mMediaRecorder = new MediaRecorder(); // Step 1: Unlock and set camera to MediaRecorder mCamera.unlock(); mMediaRecorder.setCamera(mCamera); // Step 2: Set sources mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) mMediaRecorder.setProfile(profile); // Step 4: Set output file mMediaRecorder.setOutputFile(CameraHelper.getOutputMediaFile( CameraHelper.MEDIA_TYPE_VIDEO).toString()); mMediaRecorder.setOrientationHint(90); // END_INCLUDE (configure_media_recorder) MediaScannerConnection.scanFile(this, new String[] { CameraHelper.getOutputMediaFile( CameraHelper.MEDIA_TYPE_VIDEO).getPath() }, new String[] { "video/mp4" }, null); // Step 5: 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; }
Magazines:
06-27 02:18:08.244 25734-25752/com.watchdawg.watchdawg E/MediaRecorder﹕ start failed: -22 06-27 02:18:08.253 25734-25752/com.watchdawg.watchdawg E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask