PS: No matter what you do, practice is the most important. The experience accumulated through personal practice cannot be compared to casually reading a technical article.
Recently, it may be necessary to play videos in the project, and later it may also need to support playing videos with the rtsp protocol. After weighing the options, it is decided to compile Bilibili's open-source ijkplayer. ijkplayer is a lightweight cross-platform player based on ffmpeg that can be used on Android and iOS. It can achieve support for more formats through compilation, and it can be said that ijkplayer supports any format supported by ffmpeg.
Initially, Cygwin was used for compilation, but there were always errors when generating the .so file. Of course, there were many pitfalls in between, so it was decided to compile ijkplayer in the Ubuntu environment. There were basically no problems during compilation in the Ubuntu environment, and the compilation process is as follows:
- Preparation
- Configure environment variables
- Install necessary components
- Formal compilation
- Run ijkplayer
Preparation#
Install VMware virtual machine and Ubuntu system. After installing VMware, create a virtual machine and choose the typical installation mode, as shown in the figure below:
Then click Next and select the downloaded system image, as shown in the figure below:
After selecting correctly, the image information will be displayed. For example, I chose Ubuntu 64-bit 18.04. Then continue to the next step, as shown in the figure below:
Fill in the username, password, and other information, and click Next, as shown in the figure below:
Fill in the virtual machine name and the location where the virtual machine will be installed, and click Next:
Set the virtual machine disk size. To avoid reducing disk performance, choose to store the disk as a single file, and then click Next, as shown in the figure below:
The Ubuntu virtual machine is created here. Click Finish and wait for the Ubuntu installation to complete. Enter the set password to enter the Ubuntu system, as shown in the figure below:
In addition, you need to download the Linux version of Android SDK and NDK. Here, I chose android-sdk_r24.4.1-linux.tgz and android-ndk-r10e-linux-x86_64.zip. After downloading, you can use the following commands to unzip the files:
unzip xxx.zip
tar -xvf xxx.tgz
Remember not to put the NDK directory in the shared directory of the virtual machine. To ensure smooth compilation, the NDK directory should be placed in the system directory of Ubuntu, which is the directory under /home/username.
Configure environment variables#
In /home/username/ under Ubuntu, press Ctrl+h to view the .bashrc file and configure the SDK and NDK environment variables, as follows:
NDK=/home/jzman/android/android-ndk-r10e
export NDK
ADB=/home/jzman/android/android-sdk-linux/platform-tools
export ADB
# Paths of ANDROID_NDK and ANDROID_SDK
ANDROID_NDK=/home/jzman/android/android-ndk-r10e
export ANDROID_NDK
ANDROID_SDK=/home/jzman/android/android-sdk-linux
export ANDROID_SDK
# Add to PATH
PATH=${PATH}:${NDK}:${ADB}:${ANDROID_NDK}:${ANDROID_SDK}
After configuring, save and close .bashrc, open Terminal, and enter ndk-build -v to check if the ndk is configured successfully. If the following log is displayed, the configuration is successful:
jzman@ubuntu:~$ ndk-build -v
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for x86_64-pc-linux-gnu
Install necessary components#
Enter the following commands one by one to update and install git, yasm, and make:
sudo apt-get update
sudo apt install git
sudo apt install yasm
sudo apt install make
Use git --version and make -v to check if git and make tools are installed successfully. If successful, the corresponding version numbers will be displayed, as shown below:
jzman@ubuntu:~$ git --version
git version 2.17.1
jzman@ubuntu:~$ make -v
GNU Make 4.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
jzman@ubuntu:~$
Formal compilation#
// Clone ijkplayer source code
git clone https://github.com/Bilibili/ijkplayer.git ijkplayer
cd ijkplayer
git checkout -B latest k0.8.8
// Use the lighter module-lite.sh
cd ijkplayer/config
rm module.sh
ln -s module-lite module.sh
// Download ffmpeg source code
cd ijkplayer
./init-android
// Compile ffmpeg
./compile-ffmpeg.sh clean
./compile-ffmpeg.sh all
// Compile ijkplayer and generate .so files
cd ijkplayer/android
./compile-ijk.sh all
If you want to support HTTPS, execute the following command during compilation:
cd ijkplayer
./init-android-openssl.sh (support HTTPS)
cd ijkplayer/android/contrib
./compile-openssl.sh clean
./compile-openssl.sh all
After successful compilation, the corresponding Android project will be generated under ijkplayer/android, as shown in the figure below:
Check whether the corresponding .so files are generated in each ABI library, such as ijkplayer/android/ijkplayer/ijkplayer-arm64/src/main/libs. Taking arm64 as an example, it is shown in the figure below:
Run ijkplayer#
Use Android Studio to open the compiled Android project. The screenshot of running is as follows:
The compilation of ijkPlayer ends here. If you have any questions, please leave a comment.