Android supports video recording and playback and android recording.
To achieve a similar small video function, you can record a video and then play the video.
Video Recording, using a custom control.
<Span style = "font-size: 14px; ">/*** Video Recording Control ** @ author lip ** @ date 2015-3-16 */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 mOnRecordFin IshListener; // callback interface for recording completion private int mWidth; // video resolution width private int mHeight; // video resolution height private boolean isOpenCamera; // whether to enable the camera private int mRecordMaxTime at the beginning; // The maximum time of one shot is private int mTimeCount; // The time count private File mVecordFile = null; // file public MovieRecorderView (Context context) {this (context, null);} public MovieRecorderView (Context context, AttributeSet attrs) {this (context, attrs, 0);} @ Targ EtApi (Build. public MovieRecorderView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); mWidth = 320; mHeight = 240; isOpenCamera = true; mRecordMaxTime = 10; layoutInflater. from (context ). inflate (R. layout. moive_recorder_view, this); mSurfaceView = (SurfaceView) findViewById (R. id. surfaceview); mProgressBar = (ProgressBar) findViewById (R. id. progres SBar); mProgressBar. setMax (mRecordMaxTime); // set the maximum number of mSurfaceHolder = mSurfaceView progress bars. getHolder (); mSurfaceHolder. addCallback (new CustomCallBack (); mSurfaceHolder. setType (SurfaceHolder. response);}/***** @ 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 lip * @ date 2015-3-16 * @ 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 a portrait screen ** @ author lip * @ date 2015-3-16 */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/"); File sampleDir = new File (Environment. getExternalStorageDirectory () + File. separator + "RecordVideo/"); // File sampleDir = new File ("/video /"); If (! SampleDir. exists () {sampleDir. mkdirs ();} File vecordDir = sampleDir; // create a File. try {mVecordFile = File. createTempFile ("recording ",". mp4 ", vecordDir); // mp4 format // LogUtils. I (mVecordFile. getAbsolutePath (); Log. d ("Path:", mVecordFile. getAbsolutePath ();} catch (IOException e) {}}/*** initialize ** @ author lip * @ date 2015-3-16 * @ throws IOException */private void initRecord () throws IOException {mMed IaRecorder = new MediaRecorder (); mMediaRecorder. reset (); if (mCamera! = Null) mMediaRecorder. setCamera (mCamera); mMediaRecorder. setOnErrorListener (this); mMediaRecorder. setPreviewDisplay (mSurfaceHolder. getSurface (); mMediaRecorder. setVideoSource (VideoSource. CAMERA); // The video source mMediaRecorder. setAudioSource (AudioSource. MIC); // audio source mMediaRecorder. setOutputFormat (OutputFormat. MPEG_4); // The video output format is mMediaRecorder. setAudioEncoder (AudioEncoder. AMR_NB); // audio format: mMediaRecorder. setVid EoSize (mWidth, mHeight); // sets the resolution: // mMediaRecorder. setVideoFrameRate (16); // I have removed it, and it seems that mMediaRecorder is useless. setVideoEncodingBitRate (1*1024*512); // set the frame frequency, and then the mMediaRecorder is clear. setOrientationHint (90); // The output rotates 90 degrees to keep the portrait screen recording mMediaRecorder. setVideoEncoder (VideoEncoder. MPEG_4_SP); // video recording format // mediaRecorder. setMaxDuration (Constant. MAXVEDIOTIME * 1000); mMediaRecorder. setOutputFile (mVecordFile. getAbsolute Path (); 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 * reaches the specified time callback interface */public void record (final OnRecordFinishListe Ner onRecordFinishListener) {this. mOnRecordFinishListener = onRecordFinishListener; createRecordDir (); try {if (! IsOpenCamera) // if the camera is not enabled, enable initCamera (); initRecord (); mTimeCount = 0; // The time counter value is 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) {// mMediaRecorder is not collapsed after the setting. setOnErrorListener (null); mMediaRecorder. setPreviewDisplay (null); try {mMediaRecorder. stop ();} catch (IllegalStateException e) {e. printStackTrace ();} catch (RuntimeException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace () ;}}/ *** release resource *** @ 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;}/*** callback interface for recording completion ** @ author lip ** @ date 2015-3-16 * /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 () ;}}</span>
Video Playback:
<Span style = "font-size: 14px;"> public class MainActivity extends ActionBarActivity {private MovieRecorderView movieRV; private Button startBtn; private Button stopBtn; private Button playBtn; private Button pauseBtn; private SurfaceView playView; private MediaPlayer player; int position; public MainActivity () {}@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initViews (); initEvents (); init ();} private void init () {player = new MediaPlayer (); playView = (SurfaceView) this. findViewById (R. id. play_surfaceV); // sets the playView in the buffer zone that SurfaceView does not manage. getHolder (). setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS); playView. getHolder (). addCallback (new Callback () {@ Override public void surfaceDestroyed (SurfaceHolder holder) {}@ Override public void surfaceCreated (SurfaceHolder holder) {if (position> 0) {try {// start playing play (); // and start playing player directly from the specified position. seekTo (position); position = 0;} catch (Exception e) {// TODO: handle exception }}@ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}});} private void initViews () {movieRV = (MovieRecorderView) findViewById (R. id. moive_rv); // recording startBtn = (Button) findViewById (R. id. start_btn); stopBtn = (Button) findViewById (R. id. stop_btn); // playBtn = (Button) findViewById (R. id. play_btn); pauseBtn = (Button) findViewById (R. id. pause_btn);} private void initEvents () {// start recording startBtn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {movieRV. record (new MovieRecorderView. onRecordFinishListener () {@ Override public void onRecordFinish () {}}) ;}); // stop recording stopBtn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {movieRV. stop () ;}}); // play the recorded video playBtn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {play () ;}}); // pause pauseBtn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {if (player. isPlaying () {player. pause ();} else {player. start () ;}}) ;}@ Override protected void onPause () {// determine whether the player is playing if (player. isPlaying () {// if the player is playing, we will first save the position = player. getCurrentPosition (); player. stop ();} super. onPause ();} private void play () {try {Log. d ("play:", ""); player. reset (); player. setAudioStreamType (AudioManager. STREAM_MUSIC); // set the video to be played String path = movieRV. getmVecordFile (). getAbsolutePath (); player. setDataSource (path); Log. d ("play:", path); // output the video image to SurfaceView player. setDisplay (playView. getHolder (); player. prepare (); // play player. start () ;}catch (Exception e) {// TODO: handle exception }}</span>
Layout file:
<RelativeLayout 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: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_vertical_margin" android: paddingBottom = "@ dimen/plugin" tools: context = ". mainActivity "> <record.lip.com. videorecord. movieRecorderView android: id = "@ + id/moive_rv" android: Rule = "true" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </record.lip.com. videorecord. movieRecorderView> <RelativeLayout android: id = "@ + id/record_rl" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ id/moive_rv" android: gravity = "center" android: layout_centerInParent = "true"> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/start_btn" android: text = ""/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/stop_btn" android: layout_toRightOf = "@ id/start_btn" android: text = "stop"/> </RelativeLayout> <SurfaceView android: id = "@ + id/play_surfaceV" android: layout_below = "@ id/record_rl" android: layout_width = "200dp" android: layout_height = "200dp" android: layout_centerHorizontal = "true"/> <RelativeLayout android: id = "@ + id/play_rl" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ id/play_surfaceV" android: gravity = "center" android: layout_centerInParent = "true"> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/play_btn" android: text = "play"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/pause_btn" android: text = "pause" android: layout_toRightOf = "@ id/play_btn"/> </RelativeLayout>
Required System permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.RECORD_AUDIO"/>
You can achieve the basic video recording function.
: Android video recording and Playback