0

There must be something I'm missing witht this CamcorderProfile as I keep getting "11-15 01:00:53.185: E/MediaRecorder(976): setOutputFormat called in an invalid state: 1 " on "recorder.setProfile(profile);"

package com.apress.proandroidmedia.ch1.cameraintent;

import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class JustRecord extends Activity implements SurfaceHolder.Callback{

    SurfaceView cameraView;
    SurfaceHolder surfaceHolder;
    Camera camera;
    MediaRecorder recorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        cameraView = (SurfaceView) findViewById(R.id.CameraView);
        surfaceHolder = cameraView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.addCallback(this);

    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub

        try{
            camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
            camera.setDisplayOrientation(90);

        }
        catch (Exception exception){
            camera.release();
            exception.printStackTrace();
        }


        recorder = new MediaRecorder();
        recorder.setCamera(camera);

        CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_FRONT, CamcorderProfile.QUALITY_HIGH);
        if(profile != null) {
            recorder.setProfile(profile);  

        }else {
            //default to basic H263 and AMR_NB if profile not found
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);       
            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
        }


        recorder.setOutputFile("/sdcard/videocapture_example.mp4");
        recorder.setMaxDuration(5000); // 50 seconds

        prepareRecorder();
        recorder.start();

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        //camera.stopPreview();
        recorder.stop();
        recorder.release();
        camera.release();
    }

    private void prepareRecorder() {
        recorder.setPreviewDisplay(surfaceHolder.getSurface());

        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            finish();
        } catch (IOException e) {
            e.printStackTrace();
            finish();
        }
    }

}

Any ideas? Thanks!

EDIT: I might as well add that the code works just fine if I type in the codecs and such. It's just that I cant determine the best quality for some reason.

JustAGuy
  • 5,151
  • 11
  • 41
  • 55
  • Please note that the quoted code is wrong. The integer index used for `Camera.open()` is ***not*** one of the constants in `Camera.CameraInfo` class. – Alex Cohn May 14 '17 at 09:01

1 Answers1

2

I managed to solve that one by actually starting to think for a change...

When you look at the MediaRecorder flow chart the first thing you see is the AudioSource and VideoSource defined. Moreover, those 2 are obviously NOT defined when using a CamcorderProfile. Yet after defining those 2 lines MediaRecorder still refused to start. What was missing here is "camera.unlock()" as my camera was "taken" sort of speak on previous tests.

JustAGuy
  • 5,151
  • 11
  • 41
  • 55