Mediarecorder
Android's Mediarecorder contains audio and video recording capabilities, and in the Android interface, music and video two applications are called Mediarecorder implementations.
Mediarecorder is implemented at the bottom based on the Opencore (PacketVideo) library, in order to build a Mediarecorder program, the upper layer also contains interprocess communication and other content, The basis of this interprocess communication is the binder mechanism in the Android Basic library.
Take Open-source Android as an example Mediarecorder code is mainly in the following directory:
Path to Java program:
Packages/apps/camera/src/com/android/camera/videocamera.java
Path to the JAVA framework:
Frameworks/base/media/java/android/media/mediarecorder.java
Java Local call section (JNI):
Frameworks/base/media/jni/android_media_mediarecorder.cpp
This part of the content compiled into a target is libmedia_jni.so.
The main header files are in the following directory:
frameworks/base/include/media/
The multimedia underlying library is in the following directory:
frameworks/base/media/libmedia/
This part of the content is compiled into a library libmedia.so.
Multimedia Services Section:
frameworks/base/media/libmediaplayerservice/
Mediarecorder and Meidaplayer use the same service.
Based on the Opencore section
External/opencore/android/author
This part of the content is compiled into a library libopencoreauthor.so.
Basic recording function implementation:
Well, understand the approximate path, the specific call Mediarecorder content can be in-depth in each file to study, and then we will directly look at the example, the recorder's code implementation of the instructions are written in the note, very simple:
Import java.io.IOException;
Import android.app.Activity;
Import Android.media.MediaRecorder;
Import Android.os.Bundle; /** * @description @author Chenzheng_java * @since 2011/03/23/public class mediare with a note on the Android phone recording
Cordactivity extends activity {Mediarecorder mediarecorder;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Mediarecorder = new Mediarecorder ();
Record ();
/** * Start recording/private void record () {/** * Mediarecorder.setaudiosource set sound source.
* Mediarecorder.audiosource This internal class describes the source of the sound in detail. * There are many audio sources in this class, but the main use is the microphone on the phone, MediaRecorder.AudioSource.MIC/Mediarecorder.setaudiosource (
MediaRecorder.AudioSource.MIC); /** * Mediarecorder.setoutputformat represents the format of the output file.
The statement must be after Setaudiosource, before prepare. * OutputFormat Internal class, defines the format of the audio output, mainly including Mpeg_4, THREE_GPP, Raw_amr ...
Wait * * Mediarecorder.setoutputformat (MediareCorder.
OUTPUTFORMAT.THREE_GPP); The/** * Mediarecorder.setaddioencoder () method allows you to set the audio encoding * Audioencoder Inner class defines two encodings in detail: Audioencoder.default, Audioencoder.amr_
NB * * * Mediarecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT);
/** * After recording, save the location of the audio file * * Mediarecorder.setoutputfile ("FILE:///SDCARD/MYVIDO/A.3PG");
/** * Make sure to call the Prepare method before calling start recording.
* * try {mediarecorder.prepare ();
Mediarecorder.start ();
catch (IllegalStateException e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
}/*** * In addition, there are several parameters and methods related to Mediarecorder, we look at: * Samplerateinhz: Audio sampling frequency, the number of times per second can be sampled, the higher the sampling rate, the higher the sound quality. * The examples given are 44100, 22050, 11025, but not limited to these parameters. For example, to collect low quality audio, you can use the low sampling rate of 4000, 8000 and so on * * Channelconfig: Channel settings: Android supports dual-channel stereo and mono. Mono mono, stereo stereo * RECORDER.STOP (); Stop recording * Recorder.reset (); Resetting the recording will reset to the Setaudiosource step * recorder.release ();
Lift the occupancy of the recording resource/}
Here, be sure to note that if we want to record, then first have to add audio recording permissions to the Androidmaniferst.xml:
<uses-permission android:name= "Android.permission.RECORD_AUDIO" ></uses-permission>