-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathGSYVideoView.java
1181 lines (1001 loc) · 33.5 KB
/
GSYVideoView.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.shuyu.gsyvideoplayer.video.base;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import com.shuyu.gsyvideoplayer.R;
import com.shuyu.gsyvideoplayer.listener.GSYMediaPlayerListener;
import com.shuyu.gsyvideoplayer.listener.VideoAllCallBack;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.utils.NetInfoModule;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.getTextSpeed;
/**
* 视频回调与状态处理等相关层
* Created by guoshuyu on 2017/8/2.
*/
public abstract class GSYVideoView extends GSYTextureRenderView implements GSYMediaPlayerListener {
//正常
public static final int CURRENT_STATE_NORMAL = 0;
//准备中
public static final int CURRENT_STATE_PREPAREING = 1;
//播放中
public static final int CURRENT_STATE_PLAYING = 2;
//开始缓冲
public static final int CURRENT_STATE_PLAYING_BUFFERING_START = 3;
//暂停
public static final int CURRENT_STATE_PAUSE = 5;
//自动播放结束
public static final int CURRENT_STATE_AUTO_COMPLETE = 6;
//错误状态
public static final int CURRENT_STATE_ERROR = 7;
//避免切换时频繁setup
public static final int CHANGE_DELAY_TIME = 2000;
//当前的播放状态
protected int mCurrentState = -1;
//播放的tag,防止错误,因为普通的url也可能重复
protected int mPlayPosition = -22;
//屏幕宽度
protected int mScreenWidth;
//屏幕高度
protected int mScreenHeight;
//缓存进度
protected int mBufferPoint;
//备份缓存前的播放状态
protected int mBackUpPlayingBufferState = -1;
//从哪个开始播放
protected long mSeekOnStart = -1;
//当前的播放位置
protected long mCurrentPosition;
//保存切换时的时间,避免频繁契合
protected long mSaveChangeViewTIme = 0;
//播放速度
protected float mSpeed = 1;
//是否播边边缓冲
protected boolean mCache = false;
//当前是否全屏
protected boolean mIfCurrentIsFullscreen = false;
//循环
protected boolean mLooping = false;
//是否播放过
protected boolean mHadPlay = false;
//是否发送了网络改变
protected boolean mNetChanged = false;
//是否不变调
protected boolean mSoundTouch = false;
//是否需要显示暂停锁定效果
protected boolean mShowPauseCover = false;
//是否准备完成前调用了暂停
protected boolean mPauseBeforePrepared = false;
//Prepared之后是否自动开始播放
protected boolean mStartAfterPrepared = true;
//Prepared
protected boolean mHadPrepared = false;
//是否播放器当失去音频焦点
protected boolean mReleaseWhenLossAudio = true;
//音频焦点的监听
protected AudioManager mAudioManager;
//播放的tag,防止错误,因为普通的url也可能重复
protected String mPlayTag = "";
//上下文
protected Context mContext;
//原来的url
protected String mOriginUrl;
//转化后的URL
protected String mUrl;
//标题
protected String mTitle;
//网络状态
protected String mNetSate = "NORMAL";
// 是否需要覆盖拓展类型
protected String mOverrideExtension;
//缓存路径,可不设置
protected File mCachePath;
//视频回调
protected VideoAllCallBack mVideoAllCallBack;
//http request header
protected Map<String, String> mMapHeadData = new HashMap<>();
//网络监听
protected NetInfoModule mNetInfoModule;
public GSYVideoView(@NonNull Context context) {
super(context);
init(context);
}
public GSYVideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GSYVideoView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public GSYVideoView(Context context, Boolean fullFlag) {
super(context);
mIfCurrentIsFullscreen = fullFlag;
init(context);
}
@Override
protected void showPauseCover() {
if (mCurrentState == CURRENT_STATE_PAUSE && mFullPauseBitmap != null
&& !mFullPauseBitmap.isRecycled() && mShowPauseCover
&& mSurface != null && mSurface.isValid()) {
if (getGSYVideoManager().isSurfaceSupportLockCanvas()) {
try {
RectF rectF = new RectF(0, 0, mTextureView.getWidth(), mTextureView.getHeight());
Canvas canvas;
canvas = mSurface.lockCanvas(new Rect(0, 0, mTextureView.getWidth(), mTextureView.getHeight()));
if (canvas != null) {
canvas.drawBitmap(mFullPauseBitmap, null, rectF, null);
mSurface.unlockCanvasAndPost(canvas);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@Override
protected void releasePauseCover() {
try {
if (mCurrentState != CURRENT_STATE_PAUSE && mFullPauseBitmap != null
&& !mFullPauseBitmap.isRecycled() && mShowPauseCover) {
mFullPauseBitmap.recycle();
mFullPauseBitmap = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getCurrentVideoWidth() {
if (getGSYVideoManager() != null) {
return getGSYVideoManager().getVideoWidth();
}
return 0;
}
@Override
public int getCurrentVideoHeight() {
if (getGSYVideoManager() != null) {
return getGSYVideoManager().getVideoHeight();
}
return 0;
}
@Override
public int getVideoSarNum() {
if (getGSYVideoManager() != null) {
return getGSYVideoManager().getVideoSarNum();
}
return 0;
}
@Override
public int getVideoSarDen() {
if (getGSYVideoManager() != null) {
return getGSYVideoManager().getVideoSarDen();
}
return 0;
}
protected void updatePauseCover() {
if ((mFullPauseBitmap == null || mFullPauseBitmap.isRecycled()) && mShowPauseCover) {
try {
initCover();
} catch (Exception e) {
e.printStackTrace();
mFullPauseBitmap = null;
}
}
}
protected Context getActivityContext() {
return CommonUtil.getActivityContext(getContext());
}
protected void init(Context context) {
if (getActivityContext() != null) {
this.mContext = getActivityContext();
} else {
this.mContext = context;
}
initInflate(mContext);
mTextureViewContainer = (ViewGroup) findViewById(R.id.surface_container);
if (isInEditMode())
return;
mScreenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
mScreenHeight = mContext.getResources().getDisplayMetrics().heightPixels;
mAudioManager = (AudioManager) mContext.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
}
protected void initInflate(Context context) {
try {
View.inflate(context, getLayoutId(), this);
} catch (InflateException e) {
if (e.toString().contains("GSYImageCover")) {
Debuger.printfError("********************\n" +
"***** 注意 *****" +
"********************\n" +
"*该版本需要清除布局文件中的GSYImageCover\n" +
"**** Attention ***\n" +
"*Please remove GSYImageCover from Layout in this Version\n" +
"********************\n");
e.printStackTrace();
throw new InflateException("该版本需要清除布局文件中的GSYImageCover,please remove GSYImageCover from your layout");
} else {
e.printStackTrace();
}
}
}
/**
* 开始播放逻辑
*/
protected void startButtonLogic() {
if (mVideoAllCallBack != null && (mCurrentState == CURRENT_STATE_NORMAL
|| mCurrentState == CURRENT_STATE_AUTO_COMPLETE)) {
Debuger.printfLog("onClickStartIcon");
mVideoAllCallBack.onClickStartIcon(mOriginUrl, mTitle, this);
} else if (mVideoAllCallBack != null) {
Debuger.printfLog("onClickStartError");
mVideoAllCallBack.onClickStartError(mOriginUrl, mTitle, this);
}
prepareVideo();
}
/**
* 开始状态视频播放
*/
protected void prepareVideo() {
startPrepare();
}
protected void startPrepare() {
if (getGSYVideoManager().listener() != null) {
getGSYVideoManager().listener().onCompletion();
}
if (mVideoAllCallBack != null) {
Debuger.printfLog("onStartPrepared");
mVideoAllCallBack.onStartPrepared(mOriginUrl, mTitle, this);
}
getGSYVideoManager().setListener(this);
getGSYVideoManager().setPlayTag(mPlayTag);
getGSYVideoManager().setPlayPosition(mPlayPosition);
mAudioManager.requestAudioFocus(onAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
try {
if (mContext instanceof Activity) {
((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
} catch (Exception e) {
e.printStackTrace();
}
mBackUpPlayingBufferState = -1;
getGSYVideoManager().prepare(mUrl, (mMapHeadData == null) ? new HashMap<String, String>() : mMapHeadData, mLooping, mSpeed, mCache, mCachePath, mOverrideExtension);
setStateAndUi(CURRENT_STATE_PREPAREING);
}
/**
* 监听是否有外部其他多媒体开始播放
*/
protected AudioManager.OnAudioFocusChangeListener onAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
onGankAudio();
break;
case AudioManager.AUDIOFOCUS_LOSS:
onLossAudio();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
onLossTransientAudio();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
onLossTransientCanDuck();
break;
}
}
};
/**
* 获得了Audio Focus
*/
protected void onGankAudio() {
}
/**
* 失去了Audio Focus,并将会持续很长的时间
*/
protected void onLossAudio() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
if (GSYVideoView.this.mReleaseWhenLossAudio) {
GSYVideoView.this.releaseVideos();
} else {
if(getGSYVideoManager().listener() != null)
getGSYVideoManager().listener().onVideoPause();
}
}
});
}
/**
* 暂时失去Audio Focus,并会很快再次获得
*/
protected void onLossTransientAudio() {
try {
getGSYVideoManager().listener().onVideoPause();
} catch (Exception var2) {
var2.printStackTrace();
}
}
/**
* 暂时失去AudioFocus,但是可以继续播放,不过要在降低音量
*/
protected void onLossTransientCanDuck() {
}
/**
* 设置播放URL
*
* @param url 播放url
* @param cacheWithPlay 是否边播边缓存
* @param title title
* @return
*/
public boolean setUp(String url, boolean cacheWithPlay, String title) {
return setUp(url, cacheWithPlay, ((File) null), title);
}
/**
* 设置播放URL
*
* @param url 播放url
* @param cacheWithPlay 是否边播边缓存
* @param cachePath 缓存路径,如果是M3U8或者HLS,请设置为false
* @param mapHeadData 头部信息
* @param title title
* @return
*/
public boolean setUp(String url, boolean cacheWithPlay, File cachePath, Map<String, String> mapHeadData, String title) {
if (setUp(url, cacheWithPlay, cachePath, title)) {
if (this.mMapHeadData != null) {
this.mMapHeadData.clear();
} else {
this.mMapHeadData = new HashMap<>();
}
if (mapHeadData != null) {
this.mMapHeadData.putAll(mapHeadData);
}
return true;
}
return false;
}
/**
* 设置播放URL
*
* @param url 播放url
* @param cacheWithPlay 是否边播边缓存
* @param cachePath 缓存路径,如果是M3U8或者HLS,请设置为false
* @param title title
* @return
*/
public boolean setUp(String url, boolean cacheWithPlay, File cachePath, String title) {
return setUp(url, cacheWithPlay, cachePath, title, true);
}
/**
* 设置播放URL
*
* @param url 播放url
* @param cacheWithPlay 是否边播边缓存
* @param cachePath 缓存路径,如果是M3U8或者HLS,请设置为false
* @param title title
* @param changeState 是否修改状态
* @return
*/
protected boolean setUp(String url, boolean cacheWithPlay, File cachePath, String title, boolean changeState) {
mCache = cacheWithPlay;
mCachePath = cachePath;
mOriginUrl = url;
if (isCurrentMediaListener() &&
(System.currentTimeMillis() - mSaveChangeViewTIme) < CHANGE_DELAY_TIME)
return false;
mCurrentState = CURRENT_STATE_NORMAL;
this.mUrl = url;
this.mTitle = title;
if (changeState)
setStateAndUi(CURRENT_STATE_NORMAL);
return true;
}
/**
* 重置
*/
public void onVideoReset() {
setStateAndUi(CURRENT_STATE_NORMAL);
}
/**
* 暂停状态
*/
@Override
public void onVideoPause() {
if (mCurrentState == CURRENT_STATE_PREPAREING) {
mPauseBeforePrepared = true;
}
try {
if (getGSYVideoManager() != null &&
getGSYVideoManager().isPlaying()) {
setStateAndUi(CURRENT_STATE_PAUSE);
mCurrentPosition = getGSYVideoManager().getCurrentPosition();
if (getGSYVideoManager() != null)
getGSYVideoManager().pause();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 恢复暂停状态
*/
@Override
public void onVideoResume() {
onVideoResume(true);
}
/**
* 恢复暂停状态
*
* @param seek 是否产生seek动作
*/
@Override
public void onVideoResume(boolean seek) {
mPauseBeforePrepared = false;
if (mCurrentState == CURRENT_STATE_PAUSE) {
try {
if (mCurrentPosition >= 0 && getGSYVideoManager() != null) {
if (seek) {
getGSYVideoManager().seekTo(mCurrentPosition);
}
getGSYVideoManager().start();
setStateAndUi(CURRENT_STATE_PLAYING);
if (mAudioManager != null && !mReleaseWhenLossAudio) {
mAudioManager.requestAudioFocus(onAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
mCurrentPosition = 0;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 处理因切换网络而导致的问题
*/
protected void netWorkErrorLogic() {
final long currentPosition = getCurrentPositionWhenPlaying();
Debuger.printfError("******* Net State Changed. renew player to connect *******" + currentPosition);
getGSYVideoManager().releaseMediaPlayer();
postDelayed(new Runnable() {
@Override
public void run() {
setSeekOnStart(currentPosition);
startPlayLogic();
}
}, 500);
}
/**
* 播放错误的时候,删除缓存文件
*/
protected void deleteCacheFileWhenError() {
clearCurrentCache();
Debuger.printfError("Link Or mCache Error, Please Try Again " + mOriginUrl);
if (mCache) {
Debuger.printfError("mCache Link " + mUrl);
}
mUrl = mOriginUrl;
}
@Override
public void onPrepared() {
if (mCurrentState != CURRENT_STATE_PREPAREING) return;
mHadPrepared = true;
if (mVideoAllCallBack != null && isCurrentMediaListener()) {
Debuger.printfLog("onPrepared");
mVideoAllCallBack.onPrepared(mOriginUrl, mTitle, this);
}
if (!mStartAfterPrepared) {
setStateAndUi(CURRENT_STATE_PAUSE);
onVideoPause();//todo 加上这个
return;
}
startAfterPrepared();
}
@Override
public void onAutoCompletion() {
setStateAndUi(CURRENT_STATE_AUTO_COMPLETE);
mSaveChangeViewTIme = 0;
mCurrentPosition = 0;
if (mTextureViewContainer.getChildCount() > 0) {
mTextureViewContainer.removeAllViews();
}
if (!mIfCurrentIsFullscreen)
getGSYVideoManager().setLastListener(null);
mAudioManager.abandonAudioFocus(onAudioFocusChangeListener);
if (mContext instanceof Activity) {
try {
((Activity) mContext).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} catch (Exception e) {
e.printStackTrace();
}
}
releaseNetWorkState();
if (mVideoAllCallBack != null && isCurrentMediaListener()) {
Debuger.printfLog("onAutoComplete");
mVideoAllCallBack.onAutoComplete(mOriginUrl, mTitle, this);
}
mHadPlay = false;
}
@Override
public void onCompletion() {
//make me normal first
setStateAndUi(CURRENT_STATE_NORMAL);
mSaveChangeViewTIme = 0;
mCurrentPosition = 0;
if (mTextureViewContainer.getChildCount() > 0) {
mTextureViewContainer.removeAllViews();
}
if (!mIfCurrentIsFullscreen) {
getGSYVideoManager().setListener(null);
getGSYVideoManager().setLastListener(null);
}
getGSYVideoManager().setCurrentVideoHeight(0);
getGSYVideoManager().setCurrentVideoWidth(0);
mAudioManager.abandonAudioFocus(onAudioFocusChangeListener);
if (mContext instanceof Activity) {
try {
((Activity) mContext).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} catch (Exception e) {
e.printStackTrace();
}
}
releaseNetWorkState();
if (mVideoAllCallBack != null) {
Debuger.printfLog("onComplete");
mVideoAllCallBack.onComplete(mOriginUrl, mTitle, this);
}
mHadPlay = false;
}
@Override
public void onSeekComplete() {
Debuger.printfLog("onSeekComplete");
}
@Override
public void onError(int what, int extra) {
if (mNetChanged) {
mNetChanged = false;
netWorkErrorLogic();
if (mVideoAllCallBack != null) {
mVideoAllCallBack.onPlayError(mOriginUrl, mTitle, this);
}
return;
}
if (what != 38 && what != -38) {
setStateAndUi(CURRENT_STATE_ERROR);
deleteCacheFileWhenError();
if (mVideoAllCallBack != null) {
mVideoAllCallBack.onPlayError(mOriginUrl, mTitle, this, what, extra);
}
}
}
@Override
public void onInfo(int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
mBackUpPlayingBufferState = mCurrentState;
//避免在onPrepared之前就进入了buffering,导致一只loading
if (mHadPlay && mCurrentState != CURRENT_STATE_PREPAREING && mCurrentState > 0)
setStateAndUi(CURRENT_STATE_PLAYING_BUFFERING_START);
} else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
if (mBackUpPlayingBufferState != -1) {
if (mBackUpPlayingBufferState == CURRENT_STATE_PLAYING_BUFFERING_START) {
mBackUpPlayingBufferState = CURRENT_STATE_PLAYING;
}
if (mHadPlay && mCurrentState != CURRENT_STATE_PREPAREING && mCurrentState > 0)
setStateAndUi(mBackUpPlayingBufferState);
mBackUpPlayingBufferState = -1;
}
} else if (what == getGSYVideoManager().getRotateInfoFlag()) {
mRotate = extra;
Debuger.printfLog("Video Rotate Info " + extra);
if (mTextureView != null)
mTextureView.setRotation(mRotate);
}
}
@Override
public void onVideoSizeChanged() {
int mVideoWidth = getGSYVideoManager().getCurrentVideoWidth();
int mVideoHeight = getGSYVideoManager().getCurrentVideoHeight();
if (mVideoWidth != 0 && mVideoHeight != 0 && mTextureView != null) {
mTextureView.requestLayout();
}
}
@Override
protected void setDisplay(Surface surface) {
getGSYVideoManager().setDisplay(surface);
}
@Override
protected void releaseSurface(Surface surface) {
getGSYVideoManager().releaseSurface(surface);
}
/**
* 清除当前缓存
*/
public void clearCurrentCache() {
if (getGSYVideoManager().isCacheFile() && mCache) {
//是否为缓存文件
Debuger.printfError("Play Error " + mUrl);
mUrl = mOriginUrl;
getGSYVideoManager().clearCache(mContext, mCachePath, mOriginUrl);
} else if (mUrl != null && mUrl.contains("127.0.0.1")) {
getGSYVideoManager().clearCache(getContext(), mCachePath, mOriginUrl);
}
}
/**
* 获取当前播放进度
*/
public long getCurrentPositionWhenPlaying() {
long position = 0;
if (mCurrentState == CURRENT_STATE_PLAYING || mCurrentState == CURRENT_STATE_PAUSE) {
try {
position = getGSYVideoManager().getCurrentPosition();
} catch (Exception e) {
e.printStackTrace();
return position;
}
}
if (position == 0 && mCurrentPosition > 0) {
return mCurrentPosition;
}
return position;
}
/**
* 获取当前总时长
*/
public long getDuration() {
long duration = 0;
try {
duration = getGSYVideoManager().getDuration();
} catch (Exception e) {
e.printStackTrace();
return duration;
}
return duration;
}
/**
* 释放吧
*/
public void release() {
mSaveChangeViewTIme = 0;
if (isCurrentMediaListener() &&
(System.currentTimeMillis() - mSaveChangeViewTIme) > CHANGE_DELAY_TIME) {
releaseVideos();
}
}
/**
* prepared成功之后会开始播放
*/
public void startAfterPrepared() {
if (!mHadPrepared) {
prepareVideo();
}
try {
if (getGSYVideoManager() != null) {
getGSYVideoManager().start();
}
setStateAndUi(CURRENT_STATE_PLAYING);
if (getGSYVideoManager() != null && mSeekOnStart > 0) {
getGSYVideoManager().seekTo(mSeekOnStart);
mSeekOnStart = 0;
}
} catch (Exception e) {
e.printStackTrace();
}
addTextureView();
createNetWorkState();
listenerNetWorkState();
mHadPlay = true;
if (mTextureView != null) {
mTextureView.onResume();
}
if (mPauseBeforePrepared) {
onVideoPause();
mPauseBeforePrepared = false;
}
}
protected boolean isCurrentMediaListener() {
return getGSYVideoManager().listener() != null
&& getGSYVideoManager().listener() == this;
}
/**
* 创建网络监听
*/
protected void createNetWorkState() {
if (mNetInfoModule == null) {
mNetInfoModule = new NetInfoModule(mContext.getApplicationContext(), new NetInfoModule.NetChangeListener() {
@Override
public void changed(String state) {
if (!mNetSate.equals(state)) {
Debuger.printfError("******* change network state ******* " + state);
mNetChanged = true;
}
mNetSate = state;
}
});
mNetSate = mNetInfoModule.getCurrentConnectionType();
}
}
/**
* 监听网络状态
*/
protected void listenerNetWorkState() {
if (mNetInfoModule != null) {
mNetInfoModule.onHostResume();
}
}
/**
* 取消网络监听
*/
protected void unListenerNetWorkState() {
if (mNetInfoModule != null) {
mNetInfoModule.onHostPause();
}
}
/**
* 释放网络监听
*/
protected void releaseNetWorkState() {
if (mNetInfoModule != null) {
mNetInfoModule.onHostPause();
mNetInfoModule = null;
}
}
/************************* 需要继承处理部分 *************************/
/**
* 退出全屏
*
* @return 是否在全屏界面
*/
protected abstract boolean backFromFull(Context context);
/**
* 释放播放器
*/
protected abstract void releaseVideos();
/**
* 设置播放显示状态
*
* @param state
*/
protected abstract void setStateAndUi(int state);
/**
* 获取管理器桥接的实现
*/
public abstract GSYVideoViewBridge getGSYVideoManager();
/**
* 当前UI
*/
public abstract int getLayoutId();
/**
* 开始播放
*/
public abstract void startPlayLogic();
/************************* 公开接口 *************************/
/**
* 获取当前播放状态
*/
public int getCurrentState() {
return mCurrentState;
}
/**
* 根据状态判断是否播放中
*/
public boolean isInPlayingState() {
return (mCurrentState >= 0 && mCurrentState != CURRENT_STATE_NORMAL
&& mCurrentState != CURRENT_STATE_AUTO_COMPLETE && mCurrentState != CURRENT_STATE_ERROR);
}
/**
* 播放tag防止错误,因为普通的url也可能重复
*/
public String getPlayTag() {
return mPlayTag;
}
/**
* 播放tag防止错误,因为普通的url也可能重复
*
* @param playTag 保证不重复就好
*/
public void setPlayTag(String playTag) {
this.mPlayTag = playTag;
}
public int getPlayPosition() {
return mPlayPosition;
}
/**
* 设置播放位置防止错位
*/
public void setPlayPosition(int playPosition) {
this.mPlayPosition = playPosition;
}
/**
* 网络速度
* 注意,这里如果是开启了缓存,因为读取本地代理,缓存成功后还是存在速度的
* 再打开已经缓存的本地文件,网络速度才会回0.因为是播放本地文件了
*/
public long getNetSpeed() {
return getGSYVideoManager().getNetSpeed();
}
/**
* 网络速度
* 注意,这里如果是开启了缓存,因为读取本地代理,缓存成功后还是存在速度的
* 再打开已经缓存的本地文件,网络速度才会回0.因为是播放本地文件了
*/
public String getNetSpeedText() {
long speed = getNetSpeed();
return getTextSpeed(speed);
}
public long getSeekOnStart() {
return mSeekOnStart;