Android 体系团体架构:
我们先看一下多媒体框架在整个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等。
l Open Core多媒体框架有一套通用可扩展的接口针对第三方的多媒体遍解码器,输入,输出配置等等 。
l 多媒体文件的播放,下载,包括3GPP, MPEG-4,AAC and MP3 containers
l 流媒体文件的下载,及时播放,包括:3GPP, HTTP and RTSP/RTP
l 动态视频和静态图像的编码,解码,比喻:MPEG-4, H.263 and AVC (H.264), JPEG
l 语音编码格局: AMR-NB and AMR-WB
l 音频编码格局: MP3, AAC, AAC+
l 视频和图像格局: 3GPP, MPEG-4 and JPEG
l 视频集会:基于H324-M standard
图中用黄线圈出的是Media Framework
Open Core先容:
Open Core是Android 多媒体框架的内核,全部Android平台的音视频征求,播放的利用都是通过它来实现。它也被称为PV(Packet Video), Packet Video是一家专门提供多媒体办理方案的公司。
通过Open Core步调员可以方便快速的开辟出想要的多媒体应用步调,比喻:音视频的征求,回放,视频集会,及时的流媒体播放等等应用。
Open Core 框架
代码布局:
Open Core的代码在Android代码的External/Opencore目次中。这个目次是OpenCore的根目次,此中包括的子目次如下所示:
android:这内里是一个上层的库,它实现了一个为Android利用的音视频征求,播放的接口,和DRM数字版权办理的接话柄现。
baselibs:包括数据结讨论线程沉寂等内容的底层库
codecs_v2:音视频的编解码器,基于OpenMAX实现
engines:内核部分,多媒体引擎的实现
extern_libs_v2:包括了khronos的OpenMAX的头文件
fileformats:文件格局的阐发(parser)东西
nodes:提供一些PVMF的NODE,紧张是编解码和文件阐发方面的。
oscl:利用体系兼容库
pvmi: 输入输出控制的抽象接口
protocols:紧张是与网络干系的RTSP、RTP、HTTP等协议的干系内容
pvcommon:pvcommon库文件的Android.mk文件,没有源文件。
pvplayer:pvplayer库文件的Android.mk文件,没有源文件。
pvauthor:pvauthor库文件的Android.mk文件,没有源文件。
tools_v2:编译东西以及一些可注册的模块。
Open Core 上层代码布局
在实际开辟中我们并不会过多的研究Open Core的实现,Android提供了上层的Media API给开辟职员利用,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.
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();
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.
大抵的例子:
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
团体的布局如下图所示:
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/
实际调用进程如下图所示: