-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
409 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
app/src/main/java/com/example/gsyvideoplayer/ViewPagerDemoActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.example.gsyvideoplayer; | ||
|
||
import android.os.Bundle; | ||
import android.view.View; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.recyclerview.widget.RecyclerView; | ||
import androidx.viewpager2.widget.ViewPager2; | ||
|
||
import com.example.gsyvideoplayer.adapter.ViewPagerDemoAdapter; | ||
import com.example.gsyvideoplayer.databinding.ActivityViewPager2Binding; | ||
import com.example.gsyvideoplayer.holder.RecyclerItemPlayNormalHolder; | ||
import com.example.gsyvideoplayer.model.VideoModel; | ||
import com.shuyu.gsyvideoplayer.GSYVideoManager; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ViewPagerDemoActivity extends AppCompatActivity { | ||
ActivityViewPager2Binding binding; | ||
|
||
List<VideoModel> dataList = new ArrayList<>(); | ||
|
||
ViewPagerDemoAdapter viewPagerAdapter; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
|
||
binding = ActivityViewPager2Binding.inflate(getLayoutInflater()); | ||
|
||
View rootView = binding.getRoot(); | ||
setContentView(rootView); | ||
|
||
|
||
resolveData(); | ||
viewPagerAdapter = new ViewPagerDemoAdapter(this, dataList); | ||
binding.viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL); | ||
binding.viewPager2.setAdapter(viewPagerAdapter); | ||
binding.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { | ||
@Override | ||
public void onPageSelected(int position) { | ||
super.onPageSelected(position); | ||
//大于0说明有播放 | ||
int playPosition = GSYVideoManager.instance().getPlayPosition(); | ||
if (playPosition >= 0) { | ||
//对应的播放列表TAG | ||
if (GSYVideoManager.instance().getPlayTag().equals(RecyclerItemPlayNormalHolder.TAG) && (position == playPosition)) { | ||
RecyclerView.ViewHolder viewHolder = ((RecyclerView) binding.viewPager2.getChildAt(0)).findViewHolderForAdapterPosition(position); | ||
if (viewHolder instanceof RecyclerItemPlayNormalHolder) { | ||
RecyclerItemPlayNormalHolder recyclerItemNormalHolder = (RecyclerItemPlayNormalHolder) viewHolder; | ||
if (recyclerItemNormalHolder.getPlayer().isInPlayingState()) { | ||
recyclerItemNormalHolder.getPlayer().post(new Runnable() { | ||
@Override | ||
public void run() { | ||
GSYVideoManager.instance().setDisplay(recyclerItemNormalHolder.getPlayer().getCurrentSurface()); | ||
} | ||
}); | ||
GSYVideoManager.onResume(false); | ||
} | ||
} | ||
} else { | ||
GSYVideoManager.onPause(); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onBackPressed() { | ||
if (GSYVideoManager.backFromWindowFull(this)) { | ||
return; | ||
} | ||
super.onBackPressed(); | ||
} | ||
|
||
@Override | ||
protected void onPause() { | ||
super.onPause(); | ||
GSYVideoManager.onPause(); | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
GSYVideoManager.onResume(false); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
GSYVideoManager.releaseAllVideos(); | ||
} | ||
|
||
private void resolveData() { | ||
for (int i = 0; i < 19; i++) { | ||
VideoModel videoModel = new VideoModel(); | ||
dataList.add(videoModel); | ||
} | ||
if (viewPagerAdapter != null) viewPagerAdapter.notifyDataSetChanged(); | ||
} | ||
|
||
|
||
private void playPosition(int position) { | ||
binding.viewPager2.postDelayed(new Runnable() { | ||
@Override | ||
public void run() { | ||
RecyclerView.ViewHolder viewHolder = ((RecyclerView) binding.viewPager2.getChildAt(0)).findViewHolderForAdapterPosition(position); | ||
if (viewHolder != null) { | ||
RecyclerItemPlayNormalHolder recyclerItemNormalHolder = (RecyclerItemPlayNormalHolder) viewHolder; | ||
recyclerItemNormalHolder.getPlayer().startPlayLogic(); | ||
} | ||
} | ||
}, 50); | ||
} | ||
} | ||
|
||
|
||
|
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/example/gsyvideoplayer/adapter/ViewPagerDemoAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.example.gsyvideoplayer.adapter; | ||
|
||
import android.content.Context; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.recyclerview.widget.RecyclerView; | ||
|
||
import com.example.gsyvideoplayer.R; | ||
import com.example.gsyvideoplayer.holder.RecyclerItemPlayNormalHolder; | ||
import com.example.gsyvideoplayer.holder.RecyclerPageItemViewHolder; | ||
import com.example.gsyvideoplayer.model.VideoModel; | ||
|
||
import java.util.List; | ||
|
||
public class ViewPagerDemoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | ||
private final static String TAG = "ViewPagerDemoAdapter"; | ||
|
||
private List<VideoModel> itemDataList = null; | ||
private Context context = null; | ||
|
||
public ViewPagerDemoAdapter(Context context, List<VideoModel> itemDataList) { | ||
this.itemDataList = itemDataList; | ||
this.context = context; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
if (viewType == 2) { | ||
View v = LayoutInflater.from(context).inflate(R.layout.layout_viewpager2_item, parent, false); | ||
final RecyclerView.ViewHolder holder = new RecyclerItemPlayNormalHolder(context, v); | ||
return holder; | ||
} else { | ||
View v = LayoutInflater.from(context).inflate(R.layout.layout_viewpager_demo_item, parent, false); | ||
final RecyclerView.ViewHolder holder = new RecyclerPageItemViewHolder(context, v); | ||
return holder; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) { | ||
if(position == 2) { | ||
RecyclerItemPlayNormalHolder recyclerItemViewHolder = (RecyclerItemPlayNormalHolder) holder; | ||
recyclerItemViewHolder.setRecyclerBaseAdapter(this); | ||
recyclerItemViewHolder.onBind(position, itemDataList.get(position)); | ||
} else { | ||
RecyclerPageItemViewHolder recyclerItemViewHolder = (RecyclerPageItemViewHolder) holder; | ||
recyclerItemViewHolder.setRecyclerBaseAdapter(this); | ||
recyclerItemViewHolder.setText(position + ""); | ||
} | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return itemDataList.size(); | ||
} | ||
|
||
|
||
@Override | ||
public int getItemViewType(int position) { | ||
if (position == 2) { | ||
return 2; | ||
} | ||
return 1; | ||
} | ||
|
||
public void setListData(List<VideoModel> data) { | ||
itemDataList = data; | ||
notifyDataSetChanged(); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
app/src/main/java/com/example/gsyvideoplayer/holder/RecyclerItemPlayNormalHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.example.gsyvideoplayer.holder; | ||
|
||
import android.content.Context; | ||
import android.view.View; | ||
import android.widget.ImageView; | ||
|
||
import com.example.gsyvideoplayer.R; | ||
import com.example.gsyvideoplayer.model.VideoModel; | ||
import com.example.gsyvideoplayer.video.SampleCoverVideo; | ||
import com.shuyu.gsyvideoplayer.GSYVideoManager; | ||
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder; | ||
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack; | ||
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by guoshuyu on 2017/1/9. | ||
*/ | ||
|
||
public class RecyclerItemPlayNormalHolder extends RecyclerItemBaseHolder { | ||
|
||
public final static String TAG = "RecyclerView2List"; | ||
|
||
protected Context context; | ||
|
||
SampleCoverVideo gsyVideoPlayer; | ||
|
||
ImageView imageView; | ||
|
||
GSYVideoOptionBuilder gsyVideoOptionBuilder; | ||
|
||
public RecyclerItemPlayNormalHolder(Context context, View v) { | ||
super(v); | ||
this.context = context; | ||
gsyVideoPlayer = v.findViewById(R.id.video_item_player); | ||
imageView = new ImageView(context); | ||
gsyVideoOptionBuilder = new GSYVideoOptionBuilder(); | ||
} | ||
|
||
public void onBind(final int position, VideoModel videoModel) { | ||
|
||
String url; | ||
String title; | ||
if (position % 2 == 0) { | ||
url = "https://pointshow.oss-cn-hangzhou.aliyuncs.com/McTk51586843620689.mp4"; | ||
title = "这是title"; | ||
} else { | ||
url = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"; | ||
title = "哦?Title?"; | ||
} | ||
|
||
|
||
Map<String, String> header = new HashMap<>(); | ||
header.put("ee", "33"); | ||
int playPosition = GSYVideoManager.instance().getPlayPosition(); | ||
if (!GSYVideoManager.instance().isPlaying() && (position != playPosition)) { | ||
//防止错位,离开释放 | ||
//gsyVideoPlayer.initUIState(); | ||
gsyVideoOptionBuilder.setIsTouchWiget(false) | ||
//.setThumbImageView(imageView) | ||
.setUrl(url).setVideoTitle(title).setCacheWithPlay(false).setRotateViewAuto(true).setLockLand(true).setPlayTag(TAG).setMapHeadData(header).setShowFullAnimation(true).setNeedLockFull(true).setPlayPosition(position).setVideoAllCallBack(new GSYSampleCallBack() { | ||
@Override | ||
public void onPrepared(String url, Object... objects) { | ||
super.onPrepared(url, objects); | ||
if (!gsyVideoPlayer.isIfCurrentIsFullscreen()) { | ||
//静音 | ||
GSYVideoManager.instance().setNeedMute(true); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void onAutoComplete(String url, Object... objects) { | ||
super.onAutoComplete(url, objects); | ||
} | ||
|
||
@Override | ||
public void onQuitFullscreen(String url, Object... objects) { | ||
super.onQuitFullscreen(url, objects); | ||
//全屏不静音 | ||
GSYVideoManager.instance().setNeedMute(true); | ||
} | ||
|
||
@Override | ||
public void onEnterFullscreen(String url, Object... objects) { | ||
super.onEnterFullscreen(url, objects); | ||
GSYVideoManager.instance().setNeedMute(false); | ||
gsyVideoPlayer.getCurrentPlayer().getTitleTextView().setText((String) objects[0]); | ||
} | ||
}).build(gsyVideoPlayer); | ||
|
||
|
||
//增加title | ||
gsyVideoPlayer.getTitleTextView().setVisibility(View.GONE); | ||
|
||
//设置返回键 | ||
gsyVideoPlayer.getBackButton().setVisibility(View.GONE); | ||
|
||
//设置全屏按键功能 | ||
gsyVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
resolveFullBtn(gsyVideoPlayer); | ||
} | ||
}); | ||
|
||
gsyVideoPlayer.loadCoverImageBy(R.mipmap.xxx2, R.mipmap.xxx2); | ||
} | ||
} | ||
|
||
/** | ||
* 全屏幕按键处理 | ||
*/ | ||
private void resolveFullBtn(final StandardGSYVideoPlayer standardGSYVideoPlayer) { | ||
standardGSYVideoPlayer.startWindowFullscreen(context, true, true); | ||
} | ||
|
||
public SampleCoverVideo getPlayer() { | ||
return gsyVideoPlayer; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/example/gsyvideoplayer/holder/RecyclerPageItemViewHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.example.gsyvideoplayer.holder; | ||
|
||
import android.content.Context; | ||
import android.view.View; | ||
import android.widget.FrameLayout; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
import com.example.gsyvideoplayer.R; | ||
import com.example.gsyvideoplayer.model.VideoModel; | ||
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper; | ||
|
||
|
||
/** | ||
* Created by GUO on 2015/12/3. | ||
*/ | ||
public class RecyclerPageItemViewHolder extends RecyclerItemBaseHolder { | ||
|
||
public final static String TAG = "RecyclerPageItemViewHolder"; | ||
|
||
protected Context context ; | ||
protected TextView textView ; | ||
|
||
public RecyclerPageItemViewHolder(Context context, View v) { | ||
super(v); | ||
this.context = context; | ||
textView = v.findViewById(R.id.viewpage_demo_text); | ||
} | ||
|
||
public void setText(String text) { | ||
textView.setText(text); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.