Android多媒體框架初步分析
掃描二維碼
隨時(shí)隨地手機(jī)看文章
Android 系統(tǒng)整體架構(gòu):
我們先看一下多媒體框架在整個(gè)Android系統(tǒng)所處的位置
從框架圖可以看出Media Framework處于Libraries這一層,這層的Library不是用Java實(shí)現(xiàn),一般是C/C++實(shí)現(xiàn),它們通過(guò)Java的JNI方式調(diào)用。
多媒體架構(gòu):
基于第三方PacketVideo 公司的OpenCORE platform來(lái)實(shí)現(xiàn)
支持所有通用的音頻,視頻,靜態(tài)圖像格式
CODEC(編解碼器)使用OpenMAX 1L interface 接口進(jìn)行擴(kuò)展,可以方便得支持hardware / software codec plug-ins
支持的格式包括:MPEG4、H.264、MP3、AAC、AMR、JPG、PNG、GIF等。
l Open Core多媒體框架有一套通用可擴(kuò)展的接口針對(duì)第三方的多媒體遍解碼器,輸入,輸出設(shè)備等等 。
l 多媒體文件的播放,下載,包括3GPP, MPEG-4,AAC and MP3 containers
l 流媒體文件的下載,實(shí)時(shí)播放,包括:3GPP, HTTP and RTSP/RTP
l 動(dòng)態(tài)視頻和靜態(tài)圖像的編碼,解碼,例如:MPEG-4, H.263 and AVC (H.264), JPEG
l 語(yǔ)音編碼格式: AMR-NB and AMR-WB
l 音樂(lè)編碼格式: MP3, AAC, AAC+
l 視頻和圖像格式: 3GPP, MPEG-4 and JPEG
l 視頻會(huì)議:基于H324-M standard
圖中用黃線圈出的是Media Framework
Open Core介紹:
Open Core是Android 多媒體框架的核心,所有Android平臺(tái)的音視頻采集,播放的操作都是通過(guò)它來(lái)實(shí)現(xiàn)。它也被稱(chēng)為PV(Packet Video), Packet Video是一家專(zhuān)門(mén)提供多媒體解決方案的公司。
通過(guò)Open Core程序員可以方便快速的開(kāi)發(fā)出想要的多媒體應(yīng)用程序,例如:音視頻的采集,回放,視頻會(huì)議,實(shí)時(shí)的流媒體播放等等應(yīng)用。
Open Core 框架
代碼結(jié)構(gòu):
Open Core的代碼在Android代碼的External/Opencore目錄中。這個(gè)目錄是OpenCore的根目錄,其中包含的子目錄如下所示:
android:這里面是一個(gè)上層的庫(kù),它實(shí)現(xiàn)了一個(gè)為Android使用的音視頻采集,播放的接口,和DRM數(shù)字版權(quán)管理的接口實(shí)現(xiàn)。
baselibs:包含數(shù)據(jù)結(jié)構(gòu)和線程安全等內(nèi)容的底層庫(kù)
codecs_v2:音視頻的編解碼器,基于OpenMAX實(shí)現(xiàn)
engines:核心部分,多媒體引擎的實(shí)現(xiàn)
extern_libs_v2:包含了khronos的OpenMAX的頭文件
fileformats:文件格式的解析(parser)工具
nodes:提供一些PVMF的NODE,主要是編解碼和文件解析方面的。
oscl:操作系統(tǒng)兼容庫(kù)
pvmi: 輸入輸出控制的抽象接口
protocols:主要是與網(wǎng)絡(luò)相關(guān)的RTSP、RTP、HTTP等協(xié)議的相關(guān)內(nèi)容
pvcommon:pvcommon庫(kù)文件的Android.mk文件,沒(méi)有源文件。
pvplayer:pvplayer庫(kù)文件的Android.mk文件,沒(méi)有源文件。
pvauthor:pvauthor庫(kù)文件的Android.mk文件,沒(méi)有源文件。
tools_v2:編譯工具以及一些可注冊(cè)的模塊。
Open Core 上層代碼結(jié)構(gòu)
在實(shí)際開(kāi)發(fā)中我們并不會(huì)過(guò)多的研究Open Core的實(shí)現(xiàn),Android提供了上層的Media API給開(kāi)發(fā)人員使用,MediaPlayer和MediaRecorder
Android Media APIs
l The Android platform is capable of playing both audio and video media. It is also capable of playing media contained in the resources for an application, or a standalone file in the filesystem, or even streaming media over a data connection. Playback is achieved through the android.media.MediaPlayer class.
l The Android platform can also record audio. Video recording capabilities are coming in the future. This is achieved through the android.media.MediaRecorder class.
Media Player
提供的基本接口如下:
Public Methods
static MediaPlayer create(Context context, Uri uri)
Convenience method to create a MediaPlayer for a given Uri.
int getCurrentPosition()
Gets the current playback position.
int getDuration()
Gets the duration of the file.
int getVideoHeight()
Returns the height of the video.
int getVideoWidth()
Returns the width of the video.
boolean isPlaying()
Checks whether the MediaPlayer is playing.
void pause()
Pauses playback.
void prepare()
Prepares the player for playback, synchronously.
void prepareAsync()
Prepares the player for playback, asynchronously.
void release()
Releases resources associated with this MediaPlayer object.
void reset()
Resets the MediaPlayer to its uninitialized state.
void seekTo(int msec)
Seeks to specified time position.
void setAudioStreamType(int streamtype)
Sets the audio stream type for this MediaPlayer.
void setDataSource(String path)
Sets the data source (file-path or http/rtsp URL) to use.
void setDisplay(SurfaceHolder sh)
Sets the SurfaceHolder to use for displaying the video portion of the media.
void setVolume(float leftVolume, float rightVolume)
Sets the volume on this player.
void start()
Starts or resumes playback.[!--empirenews.page--]
void stop()
Stops playback after playback has been stopped or paused.
我們可以看出MediaPlayer類(lèi)提供了一個(gè)多媒體播放器的基本操作,播放,暫停,停止,設(shè)置音量等等。
簡(jiǎn)單的例子:
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();
Media Recorder
提供的基本接口如下:
Public Method:
void prepare()
Prepares the recorder to begin capturing and encoding data.
void release()
Releases resources associated with this MediaRecorder object.
void reset()
Restarts the MediaRecorder to its idle state.
void setAudioEncoder(int audio_encoder)
Sets the audio encoder to be used for recording.
void setAudioSource(int audio_source)
Sets the audio source to be used for recording.
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 setPreviewDisplay(Surface sv)
Sets a Surface to show a preview of recorded media (video).
void start()
Begins capturing and encoding data to the file specified with setOutputFile().
void stop()
Stops recording.
簡(jiǎn)單的例子:
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
整體的結(jié)構(gòu)如下圖所示:
l MediaPlayer JNI
代碼位置 /frameworks/base/media/jni
l MediaPlayer (Native)
代碼位置 /frameworks/base/media/libmedia
l MediaPlayerService (Server)
代碼位置 /frameworks/base/media/libmediaplayerservice
l MediaPlayerService Host Process
代碼位置 /frameworks/base/media/mediaserver/main_mediaserver.cpp
l PVPlayer
代碼位置 /external/opencore/android/
實(shí)際調(diào)用過(guò)程如下圖所示: