1.MediaRecorder概述
Android 多媒体框架支持捕获和编码各种常见的音频和视频格式,简要介绍音频录音。
2.MediaRecorder
源码路径:
frameworks/base/media/java/android/media/MediaRecorder.java
源码接口:
setAudioSource(MediaRecorder.AudioSource.MIC) 用于设置音频源,如:MediaRecorder.AudioSource.MIC,此MIC一般为设备主MIC:
源码函数:
public native void setAudioSource(@Source int audioSource)throws IllegalStateException;
setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)设置输出文件格式,如:MediaRecorder.OutputFormat.THREE_GPP,THREE_GPP表示录音保存文件为3gp:
源码函数:
public native void setOutputFormat(int output_format)throws IllegalStateException;
setOutputFile()设置输出文件名,如" sdcard/audiorecordtest.3gp":
源码函数:
public void setOutputFile(File file)
{
mPath = null;
mFd = null;
mFile = file;
}
setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)设置音频编码器,MediaRecorder.AudioEncoder.AMR_NB,AMR_NB为编码格式:
源码函数:
public native void setAudioEncoder(int audio_encoder)throws IllegalStateException;
prepare()各参数设置完成后,启动初始化:
源码函数:
public void prepare() throws IllegalStateException, IOException
{
if (mPath != null) {
RandomAccessFile file = new RandomAccessFile(mPath, "rw");
try {
_setOutputFile(file.getFD());
} finally {
file.close();
}
} else if (mFd != null) {
_setOutputFile(mFd);
} else if (mFile != null) {
RandomAccessFile file = new RandomAccessFile(mFile, "rw");
try {
_setOutputFile(file.getFD());
} finally {
file.close();
}
} else {
throw new IOException("No valid output file");
}
_prepare();
}
start() 执行录音启动:
源码函数:
public native void start() throws IllegalStateException;
stop()停止录音:
源码函数:
public native void stop() throws IllegalStateException;
3 权限申请:
使用录音功能,APK必须告知用户它将访问设备的音频输入。从 Android 6.0(API 级别 23)开始,APK录音运行时,必须请求用户批准。APK 需要请求如下权限。
<uses-permission android:name="android.permission.RECORD_AUDIO" />
以上,MediaRecorder录取模式简要说明,可以参照源码进行查看。
不同参数类型: https://mp.csdn.net/mp_blog/creation/editor/140903503