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());
//設置循環(huán)播放
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);//在監(jiān)聽中把得到的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