Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preview display in android media recorder

In android how to show preview surface before media recorder start. my app have video recording functionality,when i navigate to video recording fragment it display black screen,when i start recording using start button camera preview is display and recording start.

how to start preview before recording. i have added code that i used in onCreateView() of fragment-

surfaceHolder = mySurfaceView.getHolder();
camera = Camera.open();
if(camera!=null){
    camera.setDisplayOrientation(90);
    Camera.Parameters param;
    param = camera.getParameters();
    param.setPreviewFrameRate(20);
    param.setPreviewSize(176, 144);
    camera.setParameters(param);
    camera.setPreviewDisplay(surfaceHolder);
}

mediaRecorder = new MediaRecorder();
camera.unlock();
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile
            .get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(camcorderProfile_HQ);
String filePath = getOutputMediaFile(MEDIA_TYPE_VIDEO).getPath();
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
mediaRecorder.setOutputFile(filePath);

and the code that i used on start button click-

mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.prepare();
mediaRecorder.start();

so i am able to recode video by using above code,but not able to show preview before recording start. Please help me where i am missing.for that black screen is displayed before recoding video.

Thanks in advance.

like image 807
Ravi Bhandari Avatar asked Oct 04 '13 08:10

Ravi Bhandari


2 Answers

public void surfaceCreated(SurfaceHolder holder) {
        if (mCamera != null) {
            Parameters params = mCamera.getParameters();
            mCamera.setParameters(params);
            try {
                //mCamera.setDisplayOrientation(90);
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                e.printStackTrace();
            }
            mCamera.startPreview();
        }
    }

If you implement SurfaceHolder.Callback, override the surfaceCreated method like this. This worked for me.

like image 198
Programming Pirate Avatar answered Sep 29 '22 10:09

Programming Pirate


I ran into the same problem. I looked it up and here's my Activity. It took me a bit of effort to make it not crash, so here's the final result. It displays the preview before the user clicks the REC button. (I am also displaying a countdown, but don't mind that). Notice also that there is too much work done on the main thread in this example (when clicking). There are a few things here that aren't best practices, but for a working example I think it's good enough.

import android.app.Activity;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;

public class MainActivity extends Activity implements SurfaceHolder.Callback {

    private static final String LOG_TAG = MainActivity.class.getCanonicalName();
    Button myButton;
    MediaRecorder mediaRecorder;
    SurfaceHolder surfaceHolder;
    boolean recording;
    private TextView mTimer;
    private Camera mCamera;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        recording = false;
        mediaRecorder = new MediaRecorder();
        mCamera = Camera.open();
        initMediaRecorder();

        SurfaceView myVideoView = (SurfaceView) findViewById(R.id.videoview);
        surfaceHolder = myVideoView.getHolder();
        surfaceHolder.addCallback(this);

        myButton = (Button) findViewById(R.id.mybutton);
        myButton.setOnClickListener(myButtonOnClickListener);

        mTimer = (TextView) findViewById(R.id.timer);
    }

    private Button.OnClickListener myButtonOnClickListener
            = new Button.OnClickListener() {

        @Override
        public void onClick(final View arg0) {
            CountDownTimer countDownTimer = new CountDownTimer(90 * 1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    mTimer.setText(String.valueOf(millisUntilFinished / 1000));
                }

                @Override
                public void onFinish() {
                    onClick(arg0);
                }
            };

            if (recording) {
                // Stop recording and finish
                try {
                    mediaRecorder.stop();
                    mediaRecorder.reset();
                    mediaRecorder.release();
                    finish();
                } catch (Exception e) {
                    Log.e(LOG_TAG, "Failed to stop recorder", e);
                }

            } else {
                // Release the camera and start recording
                mCamera.release();
                countDownTimer.start();
                mediaRecorder.start();
                recording = true;
                myButton.setText("STOP");
            }
        }
    };


    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (mCamera != null) {
            Camera.Parameters params = mCamera.getParameters();
            mCamera.setParameters(params);
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                e.printStackTrace();
            }
            mCamera.startPreview();
        }
        prepareMediaRecorder();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {

    }

    private void initMediaRecorder() {
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_CIF);
        mediaRecorder.setProfile(camcorderProfile_HQ);
        File file = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), "myvideo.mp4");
        if (file.exists()) {
            file.delete();
        }
        mediaRecorder.setOutputFile(file.getAbsolutePath());
        mediaRecorder.setMaxDuration(90000); // Set max duration 90 sec.
        mediaRecorder.setMaxFileSize(15000000); // Set max file size 15M
    }

    private void prepareMediaRecorder() {
        mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

        try {
            mediaRecorder.prepare();
        } catch (IllegalStateException | IOException e) {
            Log.e(LOG_TAG, "Failed to prepare recorder", e);
        }
    }
}
like image 43
Ariel Cabib Avatar answered Sep 29 '22 09:09

Ariel Cabib