Mediarecorder can't record video from front camera only landscape mode android

402 views Asked by At

I am developing app which rectors video from back camera and front camera using media recorder. The issue I am facing is whenever I try to record video from front camera in landscape mode the video is not getting recorded at all, the portrait mode records well without any issue.

I tested this code on many devices, the devices running jellybean run the code successfully and video gets recorded, but the devices running kitkat(samsung Note 2) and above can't record the video from front camera when in landscape mode.

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_480P);
            profile.videoFrameWidth = optimalSize.width;
            profile.videoFrameHeight = optimalSize.height;
    enter code here
            // likewise for the camera object itself.
            parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
            mCamera.setParameters(parameters);
            if (isCamera) {
                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);
            int orianton = getScreenOrientation();
            String str = getRotation(Capture_video.this);
            if (orianton == 1) {
                if (cameraFront) {
                    mMediaRecorder.setOrientationHint(270);
                } else {
                    mMediaRecorder.setOrientationHint(90);
                }

            } else if (orianton == 2) {


                if (str.equals("landscape")) {
                    mMediaRecorder.setOrientationHint(0);

                } else {
                    mMediaRecorder.setOrientationHint(180);
                }

            } else {
                mMediaRecorder.setOrientationHint(0);
            }

            // Step 2: Set sources
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            //mMediaRecorder.setOrientationHint(rotation);
            // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
            mMediaRecorder.setProfile(profile);
            // mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            mMediaRecorder.setVideoEncodingBitRate(690000);
            mMediaRecorder.setVideoFrameRate(20);
            // Step 4: Set output file
            mMediaRecorder.setOutputFile(CameraHelper.getOutputMediaFile(
                    CameraHelper.MEDIA_TYPE_VIDEO).toString());
            // END_INCLUDE (configure_media_recorder)

            // 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;


try {

                    mMediaRecorder.start();

                }catch (Exception e){
                    e.printStackTrace();
                }


  // dimensions of our preview surface.




And below is my logcat during error..please give me response as soon as possible..






 java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.RuntimeException: start failed.
            at android.media.MediaRecorder.start(Native Method)
            at com.mansainfotech.Camera.Capture_video$MediaPrepareTask.doInBackground(Capture_video.java:656)
            at com.mansainfotech.Camera.Capture_video$MediaPrepareTask.doInBackground(Capture_video.java:648)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
06-23 18:31:59.731  28697-28697/com.mansainfotech.sportseed E/ActivityThread﹕ Activity com.mansainfotech.Camera.Capture_video has leaked IntentReceiver com.mansainfotech.Camera.UpdateService@42ce0dd8 that was originally registered here. Are you missing a call to unregisterReceiver()?
    android.app.IntentReceiverLeaked: Activity com.mansainfotech.Camera.Capture_video has leaked IntentReceiver com.mansainfotech.Camera.UpdateService@42ce0dd8 that was originally registered here. Are you missing a call to unregisterReceiver()?
            at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:809)
            at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:610)
            at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1816)
            at android.app.ContextImpl.registerReceiver(ContextImpl.java:1796)
            at android.app.ContextImpl.registerReceiver(ContextImpl.java:1790)
            at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:479)
            at com.mansainfotech.Camera.Capture_video.onCreate(Capture_video.java:146)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.access$900(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
0

There are 0 answers