DuduPlayer.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. package com.haochuan.dudutoy;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.graphics.drawable.AnimationDrawable;
  6. import android.os.Handler;
  7. import android.os.Looper;
  8. import android.util.AttributeSet;
  9. import android.util.Log;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.SeekBar;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. import com.shuyu.gsyvideoplayer.GSYVideoManager;
  16. import com.shuyu.gsyvideoplayer.model.VideoOptionModel;
  17. import com.shuyu.gsyvideoplayer.utils.Debuger;
  18. import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
  19. import com.shuyu.gsyvideoplayer.video.GSYADVideoPlayer;
  20. import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
  21. import java.util.ArrayList;
  22. import java.util.Calendar;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Timer;
  26. import java.util.TimerTask;
  27. import moe.codeest.enviews.ENDownloadView;
  28. import tv.danmaku.ijk.media.player.IjkMediaPlayer;
  29. /**
  30. * Created by Lyn on 2018/5/30.
  31. */
  32. public class DuduPlayer extends StandardGSYVideoPlayer {
  33. ImageView startBtn;
  34. ImageView loading;
  35. AnimationDrawable a;
  36. OnCompleteListener onCompleteListener;
  37. LinearLayout seekContainer;
  38. SeekBar seekBar;
  39. TextView durationText;
  40. TextView percentText;
  41. private long currentTime = 0;
  42. Context context;
  43. Timer timer ;
  44. TimerTask timerTask;
  45. private boolean isSeeked = false;
  46. private boolean isNoticed = false;
  47. String TAG = "DuduPlayer";
  48. public DuduPlayer(Context context, AttributeSet attrs) {
  49. super(context, attrs);
  50. }
  51. public void setOnCompleteListener(OnCompleteListener onCompleteListener){
  52. this.onCompleteListener = onCompleteListener;
  53. }
  54. @Override
  55. protected void init(Context context) {
  56. super.init(context);
  57. this.context = context;
  58. mDismissControlTime = 200;
  59. startBtn = findViewById(R.id.dudu_start_btn);
  60. loading = findViewById(R.id.loading_anim);
  61. seekContainer = findViewById(R.id.seek_container);
  62. durationText = findViewById(R.id.duration_text);
  63. percentText = findViewById(R.id.percent_text);
  64. seekBar = findViewById(R.id.seek_bar);
  65. seekBar.setMax(1000);
  66. seekBar.setProgress(0);
  67. seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  68. @Override
  69. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  70. int mProgress = 0;
  71. if (fromUser) {
  72. mProgress = progress;
  73. }
  74. percentText.setText(mProgress/10 + "%");
  75. if(progress<=0){
  76. seekBar.setProgress(10);
  77. percentText.setText("1%");
  78. }
  79. if(progress >= 1000){
  80. seekBar.setProgress(990);
  81. percentText.setText("99%");
  82. }
  83. }
  84. @Override
  85. public void onStartTrackingTouch(SeekBar seekBar) {
  86. }
  87. @Override
  88. public void onStopTrackingTouch(SeekBar seekBar) {
  89. }
  90. });
  91. //GSYVideoType.enableMediaCodec();
  92. //GSYVideoType.enableMediaCodecTexture();
  93. videoOptimize();
  94. initLoadingAnimation();
  95. initTimer();
  96. Debuger.disable();
  97. }
  98. private void videoOptimize(){
  99. List<VideoOptionModel> list = new ArrayList<>();
  100. VideoOptionModel videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "allowed_media_types", "video"); //根据媒体类型来配置
  101. list.add(videoOptionModel);
  102. videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "mediacodec", 1);
  103. list.add(videoOptionModel);
  104. videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "videotoolbox", 0);
  105. list.add(videoOptionModel);
  106. videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "timeout", 20000);
  107. list.add(videoOptionModel);
  108. videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "buffer_size", 1316);
  109. list.add(videoOptionModel);
  110. videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "infbuf", 1); // 无限读
  111. list.add(videoOptionModel);
  112. videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzemaxduration", 100);
  113. list.add(videoOptionModel);
  114. videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "flush_packets", 1);
  115. list.add(videoOptionModel);
  116. GSYVideoManager.instance().setOptionModelList(list);
  117. }
  118. private void initLoadingAnimation(){
  119. loading.setImageResource(R.drawable.loading_animation);
  120. AnimationDrawable a = (AnimationDrawable)loading.getDrawable();
  121. a.start();
  122. }
  123. @Override
  124. public void clickStartIcon(){
  125. super.clickStartIcon();
  126. }
  127. @Override
  128. public int getLayoutId() {
  129. return R.layout.du_du_player;
  130. }
  131. public void seek(int position){
  132. if(mCurrentState == CURRENT_STATE_PREPAREING || getDuration() == 0 || mCurrentState!= CURRENT_STATE_PLAYING){
  133. return;
  134. }
  135. seekContainer.setVisibility(VISIBLE);
  136. seekBar.setProgress(position);
  137. currentTime =Calendar.getInstance().getTimeInMillis();
  138. cancelTimer();
  139. initTimer();
  140. isSeeked = true;
  141. }
  142. private void initTimer(){
  143. if(timer == null){
  144. timer = new Timer();
  145. }
  146. timerTask = new TimerTask() {
  147. @Override
  148. public void run() {
  149. Activity activity = (Activity)context;
  150. activity.runOnUiThread(new Runnable() {
  151. @Override
  152. public void run() {
  153. long myCurrentTime = Calendar.getInstance().getTimeInMillis();
  154. if(myCurrentTime - currentTime > 3){
  155. int position = seekBar.getProgress();
  156. position = position* getDuration()/1000;
  157. seekTo(position);
  158. seekContainer.setVisibility(INVISIBLE);
  159. }
  160. }
  161. });
  162. }
  163. };
  164. timer.schedule(timerTask,3000);
  165. }
  166. public boolean canSeek(){
  167. if(mCurrentState == CURRENT_STATE_PREPAREING || getDuration() == 0){
  168. return false;
  169. }else{
  170. return true;
  171. }
  172. }
  173. private void cancelTimer(){
  174. if(timerTask != null){
  175. timerTask.cancel();
  176. }
  177. }
  178. @Override
  179. public void onStopTrackingTouch(SeekBar seekBar) {
  180. super.onStopTrackingTouch(seekBar);
  181. }
  182. @Override
  183. protected void showWifiDialog() {
  184. startPlayLogic();
  185. }
  186. @Override
  187. protected void changeUiToPauseShow(){
  188. Debuger.printfLog("changeUiToPauseShow");
  189. setViewShowState(mTopContainer, VISIBLE);
  190. setViewShowState(mBottomContainer, VISIBLE);
  191. setViewShowState(mStartButton, INVISIBLE);
  192. setViewShowState(mLoadingProgressBar, INVISIBLE);
  193. setViewShowState(mThumbImageViewLayout, INVISIBLE);
  194. setViewShowState(mBottomProgressBar, INVISIBLE);
  195. setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
  196. if (mLoadingProgressBar instanceof ENDownloadView) {
  197. ((ENDownloadView) mLoadingProgressBar).reset();
  198. }
  199. updateStartImage();
  200. updatePauseCover();
  201. //Log.d(TAG,"changeUiToPauseShow");
  202. }
  203. public String convertLongTimeToStr(long time) {
  204. int ss = 1000;
  205. int mi = ss * 60;
  206. int hh = mi * 60;
  207. long hour = (time) / hh;
  208. long minute = (time - hour * hh) / mi;
  209. long second = (time - hour * hh - minute * mi) / ss;
  210. String strHour = hour < 10 ? "0" + hour : "" + hour;
  211. String strMinute = minute < 10 ? "0" + minute : "" + minute;
  212. String strSecond = second < 10 ? "0" + second : "" + second;
  213. if (hour > 0) {
  214. return strHour + ":" + strMinute + ":" + strSecond;
  215. } else {
  216. return strMinute + ":" + strSecond;
  217. }
  218. }
  219. @Override
  220. protected void changeUiToPlayingShow() {
  221. Debuger.printfLog("changeUiToPlayingShow");
  222. isBuffering = false;
  223. setViewShowState(mTopContainer, INVISIBLE);
  224. setViewShowState(mBottomContainer, INVISIBLE);
  225. setViewShowState(mStartButton, INVISIBLE);
  226. setViewShowState(mLoadingProgressBar, INVISIBLE);
  227. setViewShowState(mThumbImageViewLayout, INVISIBLE);
  228. setViewShowState(mBottomProgressBar, INVISIBLE);
  229. setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
  230. durationText.setText(convertLongTimeToStr(getDuration()));
  231. if (mLoadingProgressBar instanceof ENDownloadView) {
  232. ((ENDownloadView) mLoadingProgressBar).reset();
  233. }
  234. updateStartImage();
  235. //Log.d(TAG,"changeUiToPreparingShow");
  236. }
  237. @Override
  238. protected void changeUiToNormal() {
  239. Debuger.printfLog("changeUiToNormal");
  240. setViewShowState(mTopContainer, VISIBLE);
  241. setViewShowState(mBottomContainer, VISIBLE);
  242. setViewShowState(mStartButton, INVISIBLE);
  243. setViewShowState(mLoadingProgressBar, INVISIBLE);
  244. setViewShowState(mThumbImageViewLayout, VISIBLE);
  245. setViewShowState(mBottomProgressBar, INVISIBLE);
  246. setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
  247. updateStartImage();
  248. if (mLoadingProgressBar instanceof ENDownloadView) {
  249. ((ENDownloadView) mLoadingProgressBar).reset();
  250. }
  251. //Log.d(TAG,"changeUiToNormal");
  252. }
  253. @Override
  254. protected void changeUiToCompleteShow() {
  255. Debuger.printfLog("changeUiToCompleteShow");
  256. setViewShowState(mTopContainer, VISIBLE);
  257. setViewShowState(mBottomContainer, VISIBLE);
  258. setViewShowState(mStartButton, INVISIBLE);
  259. setViewShowState(mLoadingProgressBar, INVISIBLE);
  260. setViewShowState(mThumbImageViewLayout, VISIBLE);
  261. setViewShowState(mBottomProgressBar, INVISIBLE);
  262. setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
  263. this.onCompleteListener.onComplete();
  264. if (mLoadingProgressBar instanceof ENDownloadView) {
  265. ((ENDownloadView) mLoadingProgressBar).reset();
  266. }
  267. updateStartImage();
  268. //Log.d(TAG,"changeUiToCompleteShow");
  269. }
  270. private void Toast(final String msg){
  271. Activity activity = (Activity)context;
  272. activity.runOnUiThread(new Runnable() {
  273. @Override
  274. public void run() {
  275. Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
  276. }
  277. });
  278. }
  279. @Override
  280. public void onError(int what, int extra) {
  281. if(what == -10000){
  282. return;
  283. }
  284. super.onError(what,extra);
  285. Log.d(TAG,"what:" + what + "extra:" + extra);
  286. }
  287. @Override
  288. protected void changeUiToError() {
  289. Debuger.printfLog("changeUiToError");
  290. setViewShowState(mTopContainer, VISIBLE);
  291. setViewShowState(mBottomContainer, VISIBLE);
  292. setViewShowState(mStartButton, INVISIBLE);
  293. setViewShowState(mLoadingProgressBar, INVISIBLE);
  294. setViewShowState(mThumbImageViewLayout, INVISIBLE);
  295. setViewShowState(mBottomProgressBar, INVISIBLE);
  296. setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
  297. if (mLoadingProgressBar instanceof ENDownloadView) {
  298. ((ENDownloadView) mLoadingProgressBar).reset();
  299. }
  300. updateStartImage();
  301. //Log.d(TAG,"changeUiToError");
  302. Toast("播放器发生错误,请退出播放!");
  303. onCompleteListener.onComplete();
  304. }
  305. long lastBufferingTime = 0;
  306. boolean isBuffering = false;
  307. @Override
  308. protected void changeUiToPlayingBufferingShow() {
  309. Date date = new Date();
  310. long currentBufferingTime = date.getTime();
  311. Debuger.printfLog("changeUiToPlayingBufferingShow,lastBufferingTime" + currentBufferingTime);
  312. if(currentBufferingTime - lastBufferingTime > 1500){
  313. setViewShowState(mTopContainer, INVISIBLE);
  314. setViewShowState(mBottomContainer, INVISIBLE);
  315. setViewShowState(mStartButton, INVISIBLE);
  316. setViewShowState(mLoadingProgressBar, VISIBLE);
  317. setViewShowState(mThumbImageViewLayout, INVISIBLE);
  318. setViewShowState(mBottomProgressBar, INVISIBLE);
  319. setViewShowState(mLockScreen, GONE);
  320. if (mLoadingProgressBar instanceof ENDownloadView) {
  321. ENDownloadView enDownloadView = (ENDownloadView) mLoadingProgressBar;
  322. if (enDownloadView.getCurrentState() == ENDownloadView.STATE_PRE) {
  323. ((ENDownloadView) mLoadingProgressBar).start();
  324. }
  325. }
  326. }
  327. isSeeked = false;
  328. lastBufferingTime =currentBufferingTime;
  329. isBuffering = true;
  330. if(!isNoticed){
  331. Toast("提示!视频在缓冲过程中,请不要做其他任何操作,以免发生异常!");
  332. isNoticed = true;
  333. }
  334. // Log.d(TAG,"changeUiToPlayingBufferingShow,");
  335. }
  336. @Override
  337. protected void changeUiToPreparingShow() {
  338. super.changeUiToPreparingShow();
  339. // Log.d(TAG,"changeUiToPreparingShow");
  340. }
  341. public void playOrPause(){
  342. if (mCurrentState == CURRENT_STATE_PLAYING) {
  343. try {
  344. getGSYVideoManager().pause();
  345. setStateAndUi(CURRENT_STATE_PAUSE);
  346. startBtn.setImageResource(R.drawable.btn_icon_right);
  347. } catch (Exception e) {
  348. e.printStackTrace();
  349. }
  350. }else if (mCurrentState == CURRENT_STATE_PAUSE){
  351. try {
  352. getGSYVideoManager().start();
  353. setStateAndUi(CURRENT_STATE_PLAYING);
  354. startBtn.setImageResource(R.drawable.btn_icon_stop);
  355. } catch (Exception e) {
  356. e.printStackTrace();
  357. }
  358. }
  359. }
  360. public void play(){
  361. if (mCurrentState == CURRENT_STATE_PAUSE){
  362. try {
  363. getGSYVideoManager().start();
  364. setStateAndUi(CURRENT_STATE_PLAYING);
  365. startBtn.setImageResource(R.drawable.btn_icon_stop);
  366. } catch (Exception e) {
  367. e.printStackTrace();
  368. }
  369. }
  370. }
  371. public void pause(){
  372. if (mCurrentState == CURRENT_STATE_PLAYING || mCurrentState == CURRENT_STATE_PLAYING_BUFFERING_START) {
  373. try {
  374. getGSYVideoManager().pause();
  375. setStateAndUi(CURRENT_STATE_PAUSE);
  376. startBtn.setImageResource(R.drawable.btn_icon_right);
  377. } catch (Exception e) {
  378. e.printStackTrace();
  379. }
  380. }
  381. }
  382. public void onDestroy(){
  383. startBtn = null;
  384. loading = null;
  385. if(a != null){
  386. a.stop();
  387. }
  388. a = null;
  389. onCompleteListener = null;
  390. seekContainer = null;
  391. seekBar = null;
  392. durationText = null;
  393. percentText= null;
  394. context = null;
  395. if(timer !=null){
  396. timer.cancel();
  397. }
  398. if(timerTask !=null){
  399. timerTask.cancel();
  400. }
  401. timer = null;
  402. timerTask = null;
  403. }
  404. public boolean isBuffering(){
  405. return isBuffering;
  406. }
  407. public interface OnCompleteListener{
  408. public void onComplete();
  409. }
  410. }