This article is a control for recording short videos with MediaRecorder. You can set the time, space size, and whether to turn on the camera initially, etc. for video recording. This control is a combination control and inherits from LinearLayout. To prevent errors, the android.media.MediaRecorder.OnErrorListener interface needs to be implemented.
Small video recording interface
MovieRecorderView.java
import java.io.File;import java.io.IOException;import java.util.Timer;import java.util.TimerTask; import android.content.Context;import android.content.res.TypedArray;import android.hardware.Camera ;import android.hardware.Camera.Parameters;import android.media.MediaRecorder;import android.media.MediaRecorder.AudioEncoder;import android.media.MediaRecorder.AudioSource;import android.media.MediaRecorder.OnErrorListener;import android.media.MediaRecorder.OutputFormat;import android.media.MediaRecorder.VideoEncoder;import android.media.MediaRecorder .VideoSource;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;import android.widget.LinearLayout;import android.widget.ProgressBar; import com.contron.dgyj.R;import com.contron.dgyj.common.Globals;import com.contron.dgyj.im.ImGlobal;import com.lidroid.xutils.util.LogUtils; /** * Video playback control* * @author liuyinjun * * @date 2015- 2-5 */public class MovieRecorderView extends LinearLayout implements OnErrorListener { private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private ProgressBar mProgressBar; private MediaRecorder mMediaRecorder; private Camera mCamera; private Timer mTimer; // Timer private OnRecordFinishListener mOnRecordFinishListener; // Recording completion callback interface private int mWidth; // Video resolution width private int mHeight; // Video Resolution height private boolean isOpenCamera;// Whether to turn on the camera from the beginning private int mRecordMaxTime; // The maximum time for one shooting private int mTimeCount; // Time count private File mVecordFile = null; // File public MovieRecorderView(Context context) { this(context, null); } public MovieRecorderView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MovieRecorderView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MovieRecorderView, defStyle, 0); mWidth = a.getInteger(R.styleable .MovieRecorderView_width, 320);// Default 320 mHeight = a.getInteger(R.styleable.MovieRecorderView_height, 240); // Default 240 isOpenCamera = a.getBoolean(R.styleable.MovieRecorderView_is_open_camera, true); // Default open mRecordMaxTime = a.getInteger(R.styleable. MovieRecorderView_record_max_time, 10);//The default is 10 LayoutInflater.from(context).inflate(R.layout.movie_recorder_view, this); mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview); mProgressBar = (ProgressBar) findViewById(R.id .progressBar); mProgressBar.setMax(mRecordMaxTime);// Set the maximum amount of progress bar mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(new CustomCallBack()); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); a.recycle(); } /** * * @author liuyinjun * * @ date 2015-2-5 */ private class CustomCallBack implements Callback { @Override public void surfaceCreated(SurfaceHolder holder) { if (!isOpenCamera) return; try { initCamera(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (!isOpenCamera) return; freeCameraResource(); } } /** * Initialize the camera* * @author liuyinjun * @date 2015-2-5 * @throws IOException */ private void initCamera() throws IOException { if (mCamera != null) { freeCameraResource(); } try { mCamera = Camera.open(); } catch (Exception e) { e.printStackTrace(); freeCameraResource(); } if (mCamera == null) return; setCameraParams(); mCamera.setDisplayOrientation(90); mCamera.setPreviewDisplay( mSurfaceHolder); mCamera.startPreview(); mCamera.unlock(); } /** * Set the camera to vertical screen * * @author liuyinjun * @date 2015-2-5 */ private void setCameraParams() { if (mCamera != null) { Parameters params = mCamera.getParameters(); params.set("orientation" , "portrait"); mCamera.setParameters(params); } } /** * Release camera resources* * @author liuyinjun * @date 2015-2-5 */ private void freeCameraResource() { if (mCamera != null) { mCamera.setPreviewCallback(null); mCamera.stopPreview(); mCamera.lock(); mCamera.release(); mCamera = null; } } private void createRecordDir() { File sampleDir = new File(Environment.getExternalStorageDirectory() + File.separator + "im/video/"); if (!sampleDir.exists()) { sampleDir.mkdirs(); } File vecordDir = sampleDir; // Create file try { mVecordFile = File.createTempFile("recording", ".mp4", vecordDir);//mp4 format LogUtils.i(mVecordFile.getAbsolutePath()); } catch (IOException e) { } } /** * Initialization* * @author liuyinjun * @date 2015-2-5 * @throws IOException * / private void initRecord() throws IOException { mMediaRecorder = new MediaRecorder(); mMediaRecorder.reset(); if (mCamera != null) mMediaRecorder.setCamera(mCamera); mMediaRecorder.setOnErrorListener(this); mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); ;// Video source mMediaRecorder.setAudioSource(AudioSource.MIC);//Audio source mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4);//Video output format mMediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);//Audio format mMediaRecorder.setVideoSize(mWidth, mHeight) ;//Set resolution: // mMediaRecorder.setVideoFrameRate(16);// I removed this, it seems useless. mMediaRecorder.setVideoEncodingBitRate(1 * 1024 * 512);// Set the frame frequency, and then it will be clear. mMediaRecorder.setOrientationHint(90);/ / The output is rotated 90 degrees, keeping vertical screen recording. mMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP); // Video recording format // mediaRecorder.setMaxDuration(Constant.MAXVEDIOTIME * 1000); mMediaRecorder.setOutputFile(mVecordFile.getAbsolutePath()); mMediaRecorder.prepare(); try { mMediaRecorder.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace (); } } /** * Start recording video* * @author liuyinjun * @date 2015-2-5 * @param fileName * Video storage location * @param onRecordFinishListener * Callback interface after the specified time is reached */ public void record(final OnRecordFinishListener onRecordFinishListener) { this.mOnRecordFinishListener = onRecordFinishListener; createRecordDir(); try { if (! isOpenCamera)//If the camera is not turned on, open initCamera(); initRecord(); mTimeCount = 0; // Reassign the time counter mTimer = new Timer(); mTimer.schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub mTimeCount++; mProgressBar. setProgress(mTimeCount);//Set the progress bar if (mTimeCount == mRecordMaxTime) {// When the specified time is reached, stop shooting stop(); if (mOnRecordFinishListener != null) mOnRecordFinishListener.onRecordFinish(); } } }, 0, 1000); } catch (IOException e) { e.printStackTrace(); } } /** * Stop shooting * * @author liuyinjun * @date 2015-2-5 */ public void stop() { stopRecord(); releaseRecord(); freeCameraResource(); } /** * Stop recording* * @author liuyinjun * @date 2015-2-5 */ public void stopRecord() { mProgressBar.setProgress(0); if ( mTimer != null) mTimer.cancel(); if (mMediaRecorder != null) { // It will not crash after setting. mMediaRecorder.setOnErrorListener(null); mMediaRecorder.setPreviewDisplay(null); try { mMediaRecorder.stop(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace (); } catch (Exception e) { e.printStackTrace(); } } } /** * Release resources * * @author liuyinjun * @date 2015-2-5 */ private void releaseRecord() { if (mMediaRecorder != null) { mMediaRecorder.setOnErrorListener(null); try { mMediaRecorder.release() ; } catch (IllegalStateException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } mMediaRecorder = null; } public int getTimeCount() { return mTimeCount; } /** * @return the mVecordFile */ public File getmVecordFile() { return mVecordFile; } /** * Recording completion callback Interface * * @author liuyinjun * * @date 2015-2-5 */ public interface OnRecordFinishListener { public void onRecordFinish(); } @Override public void onError(MediaRecorder mr, int what, int extra) { try { if (mr != null) mr.reset(); } catch (IllegalStateException e) { e.printStackTrace (); } catch (Exception e) { e.printStackTrace(); } }}
movie_recorder_view.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas .android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_dark" android:orientation="vertical"> <SurfaceView android:id="@+id/surfaceview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight ="1" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="2dp" /> </LinearLayout>
attrs.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas .android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_dark" android:orientation="vertical"> <SurfaceView android:id="@+id/surfaceview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight ="1" /> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="2dp" /> </LinearLayout>
The above is the entire content of this article, I hope you all like it.