Quote:

The company’s current radio project is my second contact with audio playback project, Many Android audio and video playback still use MediaPlayer(except medium and large factories), but if you have used MediaPlayer, many developers will ridicule how much crater, even Google officially recommended using ExoPlayer… Encountered the most pit is playing online audio and video, the network is not good will always load, and then more easy to ANR… Before has always wanted to change the audio playback, through their own search and ask others, and the following several programs (small workshops can not be another to write a codec playback library) :

  • ExoPlayer: Demo is incredibly complex… And there’s not a lot of information online, pass;
  • Vitamio: Vitamio is free for personal use and commercial use, so I just pass.
  • Compile ffmpeg yourself: sounds complicated, also Pass;
  • Ijkplayer: large B station open source based on FFmpeg lightweight Android/iOS video player, online information quite a lot, and the official maintenance, although there are more than 1600 issues, and mediaPlayer interface, learning costs are not high, can be added to try the water!

Finally, I decided to use iJkPlayer instead of MediaPlayer!


1. How to use ijkPlayer

Official: github.com/Bilibili/ij…

Build. Gradle adds the following dependency reference:

dependencies {
    This is sufficient for most devices
    compile 'TV. Danmaku. Ijk. Media: ijkplayer - Java: 0.8.4'
    compile 'TV. Danmaku. Ijk. Media: ijkplayer - armv7a: 0.8.4'

    # Other ABIs: Optional
    compile 'TV. Danmaku. Ijk. Media: ijkplayer - armv5:0.8.4'
    compile 'TV. Danmaku. Ijk. Media: ijkplayer - arm64:0.8.4'
    compile 'TV. Danmaku. Ijk. Media: ijkplayer - x86:0.8.4'
    compile 'TV. Danmaku. Ijk. Media: ijkplayer - x86_64:0.8.4'

    # ExoPlayer as IMediaPlayer: optional, experimental
    compile 'TV. Danmaku. Ijk. Media: ijkplayer - exobiology: 0.8.4'
}
Copy the code

Here are a few things to clear up any doubts you might have:

Armv7a, ARMV5, ARM64, x86, x86_64 are the corresponding CPU architectures. Generally speaking, it is enough to prepare an ARMV7A library. If the system cannot find the CORRESPONDING CPU architecture, it will go to Armeabi. But this is also accompanied by an increase in the size of apK, which you need to weigh!! Anyway, I only have one: ARMV7A, temporarily did not find what not to accept!

And the way to use MediaPlayer is pretty much the same, so I don’t have to tell you how to use it. Here’s a problem I had:

Ijkplayer does not support HTTPS by default

If you try to use ijkPlayer to play audio starting with Https, you will get this error:

I had no choice but to compile the source code for ijkPlayer. I was baffled and hit a wall when I compiled the source code for ijkPlayer.


2. Compile the IjkPlayer that supports Https

Don’t ask me how to compile for Windows, I only know how to compile for Ubuntu and MAC! Ubuntu 14.04 and MAC OS 10.13 have all been compiled successfully for Ubuntu 14.04 and MAC OS 10.13.

Don’t clone the project to an external hard drive. For example, my 120GB SSD has a 1TB mechanical hard drive. Clone the project to the mechanical hard drive at first, and then build a bunch of problems, such as ln can’t establish a link, chmod command is not working, etc. Green light all the way!


  • Step 1: Install Git and yASM.
sudo apt-get install git
sudo apt-get install yasm
Copy the code

  • Step 2: Download and configure the SDK and NDK

SDK will not say, you develop android will certainly some, general is not the default download the NDK, here also do not recommend you to use the SDK Manager to download the NDK, tried before have some problem, suggestion to download website: developer. The android. Google. Cn/the NDK/downloa… The minimum version of NDK is 10E, currently NDK 15 is not supported!

Next, configure the environment variables:

Ubuntu:

Bashrc file, configure SDK and NDK:

Then source. bashrc, type ndk-build -v to see if anything is output to verify that the configuration works.

MAC:

Open the terminal, CD to the root directory (CD ~), and create a **.bash_profile** file: do the following

Then type source. bash_profile and type nk-build -v to verify:


  • Step 3: Pull the iJkPlayer source code
git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-android
cdIjkplayer - Android Git Checkout -B latest k0.8.4Copy the code

  • Step 4: Initialize Android
./init-android.sh
Copy the code

  • Step 5: Compile the script configuration

Config /module.sh configures the options for automatic compilation, such as protocols and audio/video types, in the config/module.sh file.

In addition, the official provided three templates for us to use:

Module-default. sh: default, use this if you like more types; Module-lite-hevc. sh: If you prefer a smaller binary size codec/format (including heVC functionality) Module-lite. sh: If you prefer a smaller binary size codec/format (default)

Module-lite.sh is the smallest, so it’s easy to use:

rm module.sh
ln -s module-lite.sh module.sh
source module.sh
Copy the code

You can also open module.sh to modify it yourself. For example, I only want it to support MP3, but other formats are not supported.


  • Step 6: Initialize android to support Https
cd. ./init-android-openssl.shCopy the code

Note: If the NDK or SDK cannot be found, you can run source ~/.bash_profile


  • Clear a wave
cd android/contrib
./compile-openssl.sh clean
./compile-ffmpeg.sh clean
Copy the code

  • Step 8: Compile OpenSSL
./compile-openssl.sh all
Copy the code

  • Step 9: Compile FFMPEG

/compile-ffmpeg.sh armv7a./compile-ffmpeg.sh armv7a./compile-ffmpeg.sh armv7a

./compile-ffmpeg.sh all
Copy the code

  • Build iJkPlayer

All builds all so libraries by default, armv7A builds only by default!

./compile-ijk.sh all
Copy the code

The ijkPlayer project will be generated in the following directory:

Here’s how to use the ijkPlayer:

At first, I thought I could just put the so library into my project liBS. As a result, all kinds of compilations reported errors. I really believe that, big guys don’t write articles in a bad way!!

Compile project(‘: ijkPlayer ‘) in build.gradle. Then you can use it.

compile 'TV. Danmaku. Ijk. Media: ijkplayer - Java: 0.8.4'
compile 'TV. Danmaku. Ijk. Media: ijkplayer - armv7a: 0.8.4'
Copy the code

Delete these dependencies, or you will still report that HTTPS is not supported! This is usually enough to quote into the project, but Piglet is not easily satisfied! So there is the following toss!


3. Delete irrelevant things and generate AAR dependent libraries

Feel a lot of irrelevant things, let’s say the pig’s expectations:

  • 1. It is only used to play music (exo and example can be omitted).
  • 2. Only need armV7A architecture (delete other architecture and put ARMV7A so library in ijkPlayer-Java);
  • 3. Retain only one ijkplayer-java file and export it as ijkplayer.aar file for your own project;

Step by step, let’s make piggy’s expectations come true:


  • Click Open Module Settings to remove all dependencies except ijkplayer-example and ijkplayer-java.

Then open ijkplayer-java/ SRC /main/ and create a new libs folder. Open ijkplayer-armv7a/main/libs and copy the armeabi-v7a folder to ijkplayer-java/ libs.

Delete everything except ijkplayer-example and ijkplayer-java. Modify the build.gradle file of ijkplayer-java, delete the last sentence, and modify the version information.

Then compile a wave of the whole project, run, click simple, randomly click a song to see if it can play, if it can play normally, then enter the next step, export aar library.


  • Step 2: Compile the AAR library

This one is easy, click on the right side of Gradle, open it in turn, right click Run

After execution, an AAR file will be generated in the build/outputs/aar directory.


  • Step 3: Add the AAR file to the project

Add a dependency to build. Gradle. (ijkplayer-java-release.aar)

implementation(name: 'ijkplayer', ext: 'aar')
Copy the code

Next, write a simple play music code in the project to test the waters, button click to play a music:

public class MusicPlayActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play);
        Button btn = findViewById(R.id.button);
        btn.setOnClickListener(v -> {
            IjkMediaPlayer player = new IjkMediaPlayer();
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setScreenOnWhilePlaying(true);
            player.setOnPreparedListener(IMediaPlayer::start);
            try {
                player.setDataSource("https:xxxx.mp3"); player.prepareAsync(); } catch (IOException e) { e.printStackTrace(); }}); }}Copy the code

If the playback is normal, it means that our porting is very successful. If you do not use modularization, this is the end. If you use modularization like ME, and also separate the audio playback into a module, app -> audio playback module -> ijkplayer.aar, congratulations, compile directly error. Can’t find AAR, 2333! To solve the problem, you need to continue to read the following


4. Modularity. Problems cannot be solved by using AAR

There are three build.gradle files that need to be modified: the audio player module, the App, and the Application level

Build. gradle for audio player module:

Build. Gradle at app level:

Build. gradle at the application level

Then build a wave of projects


summary

Take a few days, I finally successful compilation, and gain a lot, also can understand a truth: people have the fear, to learn new things always subconscious resist, feel difficult, but most of the time just seems difficult, when you go to learn, and stick to a period of time, you will find that in fact is not as difficult as you think ~

Finally, attached are the reduced IJK-Player and AAR packages, if necessary: github.com/coder-pig/i…