3

I've been trying to get MediaRecorder to work following the official example and although I am able to make a file it is rotated 90 degrees clockwise...

Now I am trying this in portrait mode and have indeed rotated the previewsurface 90 degrees and locked it to portrait mode...

I have no idea how to fix this and get a portrait oriented video and have tried the myriad of solutions related to this topic all to no avail...

Code:

public class CameraRecorder {

private Camera cam;
private MediaRecorder mMediaRecorder;
private CameraPreview mPreview;
private static Context mContext;

public CameraRecorder(CameraPreview preview, Context context){

    mPreview = preview;
    cam = mPreview.getCamera();
    //cam.getParameters().setRotation(0);
    mContext = context;

}

public boolean prep(){


    mMediaRecorder = new MediaRecorder();
    //mMediaRecorder.setOrientationHint(90);

    // Step 1: Unlock and set camera to MediaRecorder
    cam.unlock();
    mMediaRecorder.setCamera(cam);

    // Step 2: Set sources
    //mMediaRecorder.setAudioSource(mContext.openFileInput());
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);


    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    //mMediaRecorder.setVideoSize(mPreview.getMeasuredWidth(), mPreview.getMeasuredHeight());
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

    //mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile().toString());

    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d("Exception", "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d("Exception", "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (Exception e) {
        Log.d("Exception", "Exception preparing MediaRecorder: " + e.getMessage());
        return false;
    }
    return true;

}

public static File getOutputMediaFile(){

    File mediaFile;

    mediaFile = new File(mContext.getCacheDir() + File.separator + "vid.mp4");

    return mediaFile;


}

public void releaseMediaRecorder(){

    if (mMediaRecorder != null) {
        mMediaRecorder.reset();   // clear recorder configuration
        mMediaRecorder.release(); // release the recorder object
        mMediaRecorder = null;
        cam.lock();           // lock camera for later use
    }

}

public void startRecording(){

    mMediaRecorder.start();

}

public void stopRecording(){

    try {
        mMediaRecorder.stop();  // stop the recording
        releaseMediaRecorder(); // release the MediaRecorder object
        cam.lock();
    } catch (Exception e) {
        Log.d("Exception","Exiting with exception: " + e.getMessage());
    }



}



}

I've tried various combinations of the commented methods with no results (setting that particular videosize results in a "try to delete broken file" error" so I've left that well alone)... I am intentionally ignoring the audiosource by the way as I just need the video (I need to splice in audio later, and indeed no good idea where to start with that either)

Any tips, ideas or pointers are greatly appreciated!

user1504495
  • 666
  • 8
  • 17

1 Answers1

1

You cannot rotate the video. Just the preview surface or still images. But not video stream.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Oh, cheers...Any way I can rotate the file after saving it? And maybe add a custom audio track? (A push in the right direction is all I need) – user1504495 Sep 03 '12 at 19:25
  • Sure you can postprocess it, but note it is not light task - rotating may require some time (not even real time) computing power and may eat the battery, but yes, it is doable. Just google for "java video post process" or "video effect" or "recoding" etc. – Marcin Orlowski Sep 03 '12 at 19:27
  • 1
    Thanks again, I'll accept your answer and hope I'll manage :) – user1504495 Sep 03 '12 at 19:30