系统环境

  • Android
  • API 15

视频生成

1
ffmpeg -f image2 -r 15 -i %*.png -vcodec mpeg4 bootanimation.mp4

代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
commit 88044e119ffb0868bf1bcfbdd840611ca2a9f21d
Author: fantasy <fantasy@xxx.com>
Date:   Fri Jun 8 16:41:56 2018 +0800

    support bootanimation.mp4

diff --git a/cmds/bootanimation/Android.mk b/cmds/bootanimation/Android.mk
index 7d39912..60fd11d 100644
--- a/cmds/bootanimation/Android.mk
+++ b/cmds/bootanimation/Android.mk
@@ -15,7 +15,8 @@ LOCAL_SHARED_LIBRARIES := \
 	libskia \
     libEGL \
     libGLESv1_CM \
-    libgui
+    libgui \
+    libmedia
 
 LOCAL_C_INCLUDES := \
 	$(call include-path-for, corecg graphics)
diff --git a/cmds/bootanimation/BootAnimation.cpp b/cmds/bootanimation/BootAnimation.cpp
index 154dbb8..f171191 100644
--- a/cmds/bootanimation/BootAnimation.cpp
+++ b/cmds/bootanimation/BootAnimation.cpp
@@ -55,6 +55,9 @@
 #define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
 #define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
 
+#define  BOOTANIMATION_VIDEO  "/system/media/bootanimation.mp4"
+#include <media/mediaplayer.h>
+
 namespace android {
 
 // ---------------------------------------------------------------------------
@@ -262,6 +265,7 @@ status_t BootAnimation::readyToRun() {
 
     mAndroidAnimation = true;
 
+    mVideo = false;
     // If the device has encryption turned on or is in process 
     // of being encrypted we show the encrypted boot animation.
     char decrypt[PROPERTY_VALUE_MAX];
@@ -269,6 +273,10 @@ status_t BootAnimation::readyToRun() {
 
     bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
 
+    if (access(BOOTANIMATION_VIDEO, R_OK) == 0){
+       mVideo = true;
+       }
+
     if ((encryptedAnimation &&
             (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
             (mZip.open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE) == NO_ERROR)) ||
@@ -287,11 +295,16 @@ status_t BootAnimation::readyToRun() {
 bool BootAnimation::threadLoop()
 {
     bool r;
+    if (mVideo) {//这里的 mVideo 是一个标志位,表示是否有开机视频
+        r = video();
+    }
+    else {
     if (mAndroidAnimation) {
         r = android();
     } else {
         r = movie();
     }
+    }
 
     eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
     eglDestroyContext(mDisplay, mContext);
@@ -366,6 +379,40 @@ bool BootAnimation::android()
     return false;
 }
 
+bool BootAnimation::video()
+{
+    const float MAX_FPS = 60.0f;
+    const bool LOOP = true;
+    const float CHECK_DELAY = ns2us(s2ns(1) / MAX_FPS);
+
+    eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+    eglDestroySurface(mDisplay, mSurface);
+    #if 0
+    float asp = 1.0f * mWidth / mHeight;
+    SurfaceComposerClient::openGlobalTransaction();
+    mFlingerSurfaceControl->setPosition(mWidth, 0);
+    mFlingerSurfaceControl->setMatrix(0, 1 / asp, -asp, 0);
+    SurfaceComposerClient::closeGlobalTransaction();
+    #endif
+    sp<MediaPlayer> mp = new MediaPlayer();
+    mp->reset();
+    mp->setDataSource(BOOTANIMATION_VIDEO, NULL); //设置资源
+    mp->setLooping(false); //是否循环播放
+    Parcel*  _parcel = new Parcel;
+    mp->setParameter(100, *_parcel);
+    mp->setVideoSurfaceTexture(mFlingerSurface->getSurfaceTexture());
+    mp->prepare(); //准备,同步
+    mp->start(); //播放
+    #if 1
+    while(true) {
+        if(exitPending())
+            break;
+        usleep(CHECK_DELAY);
+    }
+    #endif
+    mp->stop();
+    return false;
+}
 
 bool BootAnimation::movie()
 {
diff --git a/cmds/bootanimation/BootAnimation.h b/cmds/bootanimation/BootAnimation.h
index 8e28bba..013fc30 100644
--- a/cmds/bootanimation/BootAnimation.h
+++ b/cmds/bootanimation/BootAnimation.h
@@ -95,6 +95,8 @@ private:
     sp<Surface> mFlingerSurface;
     bool        mAndroidAnimation;
     ZipFileRO   mZip;
+    bool        mVideo;
+    bool        video();
 };
 
 // ---------------------------------------------------------------------------