GlVideoPlayerView.java
11 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package com.glsx.commonres.player;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.glsx.commonres.R;
import com.glsx.commonres.player.widget.GlVideoView;
import com.glsx.commonres.util.FlowCardOverdueStateCallback;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.player.IjkPlayerManager;
import com.shuyu.gsyvideoplayer.player.PlayerFactory;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager;
/**
* 自定义视频播放器View
*/
public class GlVideoPlayerView extends RelativeLayout implements GlVideoView.RemoteVideoListener {
private Context mContext;
private GlVideoPlayerListener mListener;
private GlVideoView videoView;
private OrientationUtils orientationUtils;
public interface GlVideoPlayerListener {
void onClickStartBtn();
void onPlayerPrepared();
void onPlayerPause();
void onPlayerStop();
void onPlayerCacheTimeOut();
void onClickSwitchCamera();
}
public GlVideoPlayerView(Context context) {
super(context);
initView(context);
}
public GlVideoPlayerView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public GlVideoPlayerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
private void initView(Context context) {
mContext = context;
LayoutInflater.from(context).inflate(R.layout.public_layout_video_player, this);
videoView = findViewById(R.id.gl_video_view);
// switch2Exo();
orientationUtils = new OrientationUtils((Activity) context, videoView);
//初始化不打开外部的旋转
orientationUtils.setEnable(false);
videoView.initParams(context);
videoView.registerListener(this);
}
public int getCurrentState() {
return videoView.getCurrentState();
}
/**
* 设置缩略图控件
*
* @param thumbView 缩略图控件
*/
public void setThumbImageView(ImageView thumbView) {
videoView.setThumbImageView(thumbView);
}
/**
* 设置是否显示录像状态UI
*
* @param isRecording 是否录像状态.回看页面为非录像状态
*/
public void setRecordState(boolean isRecording) {
videoView.setRecordState(isRecording);
}
/**
* 是否显示全屏按钮
*
* @param isShow 是否显示
*/
public void setShowFullscreenBtn(boolean isShow) {
videoView.setShowFullscreenBtn(isShow);
}
/**
* 设置全屏按钮点击监听
* @param listener
*/
public void setFullScreenBtnClickListener(View.OnClickListener listener) {
if (null != listener) {
videoView.setFullScreenBtnClickListener(listener);
}
}
/**
* 设置显示视频标题
* @param isShow
*/
public void setShowTitle(boolean isShow) {
videoView.setShowTitle(isShow);
}
/**
* 设置显示返回按钮
* @param isShow
*/
public void setShowBackBtn(boolean isShow) {
videoView.setShowBackBtn(isShow);
}
/**
* 设置是否可以滑动界面改变进度,声音等
* @param isTouchWidget
*/
public void setIsTouchWidget(boolean isTouchWidget) {
videoView.setIsTouchWidget(isTouchWidget);
}
public void pausePlay() {
videoView.pausePlay();
}
public boolean isPlaying() {
return videoView.isPlaying();
}
public void seekTo(int progress) {
videoView.seekTo(progress);
}
/**
* 设置播放URL,并立即播放
*
* @param url
*/
public void setNormalUrlAndPlayNow(String url) {
// videoView.setNormalUrlAndPlayNow(url);
if (!TextUtils.isEmpty(url)) {
getVideoOptionBuilder(url).build(videoView);
videoView.setLiveStreamUrlAndPlayNow(url);
}
}
/**
* 设置播放URL,点击播放按钮才开始播放
*
* @param url
*/
public void setNormalUrlAndClickPlay(String url) {
if (!TextUtils.isEmpty(url)) {
getVideoOptionBuilder(url).build(videoView);
videoView.setNormalUrlAndClickPlay(url);
}
}
/**
* 设置流播放连接,点击开始播放按钮请求直播流.
*
* @param url 流连接
*/
public void setLiveStreamUrlAndPlayNow(String url) {
if (!TextUtils.isEmpty(url)) {
getLiveVideoOptionBuilder(url).build(videoView);
videoView.setLiveStreamUrlAndPlayNow(url);
}
}
private GSYVideoOptionBuilder getLiveVideoOptionBuilder(String url) {
GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder();
// gsyVideoOption.setAutoFullWithSize(true);
//是否使用全屏动画效果
gsyVideoOption.setShowFullAnimation(false);
//是否可以滑动界面改变进度,声音等
gsyVideoOption.setIsTouchWiget(false);
//是否可以全屏滑动界面改变进度,声音等
gsyVideoOption.setIsTouchWigetFull(false);
//一全屏就锁屏横屏,默认false竖屏,可配合setRotateViewAuto使用
gsyVideoOption.setLockLand(true);
//是否边缓存,m3u8等无效
gsyVideoOption.setCacheWithPlay(false);
//播放url
gsyVideoOption.setUrl(url);
return gsyVideoOption;
}
private GSYVideoOptionBuilder getVideoOptionBuilder(String url) {
GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder();
// gsyVideoOption.setAutoFullWithSize(true);
//是否使用全屏动画效果
gsyVideoOption.setShowFullAnimation(false);
//是否可以滑动界面改变进度,声音等
gsyVideoOption.setIsTouchWiget(false);
//是否可以全屏滑动界面改变进度,声音等
gsyVideoOption.setIsTouchWigetFull(false);
//一全屏就锁屏横屏,默认false竖屏,可配合setRotateViewAuto使用
gsyVideoOption.setLockLand(true);
//是否边缓存,m3u8等无效
gsyVideoOption.setCacheWithPlay(true);
//播放url
gsyVideoOption.setUrl(url);
return gsyVideoOption;
}
/**
* 点击返回按钮先退出全屏模式
*
* @param activity Activity的Context对象
* @return true:退出全屏模式,false可以直接退出.
*/
public boolean onVideoBack(Context activity) {
if (orientationUtils != null) {
orientationUtils.backToProtVideo();
}
return videoView.onVideoBack(activity);
}
/**
* 停止播放
*/
public void stopPlay() {
videoView.stopPlay();
}
/**
* 界面退出,销毁播放器资源
*/
public void onDestroyView() {
videoView.onDestroyView(this);
if (orientationUtils != null)
orientationUtils.releaseListener();
orientationUtils = null;
}
/**
* 注册播放状态回调监听,需要获取相应状态的页面需要注册
*
* @param listener 监听回调对象
*/
public void registerPlayerListener(GlVideoPlayerListener listener) {
mListener = listener;
}
/**
* 注销播放状态回调监听,需要获取相应状态的页面需要注册
*/
public void unregisterListener() {
mListener = null;
}
@Override
public void onClickStartBtn() {
if (null != mListener) {
mListener.onClickStartBtn();
}
}
@Override
public void onPlayerPrepared() {
if (null != mListener) {
mListener.onPlayerPrepared();
}
}
@Override
public void onPlayerPause() {
if (null != mListener) {
mListener.onPlayerPause();
}
}
@Override
public void onPlayerStop() {
if (null != mListener) {
mListener.onPlayerStop();
}
}
@Override
public void onPlayerCacheTimeOut() {
if (null != mListener) {
mListener.onPlayerCacheTimeOut();
}
}
@Override
public void onClickSwitchCamera() {
if (null != mListener) {
mListener.onClickSwitchCamera();
}
}
public void setPlayTag(String id) {
videoView.setPlayTag(id);
}
public void setPlayPosition(int playPosition) {
videoView.setPlayPosition(playPosition);
}
public void setReleaseWhenLossAudio(boolean state) {
videoView.setReleaseWhenLossAudio(state);
}
/**
* 加载视频第一帧作为封面
*
* @param context context
* @param url 视频URL
*/
public void loadCover(Context context, String url) {
ImageView imageView = new ImageView(context);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(layoutParams);
// Glide.with(context)
// .load(url)
// .centerCrop()
// .error(R.drawable.icon_travel_video_cover_defult)
// .placeholder(R.drawable.icon_travel_video_cover_defult)
// .into(imageView);
setThumbImageView(imageView);
}
public void setTimeout(int timeOut) {
videoView.setTimeout(timeOut);
}
public void onPlayError(int what, int extra){
videoView.onError(what, extra);
}
public static void switch2Exo2PlayerManager() {
//EXOPlayer内核,支持FFmpeg生成的视频格式播放 避免ffmpeg编码视频出现的有声音无画面情况
// PlayerFactory.setPlayManager(Exo2PlayerManager.class);
}
public static void switch2IjkPlayerManager() {
// PlayerFactory.setPlayManager(IjkPlayerManager.class);
}
public void switch2Exo() {
PlayerFactory.setPlayManager(Exo2PlayerManager.class);
}
public void switch2Ijk() {
PlayerFactory.setPlayManager(IjkPlayerManager.class);
}
public void showHideSwitchCamera(boolean isShow) {
videoView.showHideSwitchCamera(isShow);
}
public void updateCameraDirIcon(boolean isFront) {
videoView.updateCameraDirIcon(isFront);
}
public void resetVideoOptionModel() {
if (videoView != null) {
videoView.resetVideoOptionModel();
}
}
public void setFlowCardOverdueState(boolean isOverdue, FlowCardOverdueStateCallback callback) {
if (videoView != null) {
videoView.updateOverdueState(isOverdue, callback);
}
}
public void remoteLiveFullscreen() {
if (videoView != null) {
videoView.remoteLiveFullscreen();
}
}
}