-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
SmartPickVideo.java
360 lines (301 loc) · 10.6 KB
/
SmartPickVideo.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.example.gsyvideoplayer.video;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.gsyvideoplayer.R;
import com.example.gsyvideoplayer.model.SwitchVideoModel;
import com.example.gsyvideoplayer.view.LoadingDialog;
import com.example.gsyvideoplayer.view.SwitchVideoTypeDialog;
import com.shuyu.gsyvideoplayer.GSYVideoBaseManager;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.listener.GSYMediaPlayerListener;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* 无缝切换视频的DEMO
* 这里是切换清晰度,稍微修改下也可以作为切换下一集等
*/
public class SmartPickVideo extends StandardGSYVideoPlayer {
private TextView mSwitchSize;
private List<SwitchVideoModel> mUrlList = new ArrayList<>();
//记住切换数据源类型
private int mType = 0;
//数据源
private int mSourcePosition = 0;
private int mPreSourcePosition = 0;
private String mTypeText = "标准";
private GSYVideoManager mTmpManager;
//切换过程中最好弹出loading,不给其他任何操作
private LoadingDialog mLoadingDialog;
private boolean isChanging;
/**
* 1.5.0开始加入,如果需要不同布局区分功能,需要重载
*/
public SmartPickVideo(Context context, Boolean fullFlag) {
super(context, fullFlag);
}
public SmartPickVideo(Context context) {
super(context);
}
public SmartPickVideo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void init(Context context) {
super.init(context);
initView();
}
private void initView() {
mSwitchSize = (TextView) findViewById(R.id.switchSize);
//切换视频清晰度
mSwitchSize.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mHadPlay && !isChanging) {
showSwitchDialog();
}
}
});
}
/**
* 设置播放URL
*
* @param url 播放url
* @param cacheWithPlay 是否边播边缓存
* @param title title
* @return
*/
public boolean setUp(List<SwitchVideoModel> url, boolean cacheWithPlay, String title) {
mUrlList = url;
return setUp(url.get(mSourcePosition).getUrl(), cacheWithPlay, title);
}
/**
* 设置播放URL
*
* @param url 播放url
* @param cacheWithPlay 是否边播边缓存
* @param cachePath 缓存路径,如果是M3U8或者HLS,请设置为false
* @param title title
* @return
*/
public boolean setUp(List<SwitchVideoModel> url, boolean cacheWithPlay, File cachePath, String title) {
mUrlList = url;
return setUp(url.get(mSourcePosition).getUrl(), cacheWithPlay, cachePath, title);
}
@Override
public int getLayoutId() {
return R.layout.sample_video_pick;
}
/**
* 全屏时将对应处理参数逻辑赋给全屏播放器
*
* @param context
* @param actionBar
* @param statusBar
* @return
*/
@Override
public GSYBaseVideoPlayer startWindowFullscreen(Context context, boolean actionBar, boolean statusBar) {
SmartPickVideo sampleVideo = (SmartPickVideo) super.startWindowFullscreen(context, actionBar, statusBar);
sampleVideo.mSourcePosition = mSourcePosition;
sampleVideo.mListItemRect = mListItemRect;
sampleVideo.mListItemSize = mListItemSize;
sampleVideo.mType = mType;
sampleVideo.mUrlList = mUrlList;
sampleVideo.mTypeText = mTypeText;
sampleVideo.mSwitchSize.setText(mTypeText);
return sampleVideo;
}
/**
* 推出全屏时将对应处理参数逻辑返回给非播放器
*
* @param oldF
* @param vp
* @param gsyVideoPlayer
*/
@Override
protected void resolveNormalVideoShow(View oldF, ViewGroup vp, GSYVideoPlayer gsyVideoPlayer) {
super.resolveNormalVideoShow(oldF, vp, gsyVideoPlayer);
if (gsyVideoPlayer != null) {
SmartPickVideo sampleVideo = (SmartPickVideo) gsyVideoPlayer;
mSourcePosition = sampleVideo.mSourcePosition;
mType = sampleVideo.mType;
mTypeText = sampleVideo.mTypeText;
mSwitchSize.setText(mTypeText);
setUp(mUrlList, mCache, mCachePath, mTitle);
}
}
@Override
public void onAutoCompletion() {
super.onAutoCompletion();
releaseTmpManager();
}
@Override
public void onCompletion() {
super.onCompletion();
releaseTmpManager();
}
/**
* 弹出切换清晰度
*/
private void showSwitchDialog() {
if (!mHadPlay) {
return;
}
SwitchVideoTypeDialog switchVideoTypeDialog = new SwitchVideoTypeDialog(getContext());
switchVideoTypeDialog.initList(mUrlList, new SwitchVideoTypeDialog.OnListItemClickListener() {
@Override
public void onItemClick(int position) {
resolveStartChange(position);
}
});
switchVideoTypeDialog.show();
}
private void resolveChangeUrl(boolean cacheWithPlay, File cachePath, String url) {
if (mTmpManager != null) {
mCache = cacheWithPlay;
mCachePath = cachePath;
mOriginUrl = url;
this.mUrl = url;
}
}
private GSYMediaPlayerListener gsyMediaPlayerListener = new GSYMediaPlayerListener() {
@Override
public void onPrepared() {
if (mTmpManager != null) {
mTmpManager.start();
mTmpManager.seekTo(getCurrentPositionWhenPlaying());
}
}
@Override
public void onAutoCompletion() {
}
@Override
public void onCompletion() {
}
@Override
public void onBufferingUpdate(int percent) {
}
@Override
public void onSeekComplete() {
if (mTmpManager != null) {
GSYVideoBaseManager manager = GSYVideoManager.instance();
GSYVideoManager.changeManager(mTmpManager);
mTmpManager.setLastListener(manager.lastListener());
mTmpManager.setListener(manager.listener());
manager.setDisplay(null);
Debuger.printfError("**** showDisplay onSeekComplete ***** " + mSurface);
Debuger.printfError("**** showDisplay onSeekComplete isValid***** " + mSurface.isValid());
mTmpManager.setDisplay(mSurface);
changeUiToPlayingClear();
resolveChangedResult();
manager.releaseMediaPlayer();
}
}
@Override
public void onError(int what, int extra) {
mSourcePosition = mPreSourcePosition;
if (mTmpManager != null) {
mTmpManager.releaseMediaPlayer();
}
post(new Runnable() {
@Override
public void run() {
resolveChangedResult();
Toast.makeText(mContext, "change Fail", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onInfo(int what, int extra) {
}
@Override
public void onVideoSizeChanged() {
}
@Override
public void onBackFullscreen() {
}
@Override
public void onVideoPause() {
}
@Override
public void onVideoResume() {
}
@Override
public void onVideoResume(boolean seek) {
}
};
private void resolveStartChange(int position) {
final String name = mUrlList.get(position).getName();
if (mSourcePosition != position) {
if ((mCurrentState == GSYVideoPlayer.CURRENT_STATE_PLAYING
|| mCurrentState == GSYVideoPlayer.CURRENT_STATE_PAUSE)) {
showLoading();
final String url = mUrlList.get(position).getUrl();
cancelProgressTimer();
hideAllWidget();
if (mTitle != null && mTitleTextView != null) {
mTitleTextView.setText(mTitle);
}
mPreSourcePosition = mSourcePosition;
isChanging = true;
mTypeText = name;
mSwitchSize.setText(name);
mSourcePosition = position;
//创建临时管理器执行加载播放
mTmpManager = GSYVideoManager.tmpInstance(gsyMediaPlayerListener);
mTmpManager.initContext(getContext().getApplicationContext());
resolveChangeUrl(mCache, mCachePath, url);
mTmpManager.prepare(mUrl, mMapHeadData, mLooping, mSpeed, mCache, mCachePath, null);
changeUiToPlayingBufferingShow();
}
} else {
Toast.makeText(getContext(), "已经是 " + name, Toast.LENGTH_LONG).show();
}
}
private void resolveChangedResult() {
isChanging = false;
mTmpManager = null;
final String name = mUrlList.get(mSourcePosition).getName();
final String url = mUrlList.get(mSourcePosition).getUrl();
mTypeText = name;
mSwitchSize.setText(name);
resolveChangeUrl(mCache, mCachePath, url);
hideLoading();
}
private void releaseTmpManager() {
if (mTmpManager != null) {
mTmpManager.releaseMediaPlayer();
mTmpManager = null;
}
}
private void showLoading() {
hideLoading();
mLoadingDialog = new LoadingDialog(mContext);
mLoadingDialog.show();
}
private void hideLoading() {
if (mLoadingDialog != null) {
mLoadingDialog.dismiss();
mLoadingDialog = null;
}
}
@Override
public boolean onSurfaceDestroyed(Surface surface) {
//清空释放
setDisplay(null);
//同一消息队列中去release
//todo 需要处理为什么全屏时全屏的surface会被释放了
//releaseSurface(surface);
return true;
}
}