ijkPlayer的基本使用(一)

https://github.com/Bilibili/ijkplayer
项目github链接
很活跃的一个项目,评价也不错。

第一次使用,看到没有官方的文档,就简单写一下使用。
(没有自己编译的需求,所以不会涉及这方面)

  • build.gradle中加入依赖
/*ijkPlayer*/
    // required, enough for most devices.
    compile 'tv.danmaku.ijk.media:ijkplayer-java:0.7.8.1'
    compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.7.8.1'

    // Other ABIs: optional
    compile 'tv.danmaku.ijk.media:ijkplayer-armv5:0.7.8.1'
    compile 'tv.danmaku.ijk.media:ijkplayer-arm64:0.7.8.1'
    compile 'tv.danmaku.ijk.media:ijkplayer-x86:0.7.8.1'
    compile 'tv.danmaku.ijk.media:ijkplayer-x86_64:0.7.8.1'

    // ExoPlayer as IMediaPlayer: optional, experimental
    compile 'tv.danmaku.ijk.media:ijkplayer-exo:0.7.8.1'
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

64位的支持最低到API21, 这里我手头的手机需要用到,所以就留下了。
开始想的是去掉**64,但是运行报这样的错误:

java.lang.UnsatisfiedLinkError: …….(一堆) couldn’t find “libijkffmpeg.so”
在issues里看到也有人遇到过:https://github.com/Bilibili/ijkplayer/issues/1489
感兴趣的可以看看。

布局:

    <tv.danmaku.ijk.media.widget.media.IjkVideoView
        android:id="@+id/ijkplayer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

初始化:

        //init player
        IjkMediaPlayer.loadLibrariesOnce(null);
        IjkMediaPlayer.native_profileBegin("libijkplayer.so");
        if (mVideoPath != null) {
            mVideoView.setVideoPath(mVideoPath);//mVideoPath为视频地址
        } 
        mVideoView.start();
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

退出时要做销毁:

 @Override
    public void onBackPressed() {
        mBackPressed = true;
        super.onBackPressed();
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (mBackPressed || !mVideoView.isBackgroundPlayEnabled()) {
            mVideoView.stopPlayback();
            mVideoView.release(true);
            mVideoView.stopBackgroundPlay();
        } else {
            mVideoView.enterBackground();
        }
        IjkMediaPlayer.native_profileEnd();
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

写完一会发现飘红…… 我是直接把项目里的代码(tv.danmaku.ijk.media下)复制过来了,你也可以去下载别人编译好的jar 放到libs目录下
代码就不上传了,直接去看官方的Demo就好。建个传送门把,给迷路的孩子 https://github.com/Bilibili/ijkplayer/tree/master/android/ijkplayer/ijkplayer-example

暂时先写这么多,只是最基本的视频播放,可能视频比例还有问题,也没有控制在里边,等我完成后再继续写……

发布了272 篇原创文章 · 获赞 165 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_32534441/article/details/104869456
今日推荐