MediaRecorder类API简要解析
结构
继承关系
public class
MediaRecorder
extends Object
java.lang.Object
? android.media.MediaRecorder
类概述
用于记录音频和视频。记录控制基于一个简单的状态机(如下图)。
通常的MediaRecorder录制音频的工作机制示例如下:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
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
应用程序可能想要注册信息化和错误事件为了能够记录一些内部更新和可能存在的运行时错误在记录过程中。
通过设置合适的监听器(通过调用(setOnInfoListener(OnInfoListener)设置监听器和(或)setOnErrorListener(OnErrorListener)设置错误监听器)来注册事件监听。为了接收到各自的关联的监听器的回调信息,应用程序要求MediaRecorder对象被创建在一个循环执行的线程中(默认的主UI线程已经有循环在执行)。
注意:通常MediaRecorder不在仿真器中工作。
开发者指导
要获得更多的关于如何使用MediaRecorder录制视频的信息,请阅读Camera开发者指导。要获得更多的关于如何使用MediaRecorder用以录制音频的信息,请阅读Aduio Capture开发者指导。
概要
内部类 |
|||||
class |
Defines the audio encoding. 定义音频的编码方式 |
||||
class |
Defines the audio source. 定义音频的来源 |
||||
interface |
Interface definition for a callback to be invoked when an error occurs while recording. 接口定义当录制过程中产生的错误时被执行的回调函数 |
||||
interface |
Interface definition for a callback to be invoked when an error occurs while recording. 接口定义当录制过程中产生错误时被执行的回调函数 |
||||
class |
Defines the output format. 定义文件输出的格式 |
|
|||
class |
Defines the video encoding 定义视频编码方式 |
|
|
||
class |
Defines the video source. 定义视频的源 |
|
|
常量 |
||||||||||||
int |
Media server died. 媒体服务死亡。这种情况下,应用程序必须释放现在的MediaRecorder对象并且重新实例化一个 |
|||||||||||
int |
Unspecified media recorder error. 未指定的媒体记录器错误 |
|
|
|||||||||
int |
A maximum duration had been setup and has now been reached. 最大时长已经被定义而现在已经达到已定义的最大时长 |
|||||||||||
int |
A maximum filesize had been setup and has now been reached. 最大的文件大小已经被定义而现在已达到已定义的最大文件大小 |
|
|
|
|
|
|
|
|
|
||
int |
Unspecified media recorder error. 未指定的媒体记录器错误 |
|
|
|
|
|
|
|
|
|
公共构造方法 |
|
|
Default constructor. 默认的构造函数 |
Public Methods |
|||||||||||||||
final static int |
Gets the maximum value for audio sources. 获取音频源的最大值 |
|
|
|
|
|
|
|
|
|
|
||||
int |
Returns the maximum absolute amplitude that was sampled since the last call to this method. 返回最大绝对振幅,取样自上次调用这个方法。该方法只能在setAudioSource()方法后调用。 返回 上次调用该方法后测量的最大绝对振幅。如果是第一次调用则返回0 抛出
IllegalStateException 如果在设置音频源之前调用该方法 |
||||||||||||||
void |
prepare() Prepares the recorder to begin capturing and encoding data. 准备记录器开始捕捉和编码数据。该方法必须在设置期望捕捉的音频和视频源、编码方式、文件格式等之后,和start()方法前被调用。 抛出
|
||||||||||||||
void |
release() Releases resources associated with this MediaRecorder object. 释放和该MediaRecorder对象关联的资源。当你不再使用该MediaRecorder对象时调用该方法是一个好的尝试。无论何时应用程序的一个Activity被暂停(它的paused()方法被调用),或者停止(onStop()方法被调用),这个方法都应该被调用去释放MediaRecorder对象,出发应用程序有特殊的需要保存这个对象。 In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a MediaRecorder object is no longer needed may also lead to continuous battery consumption for mobile devices, and recording failure for other applications if no multiple instances of the same codec are supported on a device. Even if multiple instances of the same codec are supported, some performance degradation may be expected when unnecessary multiple instances are used at the same time. |
||||||||||||||
void |
reset() Restarts the MediaRecorder to its idle state. 重启MediaRecorder使其处于空闲状态。调用这个方法后,你需要重新配置一次该对象,就像刚创建时一样。 |
||||||||||||||
void |
setAudioChannels(int numChannels) Sets the number of audio channels for recording. 设置正在记录的音频的频道数量。在prepare()前调用该方法。prepare()为了保证指定数量的音频频道可用可能会进行额外的检查 参数 numChannels 音频频道的数量,通常是1(mono)或2(stereo) |
|
|
|
|
|
|
|
|
|
|
||||
void |
setAudioEncoder(int audio_encoder) Sets the audio encoder to be used for recording. 设置使用的音频的编码器。如果该方法被调用,输出文件将不包含音频轨道。在setOutputFormat() 之后,prepare()之前调用该方法。 参数 audio_encoder 要使用的音频编码器 参见 · |
|
|
|
|
|
|
|
|
|
|
||||
void |
setAudioEncodingBitRate(int bitRate) Sets the audio encoding bit rate for recording. 设置记录时音频编码的比特率。在prepare()之前调用该方法。prepare()为了保证指定的比特率是否可用而可能进行额外的参数检查。有时被传入的bitRate会被内部地截断为了保证记录音频的过程顺利,这要基于平台的容量。 参数 bitRate 音频编码每秒的比特率的比特值 |
|
|
|
|
|
|
|
|
|
|
||||
void |
setAudioSamplingRate(int samplingRate) Sets the audio sampling rate for recording. 设置记录时音频的抽样率。 参数 samplingRate 音频每秒的抽样次数 |
|
|
|
|
|
|
|
|
|
|
||||
void |
setAudioSource(int audio_source) 设置要记录的声音源。 如果未调用该方法,输出文件将不会包含音频轨道。声音源需要在设置录制参数或编码器之前指定。只在setOutputFormat().方法前调用。 参数
抛出
之后调用 参见
|
|
|
|
|
|
|
|
|
|
|
||||
void |
Sets a Camera to use for recording. 设置用于视频录制的照相机。 使用该方法在预览和采集模式间快速切换,而无需销毁camera对象。unlock()方法应该在该方法前被调用。该方法必须在prepare()前调用。 参数 c 用于录制视频的照相机 |
|
|
|
|
|
|
|
|
|
|
||||
void |
setCaptureRate(double fps) Set video frame capture rate. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setLocation(float latitude, float longitude) Set and store the geodata (latitude and longitude) in the output file. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setMaxDuration(int max_duration_ms) Sets the maximum duration (in ms) of the recording session. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setMaxFileSize(long max_filesize_bytes) Sets the maximum filesize (in bytes) of the recording session. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setOnErrorListener(MediaRecorder.OnErrorListener l) Register a callback to be invoked when an error occurs while recording. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setOnInfoListener(MediaRecorder.OnInfoListener listener) Register a callback to be invoked when an informational event occurs while recording. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setOrientationHint(int degrees) Sets the orientation hint for output video playback. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setOutputFile(FileDescriptor fd) Pass in the file descriptor of the file to be written. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setOutputFile(String path) Sets the path of the output file to be produced. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setOutputFormat(int output_format) Sets the format of the output file produced during recording. |
|
|
|
|
|
|
|
|
|
|
||||
void |
Sets a Surface to show a preview of recorded media (video). |
|
|
|
|
|
|
|
|
|
|
||||
void |
setProfile(CamcorderProfile profile) Uses the settings from a CamcorderProfile object for recording. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setVideoEncoder(int video_encoder) Sets the video encoder to be used for recording. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setVideoEncodingBitRate(int bitRate) Sets the video encoding bit rate for recording. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setVideoFrameRate(int rate) Sets the frame rate of the video to be captured. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setVideoSize(int width, int height) Sets the width and height of the video to be captured. |
|
|
|
|
|
|
|
|
|
|
||||
void |
setVideoSource(int video_source) Sets the video source to be used for recording. |
|
|
|
|
|
|
|
|
|
|
||||
void |
start() Begins capturing and encoding data to the file specified with setOutputFile(). |
|
|
|
|
|
|
|
|
|
|
||||
void |
stop() Stops recording. |
|
|
|
|
|
|
|
|
|
|
Protected Methods |
||||||||||||
void |
finalize() Invoked when the garbage collector has detected that this instance is no longer reachable. |
|
|
|
|
|
|
|
|
|