IjkPlayer的基本使用

1.作为一个小白,花费千辛万苦终于知道Ijk如何使用,这里分享给跟我一样小白的人.我们直接进入正题(这里导入的是官方编译的依赖).

compile'tv.danmaku.ijk.media:ijkplayer-java:0.7.5'
compile'tv.danmaku.ijk.media:ijkplayer-armv7a:0.7.5'
compile'tv.danmaku.ijk.media:ijkplayer-armv5:0.7.5'
compile'tv.danmaku.ijk.media:ijkplayer-arm64:0.7.5'
compile'tv.danmaku.ijk.media:ijkplayer-x86:0.7.5'
compile'tv.danmaku.ijk.media:ijkplayer-x86_64:0.7.5'
compile'tv.danmaku.ijk.media:ijkplayer-exo:0.7.5'

2.初始化播放器(并传入播放地址和头部信息等)

private AbstractMediaPlayer mediaPlayer;
public final static int IJKPLAYER=0;//IJK
public final static int EXOPLAYER=1;//谷歌EXO
private int videoType=IJKPLAYER;//Player类型,默认IJK
private voidiniPLayer(Message msg) {
try{
currentVideoWidth=0;
currentVideoHeight=0;
mediaPlayer.release();
if(videoType==IJKPLAYER) {
initIJKPlayer(msg);
}else if(videoType==EXOPLAYER) {
initEXOPlayer(msg);
}catch(Exception e) {
e.printStackTrace();
}
private voidinitIJKPlayer(Message msg) {
mediaPlayer=newIjkMediaPlayer();
//音频初始化
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try{
if(USE_MEDIA_CODEC) {
//硬解码
((IjkMediaPlayer)mediaPlayer).setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER,"mediacodec",1);
((IjkMediaPlayer)mediaPlayer).setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER,"mediacodec-auto-rotate",1);
((IjkMediaPlayer)mediaPlayer).setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER,"mediacodec-handle-resolution-change",1);
}
//设置播放源
((IjkMediaPlayer)mediaPlayer).setDataSource(((BreezeeModel) msg.obj).getUrl(),((BreezeeModel) msg.obj).getMapHeadData());
//设置循环播放
mediaPlayer.setLooping(((BreezeeModel) msg.obj).isLooping());
//设置播放速度(速度不为1时)
if(((BreezeeModel) msg.obj).getSpeed() !=1&& ((BreezeeModel) msg.obj).getSpeed() >0) {
((IjkMediaPlayer)mediaPlayer).setSpeed(((BreezeeModel) msg.obj).getSpeed());
}
}catch(IOException e) {
e.printStackTrace();
}
}
private voidinitEXOPlayer(Message msg) {
mediaPlayer=newIjkExoMediaPlayer(context);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try{
mediaPlayer.setDataSource(context,Uri.parse(((BreezeeModel) msg.obj).getUrl()),((BreezeeModel) msg.obj).getMapHeadData());
}catch(IOException e) {
e.printStackTrace();
}
}

3.初始化显示用控件,这里我没有用SurfaceView,用的TextureView(具体不同可以去自行搜索一下)

private TextureView textureView;
textureView=newBreezeeTextureView(getContext());
textureView.setSurfaceTextureListener(this);

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, intwidth, intheight) {
this.surface=new Surface(surface);
BreezeeVideoManager.instance().setDisplay(this.surface);//在监听中把得到的Surface丢给刚才初始化的播放器(下面把这个方法贴出来)
BreezeeVideoManager.instance().setListener(this);
mediaListener.bringViewsToFront();
}

@Override
public voidonSurfaceTextureSizeChanged(SurfaceTexture surface, intwidth, intheight) {
}

@Override
public booleanonSurfaceTextureDestroyed(SurfaceTexture surface) {
BreezeeVideoManager.instance().setDisplay(null);
surface.release();
return true;
}

@Override
public voidonSurfaceTextureUpdated(SurfaceTexture surface) {
}

4.show Display方法,传入显示用控件

Message msg =new Message();
msg.what=HANDLER_SETDISPLAY;
msg.obj= holder;
showDisplay(msg);

private void showDisplay(Message msg) {
if(msg.obj==null&&mediaPlayer!=null) {
mediaPlayer.setSurface(null);
}else{
Surface holder = (Surface) msg.obj;
if(mediaPlayer!=null&& holder.isValid()) {
//Player设置显示用的Surface
mediaPlayer.setSurface(holder);
}
if(mediaPlayerinstanceofIjkExoMediaPlayer) {

//总时长>30
if(mediaPlayer!=null&&mediaPlayer.getDuration() >30
//当前播放位置<总时长
&&mediaPlayer.getCurrentPosition()
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() -20);
}
}
}
}

IjkPlayer基本播放功能的使用就是酱更多别的功能可以到的github下载
github.com/WML245138231/BreezeePlayer
如果觉得功能封装比较鸡肋或者太简单可以参考老司机车站站长:猫小郭的github
github.com/CarGuo/GSYVideoPlayer

你可能感兴趣的:(IjkPlayer的基本使用)