IjkPlayer的使用

2021-07-06  本文已影响0人  放羊娃华振

一、概述

IjkPlayer是b站开源的一块播放器,底层也是基于FFmpeg这个开源库,Ijk源码:https://github.com/Bilibili/ijkplayer.Ijkplayer-0.8.8支持rtsp、支持http、支持hls、支持rtmp、支持h265、支持arm64/armv7a。

二、简单使用(demo:https://github.com/stormdzh/IjkPlayerDemo)

1、引入依赖

    implementation 'tv.danmaku.ijk.media:ijkplayer-java:0.8.8'
    implementation 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
    implementation 'tv.danmaku.ijk.media:ijkplayer-exo:0.8.8'

    ## required, enough for most devices.
    #compile 'tv.danmaku.ijk.media:ijkplayer-java:0.8.8'
    #compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'

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

    ## ExoPlayer as IMediaPlayer: optional, experimental
    #compile 'tv.danmaku.ijk.media:ijkplayer-exo:0.8.8'

2、自定义一个播放器
ijkplayer提供的没有提供一个播放器控件给我们使用,所以我们自己定。主要是创建一个surfaceview,把它赋给IMediaPlayer。

public class VideoPlayerIJK extends FrameLayout {

    /**
     * 由ijkplayer提供,用于播放视频,需要给他传入一个surfaceView
     */
    private IMediaPlayer mMediaPlayer = null;

    /**
     * 视频文件地址
     */
    private String mPath = "";

    private SurfaceView surfaceView;

    private VideoPlayerListener listener;
    private Context mContext;

    public VideoPlayerIJK(@NonNull Context context) {
        super(context);
        initVideoView(context);
    }

    public VideoPlayerIJK(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initVideoView(context);
    }

    public VideoPlayerIJK(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initVideoView(context);
    }

    private void initVideoView(Context context) {
        mContext = context;

        //获取焦点,不知道有没有必要~。~
        setFocusable(true);
    }

    /**
     * 设置视频地址。
     * 根据是否第一次播放视频,做不同的操作。
     *
     * @param path the path of the video.
     */
    public void setVideoPath(String path) {
        if (TextUtils.equals("", mPath)) {
            //如果是第一次播放视频,那就创建一个新的surfaceView
            mPath = path;
            createSurfaceView();
        } else {
            //否则就直接load
            mPath = path;
            load();
        }
    }

    /**
     * 新建一个surfaceview
     */
    private void createSurfaceView() {
        //生成一个新的surface view
        surfaceView = new SurfaceView(mContext);
        surfaceView.getHolder().addCallback(new LmnSurfaceCallback());
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT
                , LayoutParams.MATCH_PARENT, Gravity.CENTER);
        surfaceView.setLayoutParams(layoutParams);
        this.addView(surfaceView);
    }

    /**
     * surfaceView的监听器
     */
    private class LmnSurfaceCallback implements SurfaceHolder.Callback {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            //surfaceview创建成功后,加载视频
            load();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
        }
    }

    /**
     * 加载视频
     */
    private void load() {
        //每次都要重新创建IMediaPlayer
        createPlayer();
        try {
            mMediaPlayer.setDataSource(mPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //给mediaPlayer设置视图
        mMediaPlayer.setDisplay(surfaceView.getHolder());

        mMediaPlayer.prepareAsync();
    }

    /**
     * 创建一个新的player
     */
    private void createPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.setDisplay(null);
            mMediaPlayer.release();
        }
        IjkMediaPlayer ijkMediaPlayer = new IjkMediaPlayer();
        ijkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_DEBUG);

//        //开启硬解码
        ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);

        mMediaPlayer = ijkMediaPlayer;

        if (listener != null) {
            mMediaPlayer.setOnPreparedListener(listener);
            mMediaPlayer.setOnInfoListener(listener);
            mMediaPlayer.setOnSeekCompleteListener(listener);
            mMediaPlayer.setOnBufferingUpdateListener(listener);
            mMediaPlayer.setOnErrorListener(listener);
        }
    }

    public void setListener(VideoPlayerListener listener) {
        this.listener = listener;
        if (mMediaPlayer != null) {
            mMediaPlayer.setOnPreparedListener(listener);
        }
    }

    /**
     * -------======--------- 下面封装了一下控制视频的方法
     */

    public void start() {
        if (mMediaPlayer != null) {
            mMediaPlayer.start();
        }
    }

    public void release() {
        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    public void pause() {
        if (mMediaPlayer != null) {
            mMediaPlayer.pause();
        }
    }

    public void stop() {
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
        }
    }


    public void reset() {
        if (mMediaPlayer != null) {
            mMediaPlayer.reset();
        }
    }


    public long getDuration() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.getDuration();
        } else {
            return 0;
        }
    }


    public long getCurrentPosition() {
        if (mMediaPlayer != null) {
            return mMediaPlayer.getCurrentPosition();
        } else {
            return 0;
        }
    }


    public void seekTo(long l) {
        if (mMediaPlayer != null) {
            mMediaPlayer.seekTo(l);
        }
    }
}

3、使用
添加控件到xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.stormdzh.ijkplayer.ijk.VideoPlayerIJK
        android:id="@+id/mVideoPlayerIJK"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

</RelativeLayout>

Activity中测试

public class MainActivity extends AppCompatActivity {

    private VideoPlayerIJK mVideoPlayerIJK;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mVideoPlayerIJK = findViewById(R.id.mVideoPlayerIJK);
        String videoPath = "http://v-cdn.zjol.com.cn/280443.mp4";
        mVideoPlayerIJK.setVideoPath(videoPath);
        mVideoPlayerIJK.start();
    }
}

编译
https://blog.csdn.net/Xu_JL1997/article/details/104376417

错误

ff3.4--ijk0.8.7--20180103--001版本ffmpeg编译错误 ,参考:https://github.com/Bilibili/ijkplayer/issues/4093

在module-default.sh增加:
export COMMON_FF_CFG_FLAGS="$COMMON_FF_CFG_FLAGS --disable-linux-perf"
就可以编译通过了。
原因是:
libavcodec/hevc_mvs.c最终会include libavutil/timer.h文件
#if CONFIG_LINUX_PERF
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h> // read(3)
#include <sys/ioctl.h>
#include <asm/unistd.h>
#include <linux/perf_event.h>
#endif
在sys/ioctl.h中最终会include asm/termbits .h文件
#define B0 0000000
所以在用B0作为变量或参数的地方,一般都会报错。
上一篇下一篇

猜你喜欢

热点阅读