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

性能优化

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