Commit b395cc46 authored by 委座-江's avatar 委座-江

性能优化

parent 0ef30b50
......@@ -84,18 +84,6 @@ public class PlayerManager {
return mInstance;
}
public void release() {
if (exoPlayer != null) {
exoPlayer.setPlayWhenReady(false);
exoPlayer.stop(true);
exoPlayer.release();
exoPlayer = null;
}
if (playerView != null) {
playerView.setPlayer(null);
playerView = null;
}
}
/**
* 切换与播放器exoplayer 绑定的exoplayerView。用于页面切换视频无缝续播的场景
* @param newPlayerView
......@@ -106,6 +94,7 @@ public class PlayerManager {
newPlayerView.setPlayer(attach ? exoPlayer : null);
}
//todo 可以缓存起来
public MediaSource createMediaSource(String url) {
return mediaSourceFactory.createMediaSource(Uri.parse(url));
}
......
......@@ -13,6 +13,7 @@ import android.widget.FrameLayout;
import com.widget.imagevideobanner.R;
import com.widget.imagevideobanner.bean.MediaBean;
import com.widget.imagevideobanner.player.IPlayTarget;
import java.util.ArrayList;
import java.util.List;
......@@ -71,22 +72,36 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCha
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
IPlayTarget iPlayTarget = (IPlayTarget)mList.get(curIndex);
iPlayTarget.onActive();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
IPlayTarget iPlayTarget = (IPlayTarget)mList.get(curIndex);
iPlayTarget.inActive();
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageSelected(int position) {
curIndex = position;
IPlayTarget iPlayTarget = (IPlayTarget)mList.get(curIndex);
iPlayTarget.onActive();
}
@Override
public void next() {
Log.e(TAG,curIndex+"");
if (curIndex == mList.size() - 1) {
curIndex = 0;
}else{
curIndex = curIndex+1;
if (curIndex >= mList.size()) {
curIndex = 0;
}
Log.e(TAG,"curIndex = " + curIndex);
mViewPager.setCurrentItem(curIndex);
}
......@@ -121,6 +136,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCha
public void destroyItem(ViewGroup container, int position, @NonNull Object object) {
container.removeView(mList.get(position));
}
}
}
......@@ -88,7 +88,7 @@ public class ViewPager extends ViewGroup {
private static final boolean USE_CACHE = false;
private static final int DEFAULT_OFFSCREEN_PAGES = 0;//默认是1,这里直接改为0
private static final int DEFAULT_OFFSCREEN_PAGES = 5;//默认是1,这里直接改为0
private static final int MAX_SETTLE_DURATION = 600; // ms
private static final int MIN_DISTANCE_FOR_FLING = 25; // dips
......
......@@ -3,11 +3,9 @@ package com.widget.imagevideobanner.view;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.Log;
import android.view.View;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
......@@ -27,14 +25,16 @@ public class VpImageView extends AppCompatImageView implements IPlayTarget {
private static final String TAG = "VpImageView";
private static final int DEFAULT_SWITCH_PAGE_TIME = 5000;
private MediaBean mediaBean;
private OnActionFinishListener onPageChangedListener;
private Handler handler = new Handler();
private Runnable pageChangeRunable = new Runnable() {
@Override
public void run() {
if (null != onPageChangedListener) onPageChangedListener.next();
inActive();
if (null != onPageChangedListener){
onPageChangedListener.next();
}
}
};
......@@ -44,8 +44,8 @@ public class VpImageView extends AppCompatImageView implements IPlayTarget {
public VpImageView(Context context, final MediaBean mediaBean, final OnActionFinishListener onPageChangedListener) {
super(context);
this.mediaBean = mediaBean;
this.onPageChangedListener = onPageChangedListener;
setImageUrl(mediaBean.getUrl());
}
private void setImageUrl(String url) {
......@@ -69,19 +69,9 @@ public class VpImageView extends AppCompatImageView implements IPlayTarget {
}).into(this);
}
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
Log.e(TAG,"onVisibilityChanged");
if(visibility == VISIBLE) onActive();
if(visibility == GONE || visibility == INVISIBLE) inActive();
}
@Override
public void onActive() {
handler.removeCallbacks(pageChangeRunable);
setImageUrl(mediaBean.getUrl());
handler.postDelayed(pageChangeRunable, DEFAULT_SWITCH_PAGE_TIME);
}
......@@ -91,4 +81,6 @@ public class VpImageView extends AppCompatImageView implements IPlayTarget {
}
}
......@@ -88,7 +88,6 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Log.e(TAG, "onResourceReady");
return false;
}
}).into(blur);
......@@ -109,7 +108,6 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Log.e(TAG, "onResourceReady");
return false;
}
}).into(cover);
......@@ -119,6 +117,11 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.e(TAG, "onSizeChanged");
reSize();
}
private void reSize(){
//动态设置
int mHeight = getHeight();
int mWidth = getWidth();
......@@ -147,8 +150,6 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
coverParams.height = mHeightPx;
coverParams.gravity = Gravity.CENTER;
cover.setLayoutParams(coverParams);
//todo onVisibilityChange 和 onSizeChanged哪个先执行
Log.e(TAG, "onSizeChanged"+mWidthPx+" "+mHeightPx);
}
......@@ -179,6 +180,7 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
bufferView.setVisibility(VISIBLE);
}
//执行2次 bugfxied
if(playbackState == Player.STATE_ENDED){
if(!isVideoEnded){
isVideoEnded = true;
......@@ -188,8 +190,9 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
}
private void playNextVideo(){
if(null != mOnPageChangedListener){
Log.e(TAG, "playNextVideo");
inActive();
if(null != mOnPageChangedListener){
mOnPageChangedListener.next();
}
}
......@@ -227,18 +230,15 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
@Override
public void onVisibilityChange(int visibility) {
Log.e(TAG, "onVisibilityChange");
if(visibility == ViewGroup.VISIBLE){
onActive();
}else{
inActive();
}
}
@Override
public void onActive() {
isVideoEnded = false;
bufferView.setVisibility(GONE);
cover.setVisibility(VISIBLE);
//todo 这个地方能否得到具体尺寸
//视频播放,或恢复播放
//通过该View所在页面的mCategory(比如首页列表tab_all,沙发tab的tab_video,标签帖子聚合的tag_feed) 字段,
......@@ -276,21 +276,9 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
exoPlayer.setPlayWhenReady(true);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
onActive();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
bufferView.setVisibility(GONE);
cover.setVisibility(VISIBLE);
}
@Override
public void inActive() {
Log.e(TAG, "inActive");
PlayerManager playerManager = PlayerManager.getInstance();
playerManager.exoPlayer.setPlayWhenReady(false);
playerManager.exoPlayer.removeListener(this);
......@@ -303,6 +291,4 @@ public class VpVideoView extends FrameLayout implements IPlayTarget, PlayerContr
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment