View preview in recorder for Android

In android, how to show the preview surface before starting recording a media recorder. my application has a video recording function, when I go to the video clip, it displays a black screen when I start recording using the start button. A camera preview is the display and start of recording.

How to start a preview before recording. I added the code that I used in the onCreateView () 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 when I clicked the start button is

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

therefore, I can transcode the video using the code above, but I could not show the preview before starting recording. Please help me where I am absent. To do this, a black screen is displayed before transcoding the video.

Thanks in advance.

+6
source share
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. It worked for me.

+4
source

I ran into the same problem. I looked at him, and here is my activity. It took me a bit of effort to keep it from breaking, so here is the final result. It displays a preview before the user presses the REC button. (I also show the countdown, but not against). Please note that in this example there is too much work in the main thread (when clicked). There are a few things here that are not best practices, but for a working example, I find this 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); } } } 
+2
source

Source: https://habr.com/ru/post/955236/


All Articles