Android的bootanimation用于控制顯示開(kāi)機(jī)的啟動(dòng)動(dòng)畫(huà)较锡,但是由于Android源碼里只支持一幀一幀的顯示圖片盗迟,從而達(dá)到動(dòng)畫(huà)的效果, bootanimation本身并不支持播放mp4. 那對(duì)于一些對(duì)于流暢度要求很高,或者想要做一些非匙麽酷炫的動(dòng)畫(huà)效果, bootanimation怕是不行了惦积。
那如果把動(dòng)畫(huà)效果做成mp4視頻昔馋,然后在bootanimation里播放筹吐,那會(huì)不會(huì)是一種不錯(cuò)的解決方案呢?
本篇文章主要是對(duì)于bootanimation播放mp4的可行性進(jìn)行分析秘遏。
PS: 筆者已經(jīng)在7.0上試驗(yàn)成功了
轉(zhuǎn)載請(qǐng)標(biāo)注出處: http://www.reibang.com/p/d3e8f1378846
1. Android的啟動(dòng)過(guò)程
安卓的啟動(dòng)過(guò)程大致可以分為三個(gè)階段
- bootloader
- kernel
- init
bootloader啟動(dòng)時(shí)會(huì)顯示一些文字
kernel啟動(dòng)時(shí)會(huì)顯示一張靜態(tài)圖片
init啟動(dòng)時(shí)會(huì)在bootanimation里顯示動(dòng)畫(huà)
老羅的Android系統(tǒng)的開(kāi)機(jī)畫(huà)面顯示過(guò)程分析對(duì)這三個(gè)階段講解得非常清楚丘薛,大家可以去拜讀一下。
2. bootanimation是什么
bootanimation是一個(gè)可執(zhí)行進(jìn)程垄提,位于 /system/bin下
bootanimation的代碼位置: frameworks/base/cmds/bootanimation
在Android 7.0, bootanimation.rc并不是直接放到了init.rc里面榔袋,它放在/etc/init/下,作為一個(gè)單獨(dú)的rc配置文件存在铡俐。
那么bootanimation.rc什么時(shí)候被解析呢
在 init 進(jìn)程里, 準(zhǔn)確說(shuō)是在builtins.cpp
文件里被解析的
static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Parser& parser = Parser::GetInstance();
if (end_index <= start_index) {
// Use the default set if no path is given
//init_directories是保存xxx.rc的地方,
static const std::vector<std::string> init_directories = {
"/system/etc/init",
"/vendor/etc/init",
"/odm/etc/init"
};
for (const auto& dir : init_directories) {
parser.ParseConfig(dir);
}
} else {
for (size_t i = start_index; i < end_index; ++i) {
parser.ParseConfig(args[i]);
}
}
}
3. bootanimation啟動(dòng)
bootanimation.rc文件定義如下
service bootanim /system/bin/bootanimation
class core
user graphics
group graphics audio
disabled
oneshot
從定義可以看出, bootanimation被定義成了init進(jìn)程的一個(gè)service.妥粟,它屬于 core類(lèi)审丘,只能啟動(dòng)運(yùn)行一次(oneshot), 是disabled的, 意思是 class_start core時(shí),并不會(huì)啟動(dòng)它勾给。
那bootanimation是在什么時(shí)候被啟動(dòng)的呢滩报?
在 SurfaceFlinger初始化的最后
void SurfaceFlinger::init() {
// start boot animation
startBootAnim();
}
void SurfaceFlinger::startBootAnim() {
// start boot animation
property_set("service.bootanim.exit", "0");
property_set("ctl.start", "bootanim");
}
startBootAnim
設(shè)置了ctl.start=bootanim
這個(gè)property, 從而會(huì)觸發(fā)init中正在監(jiān)聽(tīng)property的handle_property_set_fd
函數(shù)
簡(jiǎn)化后如下
static void handle_property_set_fd()
{
switch(msg.cmd) {
case PROP_MSG_SETPROP:
if (memcmp(msg.name,"ctl.",4) == 0) {
if (check_control_mac_perms(msg.value, source_ctx, &cr)) {
handle_control_message((char*) msg.name + 4, (char*) msg.value);
}
}
}
void handle_control_message(const std::string& msg, const std::string& name) {
Service* svc = ServiceManager::GetInstance().FindServiceByName(name);
//找到 bootanim 的service (由bootanim.rc定義), 然后start
if (msg == "start") {
svc->Start();
} else if (msg == "stop") {
svc->Stop();
} else if (msg == "restart") {
svc->Restart();
}
}
這里先通過(guò)name也就是bootanim去查找service, 然后start這個(gè)service, 這里bootanimation就開(kāi)始運(yùn)行起來(lái)了。
bootanimation的具體代碼的執(zhí)行可以參考老羅的分析播急,在這里就不重復(fù)造輪子了脓钾。
4. bootanimation播放mp4分析
既然 bootanimation 要播放 mp4 文件,那么肯定得相關(guān)服務(wù)已經(jīng)啟動(dòng)了才行桩警,如 MediaPlayer相關(guān). 通過(guò)對(duì)init的log分析 (adb shell dmesg)可训,可以發(fā)現(xiàn) audioserver
media
相關(guān)的服務(wù)已經(jīng)于bootanim先啟動(dòng)起來(lái),這樣就可以得到 Mediaplayer在bootanim里能正常的播放多媒體文件捶枢。