Recently exposed to some audio development operations and a tripartite library for audio development: github: github.com/Bilibili/ij… One might ask why a tripartite library is used: the most immediate reason, of course, is that the system’s MediaPlayer does not support many formats or is version limited. The format only support: developer.android.com/intl/zh-cn/… Blog.csdn.net/ddna/articl… Ijkplayer is based on FFMPEG, which is a very powerful library. It is said here that some live video programs are using this player.

1. Gradle import

# required allprojects { repositories { jcenter() } } dependencies { # required, Enough for most devices. The compile 'TV. Danmaku. Ijk. Media: ijkplayer - Java: 0.5.0' compile 'TV. Danmaku. Ijk. Media: ijkplayer - armv7a: 0.5.0' # Other ABIs: Optional compile 'TV. Danmaku. Ijk. Media: ijkplayer - armv5:0.5.0' compile 'TV. Danmaku. Ijk. Media: ijkplayer - arm64:0.5.0' The compile 'TV. Danmaku. Ijk. Media: ijkplayer - x86:0.5.0' compile 'TV. Danmaku. Ijk. Media: ijkplayer - x86_64:0.5.0' # ExoPlayer as IMediaPlayer: optional, experimental compile 'TV. Danmaku. Ijk. Media: ijkplayer - exobiology: 0.5.0'}Copy the code

2. Ijkplayer supports:

First of all, can see: MediaPlayer document developer.android.com/reference/a…

1. The basic operations of MediaPlayer are supported and their parameters are consistent, for example:

public native boolean isPlaying(); 
public native void seekTo(long msec) throws IllegalStateException;
public native long getCurrentPosition();
public native long getDuration();
public void pause() throws IllegalStateException 
public void prepareAsync() throws IllegalStateException
public void start() throws IllegalStateExceptionCopy the code

These methods are in the IjkMediaPlayer class and are ultimately implemented by jni calling the underlying c++ code. There are also some advanced uses, but I’m going to dig a hole for you at the same time, because I’m only using basic functions.

2. Small pit:

Ijkplayer returns true if (1) the isPlaying method is prepared or (2) the isPlaying method isPlaying.

 if (mp->mp_state == MP_STATE_PREPARED ||
        mp->mp_state == MP_STATE_STARTED) {
        return true;
    }Copy the code

3. The CallBack interface is basically the same.

interface OnPreparedListener {
        void onPrepared(IMediaPlayer mp);
    }

    interface OnCompletionListener {
        void onCompletion(IMediaPlayer mp);
    }

    interface OnBufferingUpdateListener {
        void onBufferingUpdate(IMediaPlayer mp, int percent);
    }

    interface OnSeekCompleteListener {
        void onSeekComplete(IMediaPlayer mp);
    }

    interface OnVideoSizeChangedListener {
        void onVideoSizeChanged(IMediaPlayer mp, int width, int height,
                                int sar_num, int sar_den);
    }

    interface OnErrorListener {
        boolean onError(IMediaPlayer mp, int what, int extra);
    }

    interface OnInfoListener {
        boolean onInfo(IMediaPlayer mp, int what, int extra);
    }Copy the code

3. Special requirements, this has nothing to do with player.

1. Monitor earphone plug and unplug, for example, close the player when the earphone is unplugged. May refer to:

Blog.csdn.net/thl789/arti…

2. Pause when switching audio sources or playing other resources.

Concrete is to realize the AudioManager. OnAudioFocusChangeListener interface methods: public void onAudioFocusChange (int focusChange) continue to steal lazy, not repeat wrote. Details to give a reader according to actual situation to make a decision: www.jianshu.com/p/32a673293…

4 A recent pit encountered: ANR

ANR is not complete without ANR. Recently, when using version 0.4.5, the product needs to switch the playback source. If it switches quickly, it will cause ANR. So far I have read the official issue’s suggestion is to create a new ijkMeadiaPlayer without calling reset and only call release (). There is also talk of using reset in threads. One of my workaround is deferred execution.

Photoshop another pit.

Ijkplayer does not support the launch of unprepared, seek progress. (it is said to be related to FFMPEG, unknowingly). Change the source and fast forward.Copy the code

(1) I will delay seek after onPrepared. (2) I will delay seek after onPrepared