Android MediaRecorder 相关
本篇文章主要介绍下MediaRecorder类.
1:创建对象
MediaRecorder mediaRecorder=new MediaRecorder();
MediaRecorder可以直接通过无参构造直接创建对象.
2: 音频源
通过调用setAudioSource(int audio_source)来设置音频源,可以是麦克风.音频文件等.
具体的可以查看MediaRecorder.AudioSource类.
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
这里注意:
我们需要申请权限android.permission.RECORD_AUDIO
否则直接调用,错误如下:
2024-02-17 10:24:47.670 1396-5717/? E/ServiceUtilities: Request requires android.permission.RECORD_AUDIO
2024-02-17 10:24:47.672 19945-19945/com.test.media E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.test.media, PID: 19945java.lang.RuntimeException: setAudioSource failed.at android.media.MediaRecorder.setAudioSource(Native Method)at com.test.webtest.MainActivity.onClick(MainActivity.java:40)at android.view.View.performClick(View.java:7570)at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)at android.view.View.performClickInternal(View.java:7525)at android.view.View.access$3900(View.java:836)at android.view.View$PerformClick.run(View.java:28680)at android.os.Handler.handleCallback(Handler.java:938)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:263)at android.app.ActivityThread.main(ActivityThread.java:8321)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
3: 输出格式
调用setOutputFormat(int output_format)可以设置音频的输出格式, 可以是AMR,AAC等.
public final class OutputFormat {/* Do not change these values without updating their counterparts* in include/media/mediarecorder.h!*/private OutputFormat() {}public static final int DEFAULT = 0;/** 3GPP media file format*/public static final int THREE_GPP = 1;/** MPEG4 media file format*/public static final int MPEG_4 = 2;/** The following formats are audio only .aac or .amr formats *//*** AMR NB file format* @deprecated Deprecated in favor of MediaRecorder.OutputFormat.AMR_NB*/public static final int RAW_AMR = 3;/** AMR NB file format */public static final int AMR_NB = 3;/** AMR WB file format */public static final int AMR_WB = 4;/** @hide AAC ADIF file format */public static final int AAC_ADIF = 5;/** AAC ADTS file format */public static final int AAC_ADTS = 6;/** @hide Stream over a socket, limited to a single stream */public static final int OUTPUT_FORMAT_RTP_AVP = 7;/** H.264/AAC data encapsulated in MPEG2/TS */public static final int MPEG_2_TS = 8;/** VP8/VORBIS data in a WEBM container */public static final int WEBM = 9;};
测试代码设置为AMR, 设置如下:
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
这里需要注意:
- setOutputFormat需要在setAudioSource()/setVideoSource()后调用,否则会抛出IllegalStateException.
4:输出文件
mediaRecorder.setOutputFile(new File("sdcard/test.mp3"));
public void setOutputFile(File file)
{mPath = null;mFd = null;mFile = file;
}
public void setOutputFile(FileDescriptor fd) throws IllegalStateException{mPath = null;mFile = null;mFd = fd;
}
public void setOutputFile(String path) throws IllegalStateException
{mFd = null;mFile = null;mPath = path;
}
5: 音频编码器
调用setAudioEncoder(int audio_encoder)来设置音频编码器
audio_encoder可查看android.media.MediaRecorder.AudioEncoder,具体的参数如下:
public final class AudioEncoder {/* Do not change these values without updating their counterparts* in include/media/mediarecorder.h!*/private AudioEncoder() {}public static final int DEFAULT = 0;/** AMR (Narrowband) audio codec */public static final int AMR_NB = 1;/** AMR (Wideband) audio codec */public static final int AMR_WB = 2;/** AAC Low Complexity (AAC-LC) audio codec */public static final int AAC = 3;/** High Efficiency AAC (HE-AAC) audio codec */public static final int HE_AAC = 4;/** Enhanced Low Delay AAC (AAC-ELD) audio codec */public static final int AAC_ELD = 5;/** Ogg Vorbis audio codec */public static final int VORBIS = 6;
}
设置如下:
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
需要注意:
- setAudioEncoder需要在setOutputFormat()后调用.
- setAudioEncoder需要在prepare()前调用.
否则,会抛出异常IllegalStateException.
6:准备录制
try {mediaRecorder.prepare();
} catch (IOException e) {e.printStackTrace();
}
prepare()方法需要在setting sources,encoders,files,format, etc. ,之后调用,否则抛出IllegalStateException.
prepare()需要在start()方法之前调用,否则抛出IllegalStateException.
其余错误统一抛出IOException.
7:开始录制
mediaRecorder.start();
start()方法注意:
@throws IllegalStateException if it is called before prepare() or when the camera is already in use by another app.
8:暂停录制
mediaRecorder.pause();
9:结束录制
mediaRecorder.stop();
10:释放资源
mediaRecorder.release();