An example of Android App calling MediaRecorder to realize recording function

  • 2021-07-01 08:15:07
  • OfStack

MediaRecorder

MediaRecorder of Android includes the recording function of Audio and video. On the interface of Android, Music and Video are both implemented by calling MediaRecorder.
MediaRecorder is implemented based on OpenCore (PacketVideo) library in the bottom layer. In order to build an MediaRecorder program, the upper layer also includes interprocess communication, etc. This interprocess communication is based on Binder mechanism in Android basic library.
Take the open source Android as an example. The code of MediaRecorder is mainly in the following directory:
Path to the JAVA program:


packages/apps/Camera/src/com/android/camera/VideoCamera.java

Path to JAVA Framework:


frameworks/base/media/java/android/media/MediaRecorder.java

JAVA Local Call Part (JNI):


frameworks/base/media/jni/android_media_MediaRecorder.cpp

This section compiles to the target libmedia_jni. so.
The main header files are in the following directories:


frameworks/base/include/media/

The multimedia underlying library is in the following directory:


frameworks/base/media/libmedia/ 

This section is compiled into the library libmedia. so.
Multimedia services:


frameworks/base/media/libmediaplayerservice/

MediaRecorder and MeidaPlayer use the same service.
Based on OpenCore section


external/opencore/android/author

This part is compiled into the library libopencoreauthor. so.

Basic recording function realization:
Ok, after understanding the general path, the specific content of calling MediaRecorder can be studied in depth. Next, we will look at the example directly. The description of the code implementation of this recorder is written in the comments, which is very simple:


import java.io.IOException; 
 
import android.app.Activity; 
import android.media.MediaRecorder; 
import android.os.Bundle; 
/** 
 * @description 对通过android系统手机进行录音的1点说明测试 
 * @author chenzheng_java 
 * @since 2011/03/23 
 */ 
public class MediaRecordActivity extends Activity { 
  
 MediaRecorder mediaRecorder ; 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
   
  mediaRecorder = new MediaRecorder(); 
  record(); 
   
 } 
  
 /** 
  * 开始录制 
  */ 
 private void record(){ 
  /** 
   * mediaRecorder.setAudioSource设置声音来源。 
   * MediaRecorder.AudioSource这个内部类详细的介绍了声音来源。 
   * 该类中有许多音频来源,不过最主要使用的还是手机上的麦克风,MediaRecorder.AudioSource.MIC 
   */ 
  mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
  /** 
   * mediaRecorder.setOutputFormat代表输出文件的格式。该语句必须在setAudioSource之后,在prepare之前。 
   * OutputFormat内部类,定义了音频输出的格式,主要包含MPEG_4、THREE_GPP、RAW_AMR……等。 
   */ 
  mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
  /** 
   * mediaRecorder.setAddioEncoder()方法可以设置音频的编码 
   * AudioEncoder内部类详细定义了两种编码:AudioEncoder.DEFAULT、AudioEncoder.AMR_NB 
   */ 
  mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 
  /** 
   * 设置录音之后,保存音频文件的位置 
   */ 
  mediaRecorder.setOutputFile("file:///sdcard/myvido/a.3pg"); 
   
  /** 
   * 调用start开始录音之前,1定要调用prepare方法。 
   */ 
  try { 
   mediaRecorder.prepare(); 
   mediaRecorder.start(); 
  } catch (IllegalStateException e) { 
   e.printStackTrace(); 
  } catch (IOException e) { 
   e.printStackTrace(); 
  } 
   
 } 
  
 /*** 
  * 此外,还有和MediaRecorder有关的几个参数与方法,我们1起来看1下: 
  * sampleRateInHz :音频的采样频率,每秒钟能够采样的次数,采样率越高,音质越高。 
  * 给出的实例是44100、22050、11025但不限于这几个参数。例如要采集低质量的音频就可以使用4000、8000等低采样率 
  * 
  * channelConfig :声道设置:android支持双声道立体声和单声道。MONO单声道,STEREO立体声 
  * 
  * recorder.stop();停止录音 
  * recorder.reset(); 重置录音 ,会重置到setAudioSource这1步 
  * recorder.release(); 解除对录音资源的占用 
  */ 
} 

Here, 1 must pay attention to 1 point, that is, if we want to record, we must first add recording permission to AndroidManiferst. xml:


<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>


Related articles: