0

i am doing a application to record video.I got many samples that are working but issue is while recording camera is not opening in portrait mode, it is opening another mode This is the sample i have used including xml file:

package com.example.RecordVideo;

    import java.io.File;

    import android.app.Activity;
    import android.graphics.Camera;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Surface;
    import android.view.SurfaceHolder;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.MediaController;
    import android.widget.VideoView;
    public class MainActivity extends Activity implements SurfaceHolder.Callback, OnClickListener {
    private MediaRecorder recorder = null;
    private static final String OUTPUT_FILE = "/sdcard/videooutput.mp4";
    private static final String TAG = "RecordVideo";
    private VideoView videoView = null;
    private Button startBtn = null;
    Button endBtn;
    Button playRecordingBtn;
    Button stpPlayingRecordingBtn;
    SurfaceHolder mholder;
    File outFile ;
    Camera camera;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recordvideo);

    startBtn = (Button) findViewById(R.id.bgnBtn);
    startBtn.setOnClickListener(this);
    endBtn = (Button) findViewById(R.id.stpBtn);
    endBtn.setOnClickListener(this);
    endBtn.setEnabled(false);
    playRecordingBtn = (Button) findViewById(R.id.playRecordingBtn);
    playRecordingBtn.setOnClickListener(this);
    stpPlayingRecordingBtn =(Button) findViewById(R.id.stpPlayingRecordingBtn);
    stpPlayingRecordingBtn.setOnClickListener(this);
    videoView = (VideoView)this.findViewById(R.id.videoView);
    playRecordingBtn.setEnabled(false);
    stpPlayingRecordingBtn.setEnabled(false);
    mholder = videoView.getHolder();
    mholder.addCallback(this);
    mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        }
    //  @Override
        public void surfaceCreated(SurfaceHolder holder) {
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);    
        startBtn.setEnabled(true);
        }
    //  @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
        }
    //  @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
        Log.v(TAG, "Width x Height = " + width + "x" + height);
        }
        private void playRecording() {
        MediaController mc = new MediaController(this);
        videoView.setMediaController(mc);
        videoView.setVideoPath(OUTPUT_FILE);
        videoView.start();
        }
        private void stopPlayingRecording() {
        videoView.stopPlayback();
        }
        private void stopRecording() throws Exception {
        if (recorder != null) {
        recorder.stop();
        }
        }
        @Override
        protected void onDestroy() {
        super.onDestroy();
        if (recorder != null) {
        recorder.release();
        }
        }
        private void beginRecording(SurfaceHolder holder) throws Exception {
        if(recorder!=null)
        {
        recorder.stop();
        recorder.release();

        }
        outFile = new File(OUTPUT_FILE);
        if(outFile.exists())
        {
        outFile.delete();
        }
        try {

        recorder = new MediaRecorder();
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setVideoSize(480, 580);
        recorder.setVideoFrameRate(15);

        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setMaxDuration(60000*10); // limit to 10 minutes
        recorder.setPreviewDisplay(mholder.getSurface());
        recorder.setOutputFile(OUTPUT_FILE);
        recorder.prepare();
        recorder.start();
        }
        catch(Exception e) {
        Log.e(TAG, e.toString());     
        e.printStackTrace();
        }
        }
        public Surface getSurface()
        {
            return mholder.getSurface();
        }
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v.equals(startBtn))
            {
                try {
                    endBtn.setEnabled(true);
                    startBtn.setEnabled(false);
                    playRecordingBtn.setEnabled(false);
                    stpPlayingRecordingBtn.setEnabled(false);
                    beginRecording(mholder);
                    } catch (Exception e) {
                    Log.e(TAG, e.toString());
                    e.printStackTrace();
                    }
            }
            if(v.equals(endBtn))
            {
                try {
                    startBtn.setEnabled(false);
                    endBtn.setEnabled(false);
                    playRecordingBtn.setEnabled(true);
                    stpPlayingRecordingBtn.setEnabled(false);
                stopRecording();
                } catch (Exception e) {
                Log.e(TAG, e.toString());
                e.printStackTrace();
                }
            }
            if(v.equals(playRecordingBtn))
            {
                try {
                    startBtn.setEnabled(false);
                    endBtn.setEnabled(false);
                    playRecordingBtn.setEnabled(false);
                    stpPlayingRecordingBtn.setEnabled(true);
                playRecording();
                } catch (Exception e) {
                Log.e(TAG, e.toString());
                e.printStackTrace();
                }
            }
            if(v.equals(stpPlayingRecordingBtn))
            {
                try {
                    startBtn.setEnabled(false);
                    endBtn.setEnabled(false);
                    playRecordingBtn.setEnabled(true);
                    stpPlayingRecordingBtn.setEnabled(false);
                stopPlayingRecording();
                } catch (Exception e) {
                Log.e(TAG, e.toString());
                e.printStackTrace();
                }
            }
        }
    }

XML file is:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <VideoView
        android:id="@+id/videoView"
        android:layout_width="480px"
        android:layout_height="580px"
        android:keepScreenOn="true" 
        />
        <LinearLayout 
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="80px"
 >
        <Button
 android:id="@+id/bgnBtn"
 android:layout_width="150px"
 android:layout_marginLeft="80px"
 android:layout_height="wrap_content"
 android:text="Start"
 />
<Button
 android:id="@+id/stpBtn"
 android:layout_width="150px"
  android:layout_marginLeft="40px"
 android:layout_height="wrap_content"
 android:text="Cancel"
 />
 </LinearLayout>
 <LinearLayout 
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="80px"
 >
        <Button
 android:id="@+id/playRecordingBtn"
 android:layout_width="150px"
 android:layout_marginLeft="80px"
 android:layout_height="wrap_content"
 android:text="Play"
 />
<Button
 android:id="@+id/stpPlayingRecordingBtn"
 android:layout_width="150px"
  android:layout_marginLeft="40px"
 android:layout_height="wrap_content"
 android:text="Stop"
 />
 </LinearLayout>
</LinearLayout>

i add permissions also, What i want is 1. video recording should be opened in PORTRAIT mode 2. is video cropping possible in android

Please Help me. Thanks in advance

1 Answer 1

0

dude, u need some fix because this bug was(and will be always) already( i think it's bug ) u need add code like this to surfaceChanged()

if(getResources().getConfiguration().orientation== Configuration.ORIENTATION_PORTRAIT)
            {
                camera.setDisplayOrientation(90);
                p.setRotation(90);
            }
            else
            {
                p.setRotation(0);
                camera.setDisplayOrientation(0);
            }

easy code... and maybe stupied but it's fixed this problem. p.s. from 2.2 i think it's work. if < than code not help u

4
  • Thank u for reply, can you can tell me what is camera. and p.(on adding to surfaceChanged my code showing errors on that places where camera and p is there)
    – Abhi
    Jul 27, 2011 at 11:04
  • oh sorry for this ; p = (Camera.Parameters p = camera.getParameters();) and camera it's camera = Camera.open();/
    – Peter
    Jul 27, 2011 at 11:09
  • When i add Camera.open() it is showing "The method open() is undefined for the type Camera" what would be the problem
    – Abhi
    Jul 27, 2011 at 11:13
  • Means my code is not helpful? Then what will be solution plz?
    – Abhi
    Jul 27, 2011 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.