This article was reproduced from: http://www.jb51.net/article/82281.htm
This article mainly describes the Android app call Mediarecorder implementation of the recording function instance, Mediarecorder is very powerful, not only can be used to record audio can also record video, the need for friends can refer to the next
Mediarecorder
Android Mediarecorder contains audio and video recording capabilities, and on Android interface, music and video two applications are called Mediarecorder implementations.
Mediarecorder at the bottom is based on the Opencore (PacketVideo) library implementation, in order to build a Mediarecorder program, the upper layer also contains inter-process communication and other content, This inter-process communication is based on the binder mechanism in the Android base library.
Take the open source Android as an example Mediarecorder code is mainly in the following directory:
Path to the 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 invocation section (JNI):
Frameworks/base/media/jni/android_media_mediarecorder.cpp
This part of the content is compiled as a target for libmedia_jni.so.
The primary header file is in the following directory:
frameworks/base/include/media/
The multimedia underlying library is in the following directory:
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 services.
Based on the Opencore section
External/opencore/android/author
This section is compiled into a library libopencoreauthor.so.
Basic recording functions are implemented:
Well, understand the approximate path, the specific call Mediarecorder content can go deep into the files to study, then we will take a direct look at the example, the Recorder code implementation of the instructions are written in the comments, very simple:
Import java.io.IOException; Import android.app.Activity; Import Android.media.MediaRecorder; Import Android.os.Bundle; /** * @description A little instruction on recording via Android phone * @author Chenzheng_java * @since 2011/03/23 */public class Mediarecorda Ctivity extends Activity {mediarecorder mediarecorder; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Mediarecorder = new Mediarecorder (); Record (); }/** * starts recording */private void record () {/** * Mediarecorder.setaudiosource setting the sound source. * Mediarecorder.audiosource This internal class describes the sound source in detail. * There are many sources of audio 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 audio output, mainly includes Mpeg_4, THREE_GPP, Raw_amr ... such as */Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); /** * The Mediarecorder.setaddioencoder () method can set the encoding of the audio * Audioencoder Inner class defines two encodings in detail: Audioencoder.default, AUDIOENCODER.AMR_NB */Mediarecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT); /** * After setting the recording, save the location of the audio file */mediarecorder.setoutputfile ("FILE:///SDCARD/MYVIDO/A.3PG"); /** * Be sure to call the Prepare method before you call start to 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 given example is 44100, 22050, 11025 but not limited to these parameters. For example, to collect low-quality audio, you can use a low sample rate of 4000, 8000, etc. * * Channelconfig: Channel settings: Android supports two-channel stereo and mono. Mono mono, stereo stereo * * RECORDER.STOP (); Stop recording * Recorder.reset (); Resetting the recording will reset to setaudiosource this step * recorder.release (); Unlock the recording resource * *}
Here, it's important to note that if we want to record, we have to add the recording permission to the Androidmaniferst.xml first:
<uses-permission android:name= "Android.permission.RECORD_AUDIO" ></uses-permission>
Android app calls Mediarecorder to implement recording function "Turn"