Android Audio Architecture 圖如下所示
詳細(xì)調(diào)用路徑如下
1. 音頻播放
示例代碼
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(PATH_TO_FILE);
mp.prepare();
mp.start();
1.1 MediaPlayer mp = new MediaPlayer()
函數(shù)調(diào)用
|
文件
|
MediaPlayer:: MediaPlayer
|
MediaPlayer.Java
|
MediaPlayer::native_setup
|
MediaPlayer.Java
|
android_media_MediaPlayer_native_setup
|
android_media_MediaPlayer.cpp
|
MediaPlayer::MediaPlayer
|
MediaPlayer.cpp
|
1.2 mp.setDataSource(PATH_TO_FILE)
函數(shù)調(diào)用
|
文件
|
MediaPlayer:: setDataSource
|
MediaPlayer.Java
|
MediaPlayer::setDataSource
|
MediaPlayer.Java
|
android_media_MediaPlayer_setDataSource
|
android_media_MediaPlayer.cpp
|
MediaPlayer:: setDataSource
|
MediaPlayer.cpp
|
MediaPlayer::getMediaPlayerService
|
MediaPlayer.cpp
|
IMediaPlayerService
|
IMediaPlayerService.h
|
MediaPlayerService:: MediaPlayerService[f1]
|
MediaPlayerService.cpp
|
MediaPlayerService::create
|
MediaPlayerService.cpp
|
MediaPlayerService::Client::Client
|
MediaPlayerService.cpp
|
MediaPlayerService::Client::setDataSource
|
MediaPlayerService.cpp
|
getPlayerType[f2]
|
MediaPlayerService.cpp
|
MediaPlayerService::Client::createPlayer
|
MediaPlayerService.cpp
|
createPlayer
|
MediaPlayerService.cpp
|
PVPlayer
|
PVPlayer.h
|
MidiFile
|
MidiFile.cpp
|
VorbisPlayer
|
VorbisPlayer.cpp
|
AudioOutput::AudioOutput
|
MediaPlayerService.cpp
|
1.3 mp.prepare()
函數(shù)調(diào)用
|
文件
|
MediaPlayer:: prepare
|
MediaPlayer.Java
|
MediaPlayer:: prepare
|
MediaPlayer.Java
|
android_media_MediaPlayer_ prepare
|
android_media_MediaPlayer.cpp
|
MediaPlayer:: prepare
|
MediaPlayer.cpp
|
PVPlayer:: prepare
|
PVPlayer.h
|
MidiFile:: prepare
|
MidiFile.cpp
|
VorbisPlayer:: prepare
|
VorbisPlayer.cpp
|
VorbisPlayer::createOutputTrack
|
VorbisPlayer.cpp
|
AudioOutput::open
|
MediaPlayerService.cpp
|
AudioTrack::AudioTrack
|
AudioTrack.cpp
|
AudioSystem::get_audio_flinger
|
AudioSystem.cpp
|
AudioFlinger::createTrack
|
AudioFlinger.cpp
|
1.4 mp.start()
函數(shù)調(diào)用
|
文件
|
MediaPlayer:: start
|
MediaPlayer.Java
|
MediaPlayer:: start
|
MediaPlayer.Java
|
android_media_MediaPlayer_ start
|
android_media_MediaPlayer.cpp
|
MediaPlayer:: start
|
MediaPlayer.cpp
|
PVPlayer:: start
|
PVPlayer.h
|
MidiFile:: p start
|
MidiFile.cpp
|
VorbisPlayer:: start
|
VorbisPlayer.cpp
|
AudioTrack::start
|
AudioTrack.cpp
|
[f1]由systemserver進(jìn)程創(chuàng)建,在文件system_init.cpp里的函數(shù)system_init()調(diào)用MediaPlayerService::instantiate創(chuàng)建
[f2]返回3種player
PV_PLAYER:播放mp3
SONIVOX_PLAYER:播放midi
VORBIS_PLAYER:播放ogg,
簡單說來,軌跡如下:
Java端發(fā)起調(diào)用,MediaPlayer會轉(zhuǎn)至MediaPlayerService,然后會調(diào)用相應(yīng)的解碼工具解碼后創(chuàng)建AudioTrack,所有待輸出的AudioTrack在AudioFlinger::AudioMixer里合成,然后通過AudioHAL(AudioHardwareInterface的實際實現(xiàn)者)傳至實際的硬件來實現(xiàn)播放