MediaRecorder
Used to record audio and video.用来音视频的录制与播放,状态图如下:
A common case of using MediaRecorder to record audio works as follows,使用流程如下:
// initial
MediaRecorder recorder = new MediaRecorder();
// initialized
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
AudioSource
DEFAULT Default audio source
MIC Microphone audio source
REMOTE_SUBMIX Audio source for a submix of audio streams to be presented remotely.
VOICE_CALL Voice call uplink + downlink audio source
VOICE_COMMUNICATION Microphone audio source tuned for voice communications such as VoIP
VOICE_RECOGNITION Microphone audio source tuned for voice recognition if available, behaves like DEFAULT otherwise.
// config
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 输出格式
AAC_ADTS AAC ADTS file format
AMR_NB AMR NB file format
AMR_WB AMR WB file format
DEFAULT
MPEG_4 MPEG4 media file format
RAW_AMR This constant was deprecated in API level 16. Deprecated in favor of MediaRecorder.OutputFormat.AMR_NB
THREE_GPP 3GPP media file format
WEBM VP8/VORBIS data in a WEBM container
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 音频编码格式
AAC AAC Low Complexity (AAC-LC) audio codec
AAC_ELD Enhanced Low Delay AAC (AAC-ELD) audio codec
AMR_NB AMR (Narrowband) audio codec
AMR_WB AMR (Wideband) audio codec
DEFAULT HE_AAC High Efficiency AAC (HE-AAC) audio codec
VORBIS Ogg Vorbis audio codec
recorder.setOutputFile(PATH_NAME);
// prepare
recorder.prepare();
// recording
recorder.start(); // Recording is now started
...
recorder.stop();
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused
使用的时候最好根据上图的流程,不然会报一些异常:
public void setAudioEncoder (int audio_encoder)
Added in API level 1
Sets the audio encoder to be used for recording. If this method is not called, the output file will not contain an audio track. Call this after setOutputFormat() but before prepare().
Parameters
audio_encoder the audio encoder to use.
Throws
IllegalStateException if it is called before setOutputFormat() or after prepare().
要注意调用顺序