Android的Media Framework 处于Libraries这一层,这层的Library不是用Java实现,一般是C/C++实现,它们通过Java的JNI方式调用。 多媒体架构:基于第三方PacketVideo公司的OpenCORE platform 来实现,支持所有通用的音频,视频,静态图像格式。CODEC( 编解码器 ) 使用 OpenMAX 1L interface 接口进行扩展,可以方便的支持hardware / software codec plug-ins。支持的格式包括: MPEG4 、H.264 、MP3 、AAC 、AMR 、JPG 、PNG 、GIF 等。 在实际开发中我们并不会过多的研究Open Core 的实现,Android 提供了上层的Media API 给开发人员使用,分别是MediaPlayer 和MediaRecorder。
Media Player 提供的基本接口如下: Public Methods (1)static MediaPlayer create (Context context, Uri uri) Convenience method to create a MediaPlayer for a given Uri. (2)int getCurrentPosition () Gets the current playback position. (3)int getDuration () Gets the duration of the file. (4)int getVideoHeight () Returns the height of the video. (5)int getVideoWidth () Returns the width of the video. (6)boolean isPlaying () Checks whether the MediaPlayer is playing. (7)void pause () Pauses playback. (8)void prepare () Prepares the player for playback, synchronously. (9)void prepareAsync () Prepares the player for playback, asynchronously. (10)void release () Releases resources associated with this MediaPlayer object. (11)void reset () Resets the MediaPlayer to its uninitialized state. (12)void seekTo (int msec) Seeks to specified time position. (13) void setAudioStreamType (int streamtype) Sets the audio stream type for this MediaPlayer. (14)void setDataSource (String path) Sets the data source (file-path or http/rtsp URL) to use. (15)void setDisplay (SurfaceHolder sh) Sets the SurfaceHolder to use for displaying the video portion of the media. (16)void setVolume (float leftVolume, float rightVolume) Sets the volume on this player. (17)void start () Starts or resumes playback. (18)void stop () Stops playback after playback has been stopped or paused. 我们可以看出 MediaPlayer 类提供了一个多媒体播放器的基本操作,播放,暂停,停止,设置音量等等。 简单的例子: Playing a File: MediaPlayer mp = new MediaPlayer(); mp.setDataSource(PATH_TO_FILE); mp.prepare(); mp.start(); Playing a Raw Resource: MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); mp.start();
Public Method: (1)void prepare () Prepares the recorder to begin capturing and encoding data. (2)void release () Releases resources associated with this MediaRecorder object. (3)void reset () Restarts the MediaRecorder to its idle state. (4)void setAudioEncoder (int audio_encoder) Sets the audio encoder to be used for recording. (5)void setAudioSource (int audio_source) Sets the audio source to be used for recording. (6)void setOutputFile (String path) Sets the path of the output file to be produced. (7)void setOutputFormat (int output_format) Sets the format of the output file produced during recording. (8)void setPreviewDisplay (Surface sv) Sets a Surface to show a preview of recorded media (video). (9)void start () Begins capturing and encoding data to the file specified with setOutputFile(). (10)void stop () Stops recording. 简单的例子: Example: 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
整体结构的核心文件的位置如下: A,MediaPlayer JNI 代码位置 /frameworks/base/media/jni B, MediaPlayer (Native) 代码位置 /frameworks/base/media/libmedia C, MediaPlayerService (Server) 代码位置 /frameworks/base/media/libmediaplayerservice D, MediaPlayerService Host Process 代码位置 /frameworks/base/media/mediaserver/main_mediaserver.cpp E, PVPlayer 代码位置 /external/opencore/android/ |
|