初识IJKPlayer播放器
程序员文章站
2022-07-01 20:31:34
...
ijkplayer的编译
现在可以直接使用gradle引入ijkplayer了。但是,如果需要对更多格式进行支持,还是需要自己编译一次。
allprojects {
repositories {
jcenter()
}
}
compile 'tv.danmaku.ijk.media:ijkplayer-java:0.8.4'
compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.4'
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'
compile 'tv.danmaku.ijk.media:ijkplayer-exo:0.8.4'
如果出现minSdkVersion不匹配,删除上面注释的依赖,或者换手机运行。
自定义播放控件
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);
}
}
}
设置监听器,实现多个监听
public abstract class VideoPlayerListener implements IMediaPlayer.OnBufferingUpdateListener, IMediaPlayer.OnCompletionListener, IMediaPlayer.OnPreparedListener, IMediaPlayer.OnInfoListener, IMediaPlayer.OnVideoSizeChangedListener, IMediaPlayer.OnErrorListener, IMediaPlayer.OnSeekCompleteListener {
}
使用
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ijkpalyerdemo.MainActivity">
<com.ijkpalyerdemo.VideoPlayerIJK
android:id="@+id/ijkPlayer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.ijkpalyerdemo.VideoPlayerIJK>
</RelativeLayout>
activity:
public class MainActivity extends AppCompatActivity {
private String url = "http://9890.vod.myqcloud.com/9890_9c1fa3e2aea011e59fc841df10c92278.f20.mp4";
private VideoPlayerIJK ijkPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.ijkPlayer = (VideoPlayerIJK) findViewById(R.id.ijkPlayer);
IjkMediaPlayer.loadLibrariesOnce(null);
IjkMediaPlayer.native_profileBegin("libijkplayer.so");
ijkPlayer.setVideoPath(url);
ijkPlayer.start();
}
@Override
protected void onStop() {
super.onStop();
IjkMediaPlayer.native_profileEnd();
}
}
这样就完成了一个简单的视频播放器了。
上一篇: CSS:画扇形