Example code for recording Android with MediaRecorder

  • 2020-05-24 06:10:21
  • OfStack

There is no difference between video recording and audio recording in MediaRecorder, but there is one more format to set the image

Reference: https: / / www. ofstack. com article / 46182. htm

Example:


    <!--  Grant the program permission to record sounds  -->
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <!--  Grant the program permission to use the camera  -->
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <!--  Grant permission to use external storage  -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


<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"
    tools:context=".MainActivity" >
    <SurfaceView
        android:id="@+id/dView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"  
        android:orientation="horizontal" >
        <Button
            android:id="@+id/record"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/record" />
        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/stop" />
    </LinearLayout>
</RelativeLayout>


package com.android.xiong.videotest;
import java.io.File;
import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
 Button record, stop;
 //  System video file 
 File viodFile;
 MediaRecorder mRecorder;
 //  Video display SurfaceView
 SurfaceView sView;
 //  Record whether recording is in progress 
 boolean isRecording = false;
 Camera camera;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  record = (Button) findViewById(R.id.record);
  stop = (Button) findViewById(R.id.stop);
  sView = (SurfaceView) findViewById(R.id.dView);
  // stop Button not available 
  stop.setEnabled(false);
  //  Set up the Surface You don't need to maintain your own buffer 
  sView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  //  Set resolution 
  sView.getHolder().setFixedSize(320, 280);
  //  Setting this component does not automatically close the screen 
  sView.getHolder().setKeepScreenOn(true);
  record.setOnClickListener(this);
  stop.setOnClickListener(this);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.record:
   if (!Environment.getExternalStorageState().equals(
     Environment.MEDIA_MOUNTED)) {
    Toast.makeText(this, "SD The card does not exist, please insert the card! ", Toast.LENGTH_SHORT).show();
    return;
   }
   try {
    //  create MediaPlayer object 
    mRecorder = new MediaRecorder();
    mRecorder.reset();
   /* camera = Camera.open();
    camera.unlock();
    camera.setDisplayOrientation(0);
    mRecorder.setCamera(camera);*/
    //  Create a video file to save the recorded video 
    viodFile = new File(Environment.getExternalStorageDirectory()
      .getCanonicalFile() + "/myvideo.mp4");
    if (!viodFile.exists())
     viodFile.createNewFile();
    //  Set to collect sound from the microphone 
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    //  Set to collect images from the camera 
    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    //  Set the output format of video and audio 
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    //  Set the encoding format of audio, 
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    //  Set the image encoding format 
    mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    mRecorder.setOrientationHint(90);
    //mRecorder.setVideoSize(320, 280);
    // mRecorder.setVideoFrameRate(5);
    mRecorder.setOutputFile(viodFile.getAbsolutePath());
    //  The specified SurfaceView To preview the video 
    mRecorder.setPreviewDisplay(sView.getHolder().getSurface());
    mRecorder.prepare();
    //  Start recording 
    mRecorder.start();
    //  let record Button not available 
    record.setEnabled(false);
    //  let stop Button is available 
    stop.setEnabled(true);
    isRecording = true;
   } catch (Exception e) {
    e.printStackTrace();
   }
   break;
  case R.id.stop:
   //  If you're recording 
   if (isRecording) {
    //  To stop recording 
    mRecorder.stop();
    //  Release resources 
    mRecorder.release();
    mRecorder = null;
    //  let record Button is available 
    record.setEnabled(true);
    //  let stop Button not available 
    stop.setEnabled(false);
   }
   break;
  default:
   break;
  }
 }
}


Related articles: