Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:57:25+00:00 2026-06-17T16:57:25+00:00

I am doing video recording with media recorder. For this i used below code

  • 0

I am doing video recording with media recorder.

For this i used below code .

private void prepareMediaRecorder(boolean vsize) {
        mrec = new MediaRecorder();
        mrec.setOnErrorListener(new OnErrorListener() {

            @Override
            public void onError(MediaRecorder mr, int what, int extra) {
                // TODO Auto-generated method stub
                if (extra == -1007)
                {
                    prepareMediaRecorder(false);
                }
                else
                {
                unableToRecord();
                }
            }
        });

        camera.lock();
        camera.unlock();
        mrec.setCamera(camera);
        mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
        mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        if (vsize)
            mrec.setVideoSize(getMaxSupportedVideoSize().width,
                    getMaxSupportedVideoSize().height);
        else
        mrec.setVideoSize(640, 480);

        mrec.setOutputFile(path + filename);
        mrec.setMaxDuration(30000);
        mrec.setPreviewDisplay(surfaceHolder.getSurface());

        if (!onlyback
                && currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) {
            if (open_camera == 1)
                mrec.setOrientationHint(270);
            else
                mrec.setOrientationHint(90);
        } else if (currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) {
            mrec.setOrientationHint(90);
        }
        try {
            mrec.prepare();
            mrec.start();


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

In the above code, when media recorder error listener called I am recreating media recorder with other video size but while doing this I am getting camera lock exception getting.

how can I solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:57:27+00:00Added an answer on June 17, 2026 at 4:57 pm

    This is how I tackle the problem of attempting to use certain video recording sizes that may or may not be available on different devices. I start by attempting to record with an ideal resolution, and use try-catch blocks in order to shut down and retry opening the video camera at a different resolution. This will likely not be a huge issue with future versions of android due to increased support for CamcorderProfiles, but as of now there are many devices that simply do not accurately share available video resolutions.

    I call the initializeVideoRecorder fn from an activity and I use a custom class I created called a PreviewSize, which is basically just a width and height neatly packaged. I defined these constant PreviewSizes above.

    I think the important thing is to release everything and retry prior to attempting to connect to a different preview size. I do this using the releaseVideoCamera function. You are also calling lock prior to unlock which seems potentially problematic (maybe not though).

    Anyways here is my code. I have removed some portions, and left out certain functions unrelated to your needs:

    /**Standard 720p video size (1280x720) for both front and back.*/
    private static final PreviewSize DEFAULT_VIDEO_SIZE = new PreviewSize(720, 1280);
    /**Fallback 480p video size (720x480) for back cameras that don't support 720p */
    private static final PreviewSize BACKUP_VIDEO_SIZE = new PreviewSize(480, 720);
    /** Default pre-ICS front facing camera size (a version of 480p) */
    private static final PreviewSize PRE_ICS_DEFAULT_FRONT_VIDEO_SIZE = new PreviewSize(480, 640);
    
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public boolean initializeVideoRecorder(){
    
            if (! externalStorageAvailable()) return false;
    
            mCamera.stopPreview();
    
            if (ApiHelper.PRE_ICS && mCameraSide == CameraUtils.FRONT_FACING_CAMERA){
                return initializeVideoRecorderWithoutCamcorderProfile(PRE_ICS_DEFAULT_FRONT_VIDEO_SIZE);
            } else return initializeVideoRecorderWithoutCamcorderProfile(DEFAULT_VIDEO_SIZE);
    
        }
    
        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        private boolean initializeVideoRecorder(PreviewSize videoSize){
            mCamera.unlock();
            mVideoRecorder = new MediaRecorder();
            mVideoRecorder.setCamera(mCamera);
            mVideoRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
            mVideoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    
            CamcorderProfile dummyCamcorderProfile;
            if (Build.VERSION.SDK_INT > 8) dummyCamcorderProfile = CamcorderProfile.get(mCameraSide, CamcorderProfile.QUALITY_HIGH);
            else dummyCamcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    
            customSetProfile(dummyCamcorderProfile);
    
            mVideoRecorder.setVideoSize(videoSize.height, videoSize.width);
    
                //Set the orientation hint here if need be... i left that code out
    
            mVideoFile = someFunctionThatGivesYouAVideoFile();
            mVideoRecorder.setOutputFile(mVideoFile.toString());
            mVideoRecorder.setPreviewDisplay(yourCameraSurface);
    
            try {
                mVideoRecorder.prepare();
            } catch (IllegalStateException e) {
                releaseVideoCamera();
                return false;
            } catch (IOException e) {
                Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
                releaseVideoCamera();
                return false;
            }
    
            try {
                mVideoRecorder.start();
            } catch (RuntimeException e) {
                releaseVideoCamera();
                if (videoSize.equals(DEFAULT_VIDEO_SIZE)) {
                    if (mCameraSide == CameraUtils.BACK_FACING_CAMERA) return initializeVideoRecorder(BACKUP_VIDEO_SIZE);
                    else return initializeVideoRecorder(PRE_ICS_DEFAULT_FRONT_VIDEO_SIZE);
                }
                else return false;
            }
    
            return true;
        }
    
      public void customSetProfile(CamcorderProfile profile) {
            mVideoRecorder.setOutputFormat(profile.fileFormat);
            mVideoRecorder.setVideoFrameRate(profile.videoFrameRate);
            mVideoRecorder.setVideoEncoder(profile.videoCodec);
            mVideoRecorder.setVideoEncodingBitRate(1000000);
            mVideoRecorder.setAudioEncodingBitRate(profile.audioBitRate);
            mVideoRecorder.setAudioChannels(profile.audioChannels);
            mVideoRecorder.setAudioSamplingRate(profile.audioSampleRate);
            mVideoRecorder.setAudioEncoder(profile.audioCodec);
        }
    
      public void releaseVideoCamera(){
            mVideoRecorder.reset();
            mVideoRecorder.release();
            mVideoRecorder = null;
            try {
                mCamera.reconnect();
            } catch (IOException e) {
                    // TODO: handle this exception...
            }
            mCamera.lock();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to destroy all i-frames of a video. Doing this I want to
When I play a video doing this: NSString *videoFilepath = [[NSBundle mainBundle] pathForResource:@bacon ofType:@mov];
I stumbled across this youtube video here http://www.youtube.com/watch?v=Ha5LficiSJM that demonstrates someone doing color detection
I'm doing video processing tasks and one of the problems I need to solve
I am doing Video decoding (mpeg2, frame size 360x240) on Android phone with ARM-Cortex
I'm doing one research on video encoding tools for flv. I tested flvtool2 and
What I'm doing : I need to play audio and video files that are
I'm doing some processing on some very large video files (often up to 16MP),
I am doing something like this- http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_video_js_prop but I want to replace the buttons
Doing the getting started of Sinatra. I get this error: ./sinatra.rb:5: undefined method `get'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.