BaseVideoView.java
11.5 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
package com.cnlive.gundam.video.view;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import com.cnlive.gundam.R;
import com.cnlive.gundam.databinding.LayoutVideoViewBinding;
import com.cnlive.gundam.video.model.DataSource;
import com.cnlive.gundam.video.model.VideoModel;
import com.cnlive.libs.util.Config;
import com.cnlive.libs.util.SharedPreferencesHelper;
import com.cnlive.libs.video.video.VideoStatusUtil;
import com.cnlive.libs.video.video.base.IMediaPlayer;
import com.cnlive.libs.video.video.base.ISwitchVideoView;
import com.cnlive.libs.video.video.base.IVideoStatus;
/**
* Created by gujun on 2017/4/5.
* 这个里只是关于播放器和ugc直播播放状态的相关处理
*/
public abstract class BaseVideoView extends RelativeLayout implements
IMediaPlayer.OnCompletionListener, IMediaPlayer.OnInfoListener,
IMediaPlayer.OnErrorListener, IMediaPlayer.OnPlayStateListener,
IMediaPlayer.OnSeekCompleteListener, IMediaPlayer.OnPreparedListener,
IMediaPlayer.OnBufferingUpdateListener, ISwitchVideoView.OnSwitchVideoPreparedListener,
ISwitchVideoView.OnSwitchVideoErrorListener, ISwitchVideoView.OnSwitchVideoCompleteListener {
protected Context mContext;
protected LayoutVideoViewBinding binding;
//记录当前视频播放状态
protected boolean isPlay = false;
//当前播放器的播放进度 重试的时候从此时间播放
private long curPlayPosition = -1;
//prepare后是否seekTo 为了重试后seekComplete过滤
private boolean isPrepareSeek = false;
//播放数据
protected VideoModel videoModel;
//记录当前是否是直播节目
protected static boolean isLive;
//记录当前视频是否能播
protected boolean canPlay = true;
//本地缓存数据工具类
private SharedPreferencesHelper sharedPreferencesHelper;
//播放进度保存至本地缓存的标识前缀
private final String CUR_PLAY_POSITION_PRE = "CurPlayPosition_";
//保存切换码率之前的码率
private String preRate = Config.TYPE_PLAY_RATE_2;
//保存当前要切换的码率
private String curRate = "";
//记录当前是否正在切换视频
protected boolean switchingVideoRate = false;
private DataSource curDataSource;
//播放器播放状态的监听
private OnVideoViewPlayStatusListener l;
//ugclive需要权限监听
private OnUgcLiveStatusPermissionListener ugcLivePermissionListener;
public BaseVideoView(Context context) {
this(context, null);
}
public BaseVideoView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BaseVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
init();
}
public void setVideoViewPlayStatus(OnVideoViewPlayStatusListener listener) {
l = listener;
}
protected void init() {
sharedPreferencesHelper = SharedPreferencesHelper.getInstance(getContext());
binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.layout_video_view, this, true);
binding.videoView.setOnPreparedListener(this);
binding.videoView.setOnInfoListener(this);
binding.videoView.setOnErrorListener(this);
binding.videoView.setOnCompletionListener(this);
binding.videoView.setOnSeekCompleteListener(this);
binding.videoView.setOnPlayStateListener(this);
binding.videoView.setOnBufferingUpdateListener(this);
binding.videoView.setScreenOnWhilePlaying(true);
binding.videoView.setOnSwitchVideoPreparedListener(this);
binding.videoView.setOnSwitchVideoErrorListener(this);
binding.videoView.setOnSwitchVideoCompleteListener(this);
setKeepScreenOn(true);
}
public void setDataSource() {
//只有ugcLive视频才有状态监听
if (videoModel.getType().equals(VideoView.TYPE_UGC_LIVE)) {
if (Build.VERSION.SDK_INT >= 23) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_PHONE_STATE);
if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
if(ugcLivePermissionListener != null)
ugcLivePermissionListener.onShouldRequestPermission();
} else {
addUgcLiveStatus();
}
} else {
addUgcLiveStatus();
}
}
if (isLive) {
curPlayPosition = -1;
binding.videoView.setDataSource(curDataSource = new DataSource(videoModel.getId(), Config.TYPE_LIVE, Config.TYPE_PLAY_RATE_2));
} else {
curPlayPosition = sharedPreferencesHelper.getLong(CUR_PLAY_POSITION_PRE + videoModel.getId());
binding.videoView.setDataSource(curDataSource = new DataSource(videoModel.getId(), Config.TYPE_VOD, Config.TYPE_PLAY_RATE_2));
}
preRate = Config.TYPE_PLAY_RATE_2;
}
/**
* 设置UGC LIVE的直播状态监听
*/
public void setUgcLiveStatusPermissionListener(OnUgcLiveStatusPermissionListener l){
ugcLivePermissionListener = l;
}
/**
* 添加UGC LIVE的直播状态监听
*/
public void addUgcLiveStatus(){
VideoStatusUtil.setOnVideoPlayStatusCallback(mOnVideoStatusCallback);
VideoStatusUtil.init(videoModel.getId());
}
/**
* 设置当前播放进度
*
* @param curPosition
*/
protected void setPlayPosition(long curPosition) {
curPlayPosition = curPosition;
}
public void onResume() {
if (!binding.videoView.isPlaying() && isPlay) {
setPlayState(true);
}
}
public void onPause() {
if (binding.videoView.isPlaying()) {
setPlayState(false);
}
}
public void onDestroy() {
binding.videoView.release();
VideoStatusUtil.quit();
saveCurPlayPosition();
}
//保存当前播放视频的播放进度
protected void saveCurPlayPosition() {
if (!isLive && curDataSource != null && sharedPreferencesHelper != null) {
sharedPreferencesHelper.setValue(CUR_PLAY_POSITION_PRE + curDataSource.getDataSourceId(), curPlayPosition);
}
}
protected void retry() {
binding.videoView.reload();
}
protected void replay() {
isPrepareSeek = true;
binding.videoView.seekTo(0);
}
/**
* 设置播放状态
*
* @param isPlay
*/
protected void setPlayState(boolean isPlay) {
if (isPlay) binding.videoView.start();
else binding.videoView.pause();
}
/**
* 播放按钮点击
*/
public void playBtnClick() {
isPlay = !isPlay;
setPlayState(isPlay);
}
/**
* 切换视频码率
*
* @param rate
*/
public void switchVideoRate(String rate) {
//TODO 换成无缝切换
curRate = rate;
if (binding.controllerView != null)
binding.controllerView.setSwitchRateType(PlayerControllerView.SWITCH_RATE_START, curRate);
curDataSource.setRate(rate);
if (binding.videoView != null)
binding.videoView.switchVideo(curDataSource);
}
//----------------------播放器相关回调 start-------------------------------
@Override
public void onCompletion(IMediaPlayer iMediaPlayer) {
isPlay = false;
}
@Override
public boolean onError(IMediaPlayer iMediaPlayer, int i, int i1) {
isPlay = false;
return false;
}
@Override
public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
return false;
}
@Override
public void onPlayState(boolean isPlay) {
if (l != null) l.onVideoViewPlayStatus(isPlay);
}
@Override
public void onPrepared(IMediaPlayer iMediaPlayer) {
if (curPlayPosition > 0) {
isPrepareSeek = true;
iMediaPlayer.seekTo(curPlayPosition);
} else {
if (canPlay && !iMediaPlayer.getPlayState()) {
setPlayState(true);
isPlay = true;
}
}
}
@Override
public void onSeekComplete(IMediaPlayer iMediaPlayer) {
if (!isPrepareSeek) return;
isPrepareSeek = false;
setPlayState(true);
isPlay = true;
}
@Override
public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int i) {
}
@Override
public void onSwitchVideoError(IMediaPlayer iMediaPlayer, int i, int i1) {
binding.videoView.releaseSwitchVideo();
switchingVideoRate = false;
//切换失败继续播切换之前的码率
curDataSource.setRate(preRate);
if (binding.controllerView != null) {
binding.controllerView.setCurRate(preRate);
binding.controllerView.setSwitchRateType(PlayerControllerView.SWITCH_RATE_ERROR, curRate);
}
}
@Override
public void onSwitchVideoComplete(IMediaPlayer iMediaPlayer) {
switchingVideoRate = false;
if (binding.controllerView != null)
binding.controllerView.setSwitchRateType(PlayerControllerView.SWITCH_RATE_FINISH, curRate);
preRate = curRate;
}
@Override
public void onSwitchVideoPrepared(IMediaPlayer iMediaPlayer) {
switchingVideoRate = true;
if (binding.videoView != null) {
binding.videoView.switchVideoStart(binding.videoView.getCurrentPosition());
}
}
// ---------------------ugc直播状态的回调---------------------------------
private IVideoStatus.VideoStatusCallback mOnVideoStatusCallback = new IVideoStatus.VideoStatusCallback() {
@Override
public void onStatusChange(int status) {
switch (status) {
case IVideoStatus.MEDIA_STATUS_LIVING:
//直播中'
canPlay = true;
if (!binding.videoView.getPlayState()) {
binding.videoView.reload();
binding.infoView.show(PlayerInfoView.SHOW_BUFFERING);
}
break;
case IVideoStatus.MEDIA_STATUS_LEAVE:
//主播暂时离开
canPlay = false;
binding.videoView.stop();
binding.infoView.show(PlayerInfoView.SHOW_UGC_LIVE_STATUS, mContext.getString(R.string.player_status_leave));
break;
case IVideoStatus.MEDIA_STATUS_END:
//直播结束
canPlay = false;
binding.videoView.stop();
binding.infoView.show(PlayerInfoView.SHOW_UGC_LIVE_STATUS, mContext.getString(R.string.player_status_end));
break;
}
}
@Override
public void onError(int what, String extra) {
Log.e("ugcLive status", "onError: " + extra);
}
};
//播放器播放状态监听
public interface OnVideoViewPlayStatusListener {
void onVideoViewPlayStatus(boolean isPlay);
}
//ugc live 监听直播状态得需要android.permission.READ_PHONE_STATE
public interface OnUgcLiveStatusPermissionListener{
void onShouldRequestPermission();
}
}