用于视频播放的第三方插件,效果如图:
device-2018-01-30-142705.png使用方法:
1.build.gradle里添加依赖
compile 'fm.jiecao:jiecaovideoplayer:4.6.3'
2.application
/**
* @author : wangtt
* @time : 16/9/5
*/
public class VPApplication extends Application {
public static VPApplication instance;
public JCVideoPlayerStandard VideoPlaying;
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
}
3.在MainActivity里,如果项目没有actionbar和toolbar的话要设置:
JCVideoPlayer.ACTION_BAR_EXIST = false;
JCVideoPlayer.TOOL_BAR_EXIST = false;
否则会出异常。
@Override
protected void onPause() {
super.onPause();
if(VPApplication.instance.VideoPlaying!=null)
{
if(VPApplication.instance.VideoPlaying.currentState== JCVideoPlayer.CURRENT_STATE_PLAYING)
{
VPApplication.instance.VideoPlaying.startButton.performClick();
}else if (VPApplication.instance.VideoPlaying.currentState== JCVideoPlayer.CURRENT_STATE_PREPAREING)
{
JCVideoPlayer.releaseAllVideos();
}
}
}
4.适配器item
<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
android:id="@+id/videoplayer"
android:layout_width="match_parent"
android:layout_height="260dp"/>
5.视频列表,如果有可见时播放视频的需求,实现方式如下:
videoList.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
Log.e("videoTest", "SCROLL_STATE_FLING");
break;
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
Log.e("videoTest", "SCROLL_STATE_IDLE");
autoPlayVideo(view);//可见时播放视频
break;
case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
Log.e("videoTest", "SCROLL_STATE_TOUCH_SCROLL");
break;
default:
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// firstVisibleItem 当前第一个可见的item
// visibleItemCount 当前可见的item个数
if (firstVisible == firstVisibleItem) {
return;
}
firstVisible = firstVisibleItem;
visibleCount = visibleItemCount;
totalCount = totalItemCount;
}
});
}
void autoPlayVideo(AbsListView view) {
for (int i = 0; i < visibleCount; i++) {
if (view != null && view.getChildAt(i) != null && view.getChildAt(i).findViewById(R.id.videoplayer) != null) {
JCVideoPlayerStandard videoPlayerStandard1 = (JCVideoPlayerStandard) view.getChildAt(i).findViewById(R.id.videoplayer);
Rect rect = new Rect();
videoPlayerStandard1.getLocalVisibleRect(rect);
int videoheight3 = videoPlayerStandard1.getHeight();
if (rect.top == 0 && rect.bottom == videoheight3) {
if (videoPlayerStandard1.currentState == JCVideoPlayer.CURRENT_STATE_NORMAL || videoPlayerStandard1.currentState == JCVideoPlayer.CURRENT_STATE_ERROR) {
videoPlayerStandard1.startButton.performClick();
VPApplication.instance.VideoPlaying = videoPlayerStandard1;
}
return;
}
}
}
JCVideoPlayer.releaseAllVideos();
VPApplication.instance.VideoPlaying = null;
}
6.适配器getView方法中
//列表滑动时,及时释放资源
if (convertView != null && convertView.getTag() != null && convertView.getTag() instanceof VideoHolder) {
((VideoHolder) convertView.getTag()).jcVideoPlayer.release();
}
boolean setUp = viewHolder.jcVideoPlayer.setUp(
"http://gslb.miaopai.com/stream/ed5HCfnhovu3tyIQAiv60Q__.mp4", JCVideoPlayer.SCREEN_LAYOUT_LIST,
"");
if (setUp) {
//设置视频缩略图
Glide.with(getActivity()).load("http://a4.att.hudong.com/05/71/01300000057455120185716259013.jpg").into(viewHolder.jcVideoPlayer.thumbImageView);
}
7.播放视频
videoPlayer.startPlayLocic();
8.退出Activity时,要销毁player,否则会出现退出了还继续播放的情况
@Override
public void onDestroy() {
super.onDestroy();
try {
JCVideoPlayer.releaseAllVideos();
} catch (Exception e) {
}
}
8.遇到的问题
播放视频时,出现seekbar的thumb圆点被覆盖情况或者使用其他地方使用seekbar时thumb圆点被覆盖情况,如图:
WX20180130-154430.png
这个问题出现的原因是因为manifest.xml里application节点使用了自定义的theme。
解决办法:在使用了的seekbar的类节点里,加上theme如下:
AndroidManifest.xml
<activity
android:name="com.nongji.ah.activity.VideoPlayAct"
android:screenOrientation="portrait"
android:theme="@style/AppTheme"></activity>
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
网友评论