Skip to content

Commit

Permalink
添加封面设置,coverURL,demo播放地址修改
Browse files Browse the repository at this point in the history
  • Loading branch information
xieguangwei committed Apr 16, 2018
1 parent 709e5ea commit c8abfd0
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ allprojects {
### Step 2. Add the dependency
```
dependencies {
compile 'com.github.xieguangwei:MyCustomMediaPlayer:1.3'
compile 'com.github.xieguangwei:MyCustomMediaPlayer:1.4'
}
```
### 感兴趣的可以下载demo看下,[同款的基于ExoPlayer封装的播放器>>>](https://github.com/xieguangwei/MyCustomExoPlayer)
41 changes: 31 additions & 10 deletions app/src/main/java/com/xgw/mycustommediaplayer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,7 @@ protected int getLayoutId() {

@Override
protected void initView() {
List<VideoBean> videoBeen = new ArrayList<>();
for (int i = 0; i < 10; i++) {
VideoBean videoBean = new VideoBean();
videoBean.setName("视频" + i);
videoBean.setUrl("http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super");
videoBean.setPlaying(i == 0);
videoBeen.add(videoBean);
}
mediaPlayer.setVideoList(videoBeen);
mediaPlayer.setVideoList(getVideoListData());
mediaPlayer.setVideoPlayListener(new SimpleVideoPlayListener() {
@Override
public void onCompletion() {
Expand Down Expand Up @@ -77,14 +69,43 @@ public void onExitFullScreen() {
super.onExitFullScreen();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mediaPlayer.getLayoutParams();
params.width = LinearLayout.LayoutParams.MATCH_PARENT;
params.height = SizeUtils.dp2px(300);
params.height = SizeUtils.dp2px(220);
mediaPlayer.setLayoutParams(params);
mediaPlayer.setScreenParams(ScreenStatus.SCREEN_STATUS_NORMAL);
}
});
mediaPlayer.setController(this, new MyVideoControlManager());
}

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://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://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://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://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://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://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/04/2017-04-21_16-37-16.jpg",
"可乐爆米花,嘭嘭嘭......收花的人说要把我娶回家",false));
return videoList;
}

@Override
protected void initData() {

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<com.xgw.custommediaplayer.view.MyMediaPlayer
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="300dp" />
android:layout_height="220dp" />

<ImageView
android:id="@+id/extra_iv"
Expand Down
2 changes: 1 addition & 1 deletion custommediaplayer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.3"
versionName "1.4"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ protected void convert(BaseViewHolder helper, VideoBean item) {
.setText(R.id.bottom_name_tv, item.getName()).addOnClickListener(R.id.item_video_iv);
RelativeLayout parentRootRl = helper.getView(R.id.parent_root_rl);
RelativeLayout childRootRl = helper.getView(R.id.child_root_rl);
setParams(parentRootRl,childRootRl);
setParams(parentRootRl, childRootRl);

VideoCoverUtils.load(mContext,(ImageView) helper.getView(R.id.item_video_iv),item.getUrl());
VideoCoverUtils.load(mContext, (ImageView) helper.getView(R.id.item_video_iv), TextUtils.isEmpty(item.getCoverUrl()) ? item.getUrl() : item.getCoverUrl());
}

private void setParams(RelativeLayout parentRl,RelativeLayout childRl) {
int parentWidth,parentHeight,childWidth,childHeight;
private void setParams(RelativeLayout parentRl, RelativeLayout childRl) {
int parentWidth, parentHeight, childWidth, childHeight;
if (MyMediaPlayManager.isIsFullScreen()) {
childWidth = 150;
childHeight = 110;
Expand Down Expand Up @@ -76,13 +76,14 @@ public void setPlay(final String url, final View view) {
public void run() {
for (int i = 0; i < getData().size(); i++) {
getData().get(i).setPlaying(url.equals(getData().get(i).getUrl()));
view.post(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
}

view.post(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
}
}).start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public class VideoBean implements Serializable{
private String name;
private boolean isPlaying;

public VideoBean(String url, String coverUrl, String name, boolean isPlaying) {
this.url = url;
this.coverUrl = coverUrl;
this.name = name;
this.isPlaying = isPlaying;
}

public String getUrl() {
return url;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.xgw.custommediaplayer.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

public class MarqueeText extends TextView {
public MarqueeText(Context con) {
super(con);
}

public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean isFocused() {
return true;
}

@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public class MyMediaPlayer extends RelativeLayout implements View.OnClickListene
private boolean isTextureAvailable = false;
private boolean isFirstEnter = true;

private String coverUrl;//封面url

/**
* handler每隔500ms刷新一次当前播放进度
*/
Expand Down Expand Up @@ -167,7 +169,7 @@ public void onClick(View v) {
playOrPause();
} else if (id == reloadRl.getId()) {
//播放出错,重新加载
playSpecifiedVideo(playUrl);
playSpecifiedVideo(playUrl,coverUrl);
} else if (id == fullScreenIv.getId()) {
//全屏/退出全屏
if (listener != null) {
Expand Down Expand Up @@ -293,9 +295,10 @@ public void onItemChildClick(BaseQuickAdapter adapter, View view, int position)
return;
}
String url = videoBean.getUrl();
String coverUrl = videoBean.getCoverUrl();
videoListAdapter.setPlay(url, MyMediaPlayer.this);
currentProgress = 0;
playSpecifiedVideo(url);
playSpecifiedVideo(url,coverUrl);
}
});

Expand Down Expand Up @@ -344,20 +347,21 @@ 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());
setPlayUrl(item.getUrl(),item.getCoverUrl());
break;
}
}
if (TextUtils.isEmpty(playUrl)) {
setPlayUrl(videoList.get(0).getUrl());
setPlayUrl(videoList.get(0).getUrl(),videoList.get(0).getCoverUrl());
videoListAdapter.setPlay(playUrl, this);
}
}
VideoCoverUtils.load(getContext(), coverIv, playUrl);
loadCoverUrl();
}

private void setPlayUrl(String playUrl) {
private void setPlayUrl(String playUrl,String coverUrl) {
this.playUrl = playUrl;
this.coverUrl = TextUtils.isEmpty(coverUrl) ? playUrl : coverUrl;
}

//TextureView创建监听
Expand All @@ -368,7 +372,7 @@ public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int
mSurface = new Surface(surfaceTexture);//连接MediaPlayer和TextureView两个对象
isTextureAvailable = true;
//设置第一次打开时的播放器
playSpecifiedVideo(playUrl);
playSpecifiedVideo(playUrl,coverUrl);
}

@Override
Expand Down Expand Up @@ -401,13 +405,20 @@ public void setVideoPlayListener(VideoPlayListener listener) {
this.listener = listener;
}

/**
* 加载封面
*/
private void loadCoverUrl() {
VideoCoverUtils.load(getContext(), coverIv, coverUrl);
}

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

try {
//设置uri、封面
VideoCoverUtils.load(getContext(), coverIv, playUrl);
loadCoverUrl();
setControlVisibility(GONE, GONE, GONE);
} catch (Exception e) {
KLog.e(e.getMessage());
Expand Down Expand Up @@ -445,8 +456,8 @@ private void onErrorHandle(Exception e) {
*
* @param url
*/
public void playSpecifiedVideo(String url) {
setPlayUrl(url);
public void playSpecifiedVideo(String url,String coverUrl) {
setPlayUrl(url,coverUrl);
//没设置正在播放的视频,默认播放第一个视频
if (TextUtils.isEmpty(playUrl)) {
onErrorHandle(new Exception("playSpecifiedVideo时视频地址为空!!!"));
Expand All @@ -455,7 +466,7 @@ public void playSpecifiedVideo(String url) {

try {
//设置uri、封面
VideoCoverUtils.load(getContext(), coverIv, playUrl);
loadCoverUrl();
coverIv.setVisibility(VISIBLE);
if (mHandler != null) {
mHandler.removeMessages(UPDATE_TIME_AND_PROGRESS);
Expand Down Expand Up @@ -551,15 +562,15 @@ private void onCompleteHandle() {
int playMode = PlayModeUtils.getPlayMode();
//单曲循环模式---一直播放该视频
if (PlayModeUtils.getPlayMode() == PlayMode.PLAY_MODE_SINGLE_CYCLE) {
playSpecifiedVideo(playUrl);
playSpecifiedVideo(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());
playSpecifiedVideo(videoListAdapter.getData().get(i + 1).getUrl(),videoListAdapter.getData().get(i + 1).getCoverUrl());
} else {
playSpecifiedVideo(videoListAdapter.getData().get(0).getUrl());
playSpecifiedVideo(videoListAdapter.getData().get(0).getUrl(),videoListAdapter.getData().get(0).getCoverUrl());
}
break;
}
Expand All @@ -569,7 +580,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());
playSpecifiedVideo(videoListAdapter.getData().get(i + 1).getUrl(),videoListAdapter.getData().get(i + 1).getCoverUrl());
} else {
coverIv.setVisibility(VISIBLE);
btnPlay.setVisibility(VISIBLE);
Expand Down Expand Up @@ -666,7 +677,7 @@ public void playOrPause() {
btnPlay.setBackgroundResource(R.drawable.play_pause);
//开始播放(新的)
//显示TextureView,回调onSurfaceTextureAvailable方法,开始执行新的播放操作
playSpecifiedVideo(playUrl);
playSpecifiedVideo(playUrl,coverUrl);
}
setControlVisibility(GONE, GONE, GONE);
}
Expand Down Expand Up @@ -803,7 +814,7 @@ public void onResume() {
setPlayModelImage();
if (!isFirstEnter && isTextureAvailable) {
btnPlay.setBackgroundResource(R.drawable.play_pause);
playSpecifiedVideo(playUrl);
playSpecifiedVideo(playUrl,coverUrl);
}
isFirstEnter = false;
}
Expand Down
20 changes: 15 additions & 5 deletions custommediaplayer/src/main/res/layout/item_video.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@
android:id="@+id/mongli_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#a0000000" />
android:background="#88000000" />

<TextView
<com.xgw.custommediaplayer.view.MarqueeText
android:id="@+id/center_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:ellipsize="end"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="啦啦啦啦啦"
android:textColor="#ffffff"
android:textSize="15sp" />

<TextView
<com.xgw.custommediaplayer.view.MarqueeText
android:id="@+id/bottom_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -44,7 +49,12 @@
android:layout_marginBottom="10dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:ellipsize="end"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="啦啦啦啦啦"
android:textColor="#ffffff"
Expand Down

0 comments on commit c8abfd0

Please sign in to comment.