SoFunction
Updated on 2025-04-10

Example code for recording using MediaRecorder in Android

There is no difference between recording and recording on MediaRecorder, so there is an additional format to set the image.

Reference: https:///article/

Example:

Copy the codeThe code is as follows:

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

Copy the codeThe code is as follows:

<RelativeLayout xmlns:andro
    xmlns:tools="/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <SurfaceView
        android:
        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:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/record" />

        <Button
            android:
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/stop" />
    </LinearLayout>

</RelativeLayout>

Copy the codeThe code is as follows:

package ;

import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class MainActivity extends Activity implements OnClickListener {

 Button record, stop;
// System video file
 File viodFile;
 MediaRecorder mRecorder;
// SurfaceView showing video
 SurfaceView sView;
// Record whether recording is being recorded
 boolean isRecording = false;

 Camera camera;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_main);
  record = (Button) findViewById();
  stop = (Button) findViewById();
  sView = (SurfaceView) findViewById();
// The stop button is not available
  (false);

// Setting up Surface does not require maintaining its own buffer
  ().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// Set resolution
  ().setFixedSize(320, 280);
// Setting this component will not automatically turn off the screen
  ().setKeepScreenOn(true);

  (this);
  (this);

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(, menu);
  return true;
 }

 @Override
 public void onClick(View v) {
  switch (()) {
  case :
   if (!().equals(
     Environment.MEDIA_MOUNTED)) {
(this, "The SD card does not exist, please insert the card!", Toast.LENGTH_SHORT).show();
    return;
   }
   try {
// Create MediaPlayer object
    mRecorder = new MediaRecorder();
    ();
   /* camera = ();
    ();
    (0);
    (camera);*/
// Create a video file that saves recorded videos
    viodFile = new File(()
      .getCanonicalFile() + "/myvideo.mp4");

    if (!())
     ();

// Set to collect sound from the microphone
    ();
// Set up to capture images from the camera
    ();
// Set the output format of video and audio
    ();
// Set the encoding format of the audio,
    ();
// Set image encoding format
    ();
    (90);
    //(320, 280);
    // (5);
    (());
// Specify SurfaceView to preview the video
    (().getSurface());
    ();
// Start recording
    ();
// Make the record button unavailable
    (false);
// Make the stop button available
    (true);
    isRecording = true;

   } catch (Exception e) {
    ();
   }
   break;
  case :
// If recording
   if (isRecording) {
// Stop recording
    ();
// Release resources
    ();
    mRecorder = null;
// Make the record button available
    (true);
// Make the stop button unavailable
    (false);
   }
   break;
  default:
   break;
  }
 }
}