本文章为博主在借鉴了一名前辈的文章后为了自己以后能够快速的回忆所写小计如有看不懂的地方还请谅解,毕竟只是为了自己更好的回忆 如有何不懂或者异常出现请浏览原文章,原文章中对于异常都有一 一解答。
在此标注前辈文章地址 :https://github.com/jjdxmashl/jjdxm_ijkplayer
依赖本项目类库
该项目是基于ijkplayer项目进行的视频UI的二次封装,需导入依赖
compile 'com.dou361.ijkplayer:jjdxm-ijkplayer:1.0.6'
多种分辨率流切换的案例,例如播放器的标清、高清、超清、720P等。
1.简单的播放器实现
rootView = getLayoutInflater().from(this).inflate(R.layout.simple_player_view_player, null);
setContentView(rootView);
String url = "http://9890.vod.myqcloud.com/9890_9c1fa3e2aea011e59fc841df10c92278.f20.mp4";
player = new PlayerView(this,rootView)
.setTitle("什么")
.setScaleType(PlayStateParams.fitparent)
.hideMenu(true)
.forbidTouch(false)
.showThumbnail(new OnShowThumbnailListener() {
@Override
public void onShowThumbnail(ImageView ivThumbnail) {
Glide.with(mContext)
.load("http://pic2.nipic.com/20090413/406638_125424003_2.jpg")
.placeholder(R.color.cl_default)
.error(R.color.cl_error)
.into(ivThumbnail);
}
})
.setPlaySource(url)
.startPlay();
2.多种不同的分辨率流的播放器实现
在布局中使用simple_player_view_player.xml布局
<include
layout="@layout/simple_player_view_player"
android:layout_width="match_parent"
android:layout_height="180dp"/>
代码中创建一个播放器对象
/**播放资源*/
ist<VideoijkBean> list = new ArrayList<VideoijkBean>();
String url1 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4";
String url2 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f30.mp4";
VideoijkBean m1 = new VideoijkBean();
m1.setStream("标清");
m1.setUrl(url1);
VideoijkBean m2 = new VideoijkBean();
m2.setStream("高清");
m2.setUrl(url2);
list.add(m1);
list.add(m2);
/**播放器*/
rootView = getLayoutInflater().from(this).inflate(你的布局, null);
setContentView(rootView);
player = new PlayerView(this,rootView)
.setTitle("什么")
.setScaleType(PlayStateParams.fitparent)
.hideMenu(true)
.forbidTouch(false)
.showThumbnail(new OnShowThumbnailListener() {
@Override
public void onShowThumbnail(ImageView ivThumbnail) {
/**加载前显示的缩略图*/
Glide.with(mContext)
.load("http://pic2.nipic.com/20090413/406638_125424003_2.jpg")
.placeholder(R.color.cl_default)
.error(R.color.cl_error)
.into(ivThumbnail);
}
})
.setPlaySource(list)
.startPlay();
配置生命周期方法,为了让播放器同步Activity生命周期,建议以下方法都去配置,注释的代码,主要作用是播放时屏幕常亮和暂停其它媒体的播放。
@Override
protected void onPause() {
super.onPause();
if (player != null) {
player.onPause();
}
/**demo的内容,恢复系统其它媒体的状态*/
//MediaUtils.muteAudioFocus(mContext, true);
}
@Override
protected void onResume() {
super.onResume();
if (player != null) {
player.onResume();
}
/**demo的内容,暂停系统其它媒体的状态*/
MediaUtils.muteAudioFocus(mContext, false);
/**demo的内容,激活设备常亮状态*/
//if (wakeLock != null) {
// wakeLock.acquire();
//}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (player != null) {
player.onDestroy();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (player != null) {
player.onConfigurationChanged(newConfig);
}
}
@Override
public void onBackPressed() {
if (player != null && player.onBackPressed()) {
return;
}
super.onBackPressed();
/**demo的内容,恢复设备亮度状态*/
//if (wakeLock != null) {
// wakeLock.release();
//}
}