Android USES the MediaRecorder class for video recording
- 2020-09-28 09:08:40
- OfStack
This site reminds you to follow the order specified by API in step 1 of the setup code for recording and recording using MediaRecorder, otherwise an error will be reported
Steps as follows:
1. Set video source and audio source, namely input source
2. Set the output format
3. Set the encoding format of audio and video
1. First of all, look at the layout file. There is an SurfaceView, which is a drawing container, and image data can be directly obtained from memory or hardware interface like DMA.
<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:orientation="vertical"
tools:context="com.xqx.mediarecorder.app.WorkRecorder">
<!-- Start recording button -->
<Button
android:id="@+id/startRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnStartRecord"
android:text=" Start recording "
/>
<Button
android:id="@+id/stopRecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnStopRecord"
android:text=" To stop recording "
/>
<SurfaceView
android:id="@+id/surView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
2. Activity code
1. First look at the member variables
// Start recording, stop recording button
private Button startRecord,stopRecord;
// Show preview SurfaceView
private SurfaceView surfaceView;
// Mark to determine whether you are currently recording
boolean isRunning = false;
// Record class
private MediaRecorder recorder;
// Storing files
private File saveFile;
2. Do some initialization in onCreate() method
startRecord = (Button) findViewById(R.id.startRecord);
stopRecord = (Button) findViewById(R.id.stopRecord);
surfaceView = (SurfaceView) findViewById(R.id.surView);
// onCreate() initialization, 1 must not start recording, so the stop button is not clickable
stopRecord.setEnabled(false);
// Setting up Surface does not require maintaining its own buffer
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// Set the resolution
surfaceView.getHolder().setFixedSize(320, 280);
// Setting this component does not allow the screen to close automatically
surfaceView.getHolder().setKeepScreenOn(true);
3. Now look at the start Recording listening event
----3.1
create MediaRecorder object
recorder = new MediaRecorder();
recorder.reset();
--3.2 Set 3 steps in a fixed order
//1. Set acquisition sound
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// Set acquisition image
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//2. Set the output format of video and audio
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//3. Set the encoding format of the audio
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
// Set the encoding format of the image
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
--3.3 For other optional Settings, check out API for more
// Set stereo
recorder.setAudioChannels(2);
// Set the maximum recording time Unit: Milliseconds
recorder.setMaxDuration(600000);
// Sets the maximum recording size Units, bytes
recorder.setMaxFileSize(1024*1024);
// audio 1 How many bits of data a second contains
recorder.setAudioEncodingBitRate(128);
// Set the selection Angle clockwise as the default is backward 90 Degree, so that the image is displayed normally , What you're setting here is the Angle at which you're going to watch the saved video
recorder.setOrientationHint(90);
// Set the resolution of the video
recorder.setVideoSize(176, 144);
--3.4 To set the file storage path, here is simple, the actual development needs to determine whether there is external storage, whether there is a target directory, whether there is already a file with the same name and other problems
// Set the output file path
saveFile = new File(Environment.getExternalStorageDirectory()
.getCanonicalFile() + "/myvideo.mp4");
recorder.setOutputFile(saveFile.getAbsolutePath());
--3.5 Preview using SurfaceView
recorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
--3.6 Start recording
recorder.prepare();
// Start recording
recorder.start();
// Make the start button unclickable , The stop button can be clicked
startRecord.setEnabled(false);
stopRecord.setEnabled(true);
isRunning = true;
--3.7 Stop recording
// To stop recording
recorder.stop();
// Release resources
recorder.release();
recorder = null;
// The start button can be clicked, but the stop button cannot be clicked
startRecord.setEnabled(true);
stopRecord.setEnabled(false);
Complete code:
package com.xqx.mediarecorder.app;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.hardware.Camera;
import java.io.File;
import java.io.IOException;
public class WorkRecorder extends Activity implements Camera.PreviewCallback {
// Start recording, stop recording button
private Button startRecord,stopRecord;
// Show preview SurfaceView
private SurfaceView surfaceView;
// Mark to determine whether you are currently recording
boolean isRunning = false;
// Record class
private MediaRecorder recorder;
// Storing files
private File saveFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_recorder);
startRecord = (Button) findViewById(R.id.startRecord);
stopRecord = (Button) findViewById(R.id.stopRecord);
surfaceView = (SurfaceView) findViewById(R.id.surView);
// onCreate() Initialize the . 1 The start is definitely not recorded, so the stop button is not clickable
stopRecord.setEnabled(false);
// Set up the Surface You don't need to maintain your own buffer
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// Set resolution
surfaceView.getHolder().setFixedSize(, );
// Setting this component does not allow the screen to close automatically
surfaceView.getHolder().setKeepScreenOn(true);
}
/**
* Start recording
* @param view
*/
public void btnStartRecord(View view) {
// First determine whether the current recording state of the video is being processed. Only when the recording state is not available can recording begin
if (!isRunning){
try {
// create MediaRecorder object
recorder = new MediaRecorder();
recorder.reset();
//. Set acquisition sound
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// Set acquisition image
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//. Set the output format of video and audio
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_);
//. Set the encoding format of the audio
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
// Set the encoding format of the image
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H);
// Set stereo
recorder.setAudioChannels();
// Set the maximum recording time Unit: Milliseconds
recorder.setMaxDuration();
// Sets the maximum recording size Units, bytes
recorder.setMaxFileSize(*);
// audio 1 How many bits of data a second contains
recorder.setAudioEncodingBitRate();
// Set the selection Angle, clockwise, as the default is backward, so that the image is displayed normally , What you're setting here is the Angle at which you're going to watch the saved video
recorder.setOrientationHint();
// Set the output file path
// saveFile = FileUtils.getMediaRecoderFolder(this);
saveFile = new File(Environment.getExternalStorageDirectory()
.getCanonicalFile() + "/myvideo.mp");
// recorder.setVideoSize(, );
// recorder.setVideoFrameRate();
recorder.setOutputFile(saveFile.getAbsolutePath());
// use SurfaceView preview
recorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
recorder.prepare();
// Start recording
recorder.start();
// Make the start button unclickable , The stop button can be clicked
startRecord.setEnabled(false);
stopRecord.setEnabled(true);
isRunning = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* The end of the recording
* @param view
*/
public void btnStopRecord(View view) {
if (isRunning){
// To stop recording
recorder.stop();
// Release resources
recorder.release();
recorder = null;
// The start button can be clicked, but the stop button cannot be clicked
startRecord.setEnabled(true);
stopRecord.setEnabled(false);
}
}
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
}
}
This example is just a simple Demo, which is used to learn coder when you are new to MediaRecorder audio recording. There are 1 Bug and some deficiencies. You can continue to develop coder
Inadequate:
1. Only when clicking "Start Recording" can SurfaceView component see the preview of camera shooting, otherwise it will be 1 black. Here you can see the Canera class and make corresponding adjustments
2. To save the path, it is necessary to determine whether there is external storage, whether the storage space is sufficient, whether the path does not exist, whether there are existing files with the same file name and other problems. Set the name of the file
3. The camera preview effect is rotated 90 degrees, so you need to go to API of MedioRecoder for setting
4. No focus is set, pixels are not clear, and the width and height of the camera are distorted