Skip to content
tusantrey edited this page Jul 3, 2022 · 4 revisions

characteristic

Function MediaPlayer IjkPlayer ExoPlayer
Adjust the aspect ratio support support support
Slide to adjust playback progress, sound, brightness support support support
Double tap to play, pause support support support
Gravity sensing to automatically enter/exit fullscreen and manually enter/exit fullscreen support support support
Double speed playback not support support support
Video screenshots (not supported when using SurfaceView, TextureView is used by default) 支持 支持 支持
The list widget can be played globally by floating 支持 支持 支持
Play a list of videos continuously 支持 支持 支持
广告播放 支持 支持 支持
边播边缓存,使用了 AndroidVideoCache 实现 支持 支持 支持
弹幕,使用 DanmakuFlameMaster 实现 支持 支持 支持
多路播放器同时播放 支持 支持 支持
没有任何控制UI的纯播放 支持 支持 支持
Android 8.0画中画 支持 支持 支持
无缝衔接播放 支持 支持 支持
抖音,实现预加载 支持 支持 支持

简单使用

建议使用之前先把demo跑一下,我对很多使用姿势都进行了演示。

1.添加类库

gradle

repositories {
    mavenCentral()
}

dependencies {
    # Required, the internal system mediaplayer is used for decoding by default
    implementation 'xyz.doikki.android.dkplayer:dkplayer-java:3.3.6'

    # Optional, contains an implementation of StandardVideoController
    implementation 'xyz.doikki.android.dkplayer:dkplayer-ui:3.3.6'

    # Optional, use exoplayer for decoding
    implementation 'xyz.doikki.android.dkplayer:player-exo:3.3.6'

    # Optional, use ijkplayer for decoding
    implementation 'xyz.doikki.android.dkplayer:player-ijk:3.3.6'
    
    # Optional, if you need caching or Douyin preloading function, please import this library
    implementation 'xyz.doikki.android.dkplayer:videocache:3.3.6'
}

or download the library and import it into the project for use

Notice:

  • 3.0.4 and later versions support both androidx and support library。
  • 3.1.2及以后的版本适配了刘海屏。
  • 3.1.3及之后的版本已经将ijkplayer的so库合并到了player-ijk这个库里面了,开发者可通过abiFilters来筛选你需要的so,例如:
ndk {
    abiFilters "arm64-v8a"
}

2.添加布局

<xyz.doikki.videoplayer.player.VideoView
        android:id="@+id/player"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

注意:一定要定死宽高

3.设置视频地址、控制器等

videoView.setUrl(URL_VOD); //设置视频地址
StandardVideoController controller = new StandardVideoController(this); 
controller.addDefaultControlComponent("标题", false);
videoView.setVideoController(controller); //设置控制器
videoView.start(); //开始播放,不调用则不自动播放

4.在Activity

@Override
    protected void onPause() {
        super.onPause();
        videoView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        videoView.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoView.release();
    }
    

    @Override
    public void onBackPressed() {
        if (!videoView.onBackPressed()) {
            super.onBackPressed();
        }
    }

5.在AndroidManifest.xml

<activity
    android:name=".PlayerActivity"
    android:configChanges="orientation|screenSize|keyboardHidden"
    android:screenOrientation="portrait" /> <!-- or android:screenOrientation="landscape"-->

切换播放核心

首先需要引入相关依赖,需要注意,播放器内部默认使用系统MediaPlayer进行解码

全局切换

建议在Application中调用

 VideoViewManager.setConfig(VideoViewConfig.newBuilder()
         //使用使用IjkPlayer解码
         .setPlayerFactory(IjkPlayerFactory.create())
         //使用ExoPlayer解码
         .setPlayerFactory(ExoMediaPlayerFactory.create())
         //使用MediaPlayer解码
         .setPlayerFactory(AndroidMediaPlayerFactory.create())
         .build());

临时切换

调用VideoView中的setPlayerFactory方法,需要在start方法之前调用方可生效

//使用IjkPlayer解码
mVideoView.setPlayerFactory(IjkPlayerFactory.create());
//使用ExoPlayer解码
mVideoView.setPlayerFactory(ExoMediaPlayerFactory.create());
//使用MediaPlayer解码
mVideoView.setPlayerFactory(AndroidMediaPlayerFactory.create());

Advanced usage

reference demo ╰( ̄▽ ̄)╭

obfuscate

-keep class xyz.doikki.videoplayer.** { *; }
-dontwarn xyz.doikki.videoplayer.**

# IjkPlayer
-keep class tv.danmaku.ijk.** { *; }
-dontwarn tv.danmaku.ijk.**

# ExoPlayer
-keep class com.google.android.exoplayer2.** { *; }
-dontwarn com.google.android.exoplayer2.**

Clone this wiki locally