Skip to content

Commit

Permalink
fix list bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xieguangwei committed May 3, 2018
1 parent 88f13b4 commit 4d01d14
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
12 changes: 6 additions & 6 deletions app/src/main/java/com/xgw/mycustommediaplayer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,27 @@ public void onCacheProgress(String url, int percentAvailable) {
private static List<VideoBean> getVideoListData() {
List<VideoBean> videoList = new ArrayList<>();
videoList.add(new VideoBean(
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-33-30.mp4",
"http://p79di6ot0.bkt.clouddn.com/upload/1/video/jghdpdfc_598xvnbv6zjn5ae29473a8eed.mp4",
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-30-43.jpg",
"办公室小野开番外了,居然在办公室开澡堂!老板还点赞?",true));
videoList.add(new VideoBean(
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-10_10-20-26.mp4",
"http://p79di6ot0.bkt.clouddn.com/upload/1/video/jghdpdfc_598xvnbv6zjn5ae29473a8eed.mp4",
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-10_10-09-58.jpg",
"小野在办公室用丝袜做茶叶蛋 边上班边看《外科风云》",false));
videoList.add(new VideoBean(
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-03_13-02-41.mp4",
"http://p79di6ot0.bkt.clouddn.com/upload/1/video/jghdpdfc_598xvnbv6zjn5ae29473a8eed.mp4",
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-03_12-52-08.jpg",
"花盆叫花鸡,怀念玩泥巴,过家家,捡根竹竿当打狗棒的小时候",false));
videoList.add(new VideoBean(
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/04/2017-04-28_18-20-56.mp4",
"http://p79di6ot0.bkt.clouddn.com/upload/1/video/jghdpdfc_598xvnbv6zjn5ae29473a8eed.mp4",
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/04/2017-04-28_18-18-22.jpg",
"针织方便面,这可能是史上最不方便的方便面",false));
videoList.add(new VideoBean(
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/04/2017-04-26_10-06-25.mp4",
"http://p79di6ot0.bkt.clouddn.com/upload/1/video/jghdpdfc_598xvnbv6zjn5ae29473a8eed.mp4",
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/04/2017-04-26_10-00-28.jpg",
"小野的下午茶,办公室不只有KPI,也有诗和远方",false));
videoList.add(new VideoBean(
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/04/2017-04-21_16-41-07.mp4",
"http://p79di6ot0.bkt.clouddn.com/upload/1/video/jghdpdfc_598xvnbv6zjn5ae29473a8eed.mp4",
"http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/04/2017-04-21_16-37-16.jpg",
"可乐爆米花,嘭嘭嘭......收花的人说要把我娶回家",false));
return videoList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ private void setParams(RelativeLayout parentRl, RelativeLayout childRl) {
childRl.setLayoutParams(childParams);
}

public void setPlay(final String url, final View view) {
if (TextUtils.isEmpty(url)) {
public void setPlay(final int position, final View view) {
if (position > getData().size() - 1 || position < 0 || view == null) {
return;
}
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < getData().size(); i++) {
getData().get(i).setPlaying(url.equals(getData().get(i).getUrl()));
getData().get(i).setPlaying(i == position);
}

view.post(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class MyMediaPlayer extends RelativeLayout implements View.OnClickListene
private static final int UPDATE_TIME_AND_PROGRESS = 1;//更新时间和进度的消息

private String playUrl;//当前播放url
private int currentPosition = -1;//当前播放视频索引


private MediaPlayer mPlayer;
Expand Down Expand Up @@ -173,7 +174,7 @@ public void onClick(View v) {
playOrPause();
} else if (id == reloadRl.getId()) {
//播放出错,重新加载
playSpecifiedVideo(playUrl, coverUrl);
playSpecifiedVideo(currentPosition, playUrl, coverUrl);
} else if (id == fullScreenIv.getId()) {
//全屏/退出全屏
if (listener != null) {
Expand Down Expand Up @@ -310,9 +311,8 @@ public void onItemChildClick(BaseQuickAdapter adapter, View view, int position)
}
String url = videoBean.getUrl();
String coverUrl = videoBean.getCoverUrl();
videoListAdapter.setPlay(url, MyMediaPlayer.this);
currentProgress = 0;
playSpecifiedVideo(url, coverUrl);
playSpecifiedVideo(position, url, coverUrl);
}
});

Expand Down Expand Up @@ -361,19 +361,20 @@ public void setVideoList(List<VideoBean> videoList) {
for (int i = 0; i < videoList.size(); i++) {
VideoBean item = videoList.get(i);
if (item.isPlaying()) {
setPlayUrl(item.getUrl(), item.getCoverUrl());
setPlayUrl(i, item.getUrl(), item.getCoverUrl());
break;
}
}
if (TextUtils.isEmpty(playUrl)) {
setPlayUrl(videoList.get(0).getUrl(), videoList.get(0).getCoverUrl());
videoListAdapter.setPlay(playUrl, this);
setPlayUrl(0, videoList.get(0).getUrl(), videoList.get(0).getCoverUrl());
videoListAdapter.setPlay(0, this);
}
}
loadCoverUrl();
}

private void setPlayUrl(String playUrl, String coverUrl) {
private void setPlayUrl(int currentPosition, String playUrl, String coverUrl) {
this.currentPosition = currentPosition;
this.playUrl = playUrl;
this.coverUrl = TextUtils.isEmpty(coverUrl) ? playUrl : coverUrl;
}
Expand All @@ -386,7 +387,7 @@ public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int
mSurface = new Surface(surfaceTexture);//连接MediaPlayer和TextureView两个对象
isTextureAvailable = true;
//设置第一次打开时的播放器
playSpecifiedVideo(playUrl, coverUrl);
playSpecifiedVideo(currentPosition, playUrl, coverUrl);
}

@Override
Expand Down Expand Up @@ -426,29 +427,29 @@ private void loadCoverUrl() {
VideoCoverUtils.load(getContext(), coverIv, coverUrl);
}

/**
* 给一个初始化的播放url
*
* @param url
*/
public void initPlayUrl(String url, String coverUrl) {
setPlayUrl(url, coverUrl);
//没设置正在播放的视频,默认播放第一个视频
if (TextUtils.isEmpty(playUrl)) {
onErrorHandle(new Exception("initPlayUrl时视频地址为空!!!"));
return;
}

try {
//设置uri、封面
loadCoverUrl();
setControlVisibility(GONE, GONE, GONE);
} catch (Exception e) {
KLog.e(e.getMessage());
e.printStackTrace();
onErrorHandle(e);
}
}
// /**
// * 给一个初始化的播放url
// *
// * @param url
// */
// public void initPlayUrl(String url, String coverUrl) {
// setPlayUrl(url, coverUrl);
// //没设置正在播放的视频,默认播放第一个视频
// if (TextUtils.isEmpty(playUrl)) {
// onErrorHandle(new Exception("initPlayUrl时视频地址为空!!!"));
// return;
// }
//
// try {
// //设置uri、封面
// loadCoverUrl();
// setControlVisibility(GONE, GONE, GONE);
// } catch (Exception e) {
// KLog.e(e.getMessage());
// e.printStackTrace();
// onErrorHandle(e);
// }
// }

/**
* 播放出错时的处理
Expand All @@ -470,8 +471,9 @@ private void onErrorHandle(Exception e) {
*
* @param url
*/
public void playSpecifiedVideo(String url, String coverUrl) {
setPlayUrl(url, coverUrl);
public void playSpecifiedVideo(int currentPosition, String url, String coverUrl) {
setPlayUrl(currentPosition, url, coverUrl);
videoListAdapter.setPlay(currentPosition, MyMediaPlayer.this);
//没设置正在播放的视频,默认播放第一个视频
if (TextUtils.isEmpty(playUrl)) {
onErrorHandle(new Exception("playSpecifiedVideo时视频地址为空!!!"));
Expand Down Expand Up @@ -512,7 +514,6 @@ private void onPreparedHandle() {
KLog.e("onPrepared");
//进行视频的播放
MyMediaPlayManager.play();
videoListAdapter.setPlay(playUrl, MyMediaPlayer.this);
mHandler.removeMessages(UPDATE_TIME_AND_PROGRESS);
mHandler.sendEmptyMessage(UPDATE_TIME_AND_PROGRESS);
btnPlay.setBackgroundResource(R.drawable.play_pause);
Expand Down Expand Up @@ -576,15 +577,15 @@ private void onCompleteHandle() {
int playMode = PlayModeUtils.getPlayMode();
//单曲循环模式---一直播放该视频
if (PlayModeUtils.getPlayMode() == PlayMode.PLAY_MODE_SINGLE_CYCLE) {
playSpecifiedVideo(playUrl, coverUrl);
playSpecifiedVideo(currentPosition, playUrl, coverUrl);
} else if (playMode == PlayMode.PLAY_MODE_LIST_CYCLE) {//列表循环模式---播放完最后一个视频播放第一个视频
for (int i = 0; i < videoListAdapter.getData().size(); i++) {
VideoBean videoBean = videoListAdapter.getData().get(i);
if (videoBean != null && videoBean.isPlaying()) {
if (i < videoListAdapter.getData().size() - 1) {
playSpecifiedVideo(videoListAdapter.getData().get(i + 1).getUrl(), videoListAdapter.getData().get(i + 1).getCoverUrl());
playSpecifiedVideo(i + 1, videoListAdapter.getData().get(i + 1).getUrl(), videoListAdapter.getData().get(i + 1).getCoverUrl());
} else {
playSpecifiedVideo(videoListAdapter.getData().get(0).getUrl(), videoListAdapter.getData().get(0).getCoverUrl());
playSpecifiedVideo(0, videoListAdapter.getData().get(0).getUrl(), videoListAdapter.getData().get(0).getCoverUrl());
}
break;
}
Expand All @@ -594,7 +595,7 @@ private void onCompleteHandle() {
VideoBean videoBean = videoListAdapter.getData().get(i);
if (videoBean != null && videoBean.isPlaying()) {
if (i < videoListAdapter.getData().size() - 1) {
playSpecifiedVideo(videoListAdapter.getData().get(i + 1).getUrl(), videoListAdapter.getData().get(i + 1).getCoverUrl());
playSpecifiedVideo(i + 1, videoListAdapter.getData().get(i + 1).getUrl(), videoListAdapter.getData().get(i + 1).getCoverUrl());
} else {
coverIv.setVisibility(VISIBLE);
btnPlay.setVisibility(VISIBLE);
Expand Down Expand Up @@ -716,7 +717,7 @@ public void playOrPause() {
btnPlay.setBackgroundResource(R.drawable.play_pause);
//开始播放(新的)
//显示TextureView,回调onSurfaceTextureAvailable方法,开始执行新的播放操作
playSpecifiedVideo(playUrl, coverUrl);
playSpecifiedVideo(currentPosition, playUrl, coverUrl);
}
setControlVisibility(GONE, GONE, GONE);
}
Expand Down Expand Up @@ -873,7 +874,7 @@ public void onResume() {
setPlayModelImage();
if (!isFirstEnter && isTextureAvailable) {
btnPlay.setBackgroundResource(R.drawable.play_pause);
playSpecifiedVideo(playUrl, coverUrl);
playSpecifiedVideo(currentPosition, playUrl, coverUrl);
}
isFirstEnter = false;
}
Expand Down

0 comments on commit 4d01d14

Please sign in to comment.