-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
RecyclerView3Activity.java
206 lines (160 loc) · 7.15 KB
/
RecyclerView3Activity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.example.gsyvideoplayer;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.transition.Explode;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.gsyvideoplayer.adapter.RecyclerBaseAdapter;
import com.example.gsyvideoplayer.databinding.ActivityRecyclerView2Binding;
import com.example.gsyvideoplayer.holder.RecyclerItemViewHolder;
import com.example.gsyvideoplayer.mediacodec.MediaCodecVideo;
import com.example.gsyvideoplayer.model.VideoModel;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
import java.util.ArrayList;
import java.util.List;
/**
* MediaCodec
*
*
*
* 没有适配小窗口切换的,请悉知
*/
public class RecyclerView3Activity extends AppCompatActivity {
LinearLayoutManager linearLayoutManager;
RecyclerBaseAdapter recyclerBaseAdapter;
List<VideoModel> dataList = new ArrayList<>();
GSYVideoHelper smallVideoHelper;
GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder;
int lastVisibleItem;
int firstVisibleItem;
ActivityRecyclerView2Binding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
// 设置一个exit transition
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().setEnterTransition(new Explode());
getWindow().setExitTransition(new Explode());
}
super.onCreate(savedInstanceState);
binding = ActivityRecyclerView2Binding.inflate(getLayoutInflater());
View rootView = binding.getRoot();
setContentView(rootView);
initView();
binding.listItemRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
Debuger.printfLog("firstVisibleItem " + firstVisibleItem +" lastVisibleItem " + lastVisibleItem);
//大于0说明有播放,//对应的播放列表TAG
if (smallVideoHelper.getPlayPosition() >= 0 && smallVideoHelper.getPlayTAG().equals(RecyclerItemViewHolder.TAG)) {
//当前播放的位置
int position = smallVideoHelper.getPlayPosition();
//不可视的是时候
if ((position < firstVisibleItem || position > lastVisibleItem)) {
//如果是小窗口就不需要处理
if (!smallVideoHelper.isSmall() && !smallVideoHelper.isFull()) {
//小窗口
int size = CommonUtil.dip2px(RecyclerView3Activity.this, 150);
//actionbar为true才不会掉下面去
smallVideoHelper.showSmallVideo(new Point(size, size), true, true);
}
} else {
if (smallVideoHelper.isSmall()) {
smallVideoHelper.smallVideoToNormal();
}
}
}
}
});
}
@Override
protected void onStart() {
super.onStart();
/// 打开硬解码
GSYVideoType.enableMediaCodec();
}
@Override
protected void onDestroy() {
super.onDestroy();
/// 关闭硬解码
GSYVideoType.disableMediaCodec();
smallVideoHelper.releaseVideoPlayer();
GSYVideoManager.releaseAllVideos();
}
@Override
public void onBackPressed() {
if (smallVideoHelper.backFromFull()) {
return;
}
super.onBackPressed();
}
private void initView() {
linearLayoutManager = new LinearLayoutManager(this);
binding.listItemRecycler.setLayoutManager(linearLayoutManager);
resolveData();
recyclerBaseAdapter = new RecyclerBaseAdapter(this, dataList);
binding.listItemRecycler.setAdapter(recyclerBaseAdapter);
smallVideoHelper = new GSYVideoHelper(this, new MediaCodecVideo(this));
smallVideoHelper.setFullViewContainer(binding.videoFullContainer);
//配置
gsySmallVideoHelperBuilder = new GSYVideoHelper.GSYVideoHelperBuilder();
gsySmallVideoHelperBuilder
.setHideActionBar(true)
.setHideStatusBar(true)
.setNeedLockFull(true)
.setCacheWithPlay(true)
.setAutoFullWithSize(false)
.setShowFullAnimation(false)
.setLockLand(true).setVideoAllCallBack(new GSYSampleCallBack() {
@Override
public void onPrepared(String url, Object... objects) {
super.onPrepared(url, objects);
Debuger.printfLog("Duration " + smallVideoHelper.getGsyVideoPlayer().getDuration() + " CurrentPosition " + smallVideoHelper.getGsyVideoPlayer().getCurrentPositionWhenPlaying());
}
@Override
public void onQuitSmallWidget(String url, Object... objects) {
super.onQuitSmallWidget(url, objects);
//大于0说明有播放,//对应的播放列表TAG
if (smallVideoHelper.getPlayPosition() >= 0 && smallVideoHelper.getPlayTAG().equals(RecyclerItemViewHolder.TAG)) {
//当前播放的位置
int position = smallVideoHelper.getPlayPosition();
//不可视的是时候
if ((position < firstVisibleItem || position > lastVisibleItem)) {
//释放掉视频
smallVideoHelper.releaseVideoPlayer();
recyclerBaseAdapter.notifyDataSetChanged();
}
}
}
});
smallVideoHelper.setGsyVideoOptionBuilder(gsySmallVideoHelperBuilder);
recyclerBaseAdapter.setVideoHelper(smallVideoHelper, gsySmallVideoHelperBuilder);
}
private void resolveData() {
for (int i = 0; i < 19; i++) {
VideoModel videoModel = new VideoModel();
dataList.add(videoModel);
}
if (recyclerBaseAdapter != null)
recyclerBaseAdapter.notifyDataSetChanged();
}
}