123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- package com.haochuan.dudutoy;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Handler;
- import android.os.Looper;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.SeekBar;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.shuyu.gsyvideoplayer.GSYVideoManager;
- import com.shuyu.gsyvideoplayer.model.VideoOptionModel;
- import com.shuyu.gsyvideoplayer.utils.Debuger;
- import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
- import com.shuyu.gsyvideoplayer.video.GSYADVideoPlayer;
- import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import java.util.Timer;
- import java.util.TimerTask;
- import moe.codeest.enviews.ENDownloadView;
- import tv.danmaku.ijk.media.player.IjkMediaPlayer;
- /**
- * Created by Lyn on 2018/5/30.
- */
- public class DuduPlayer extends StandardGSYVideoPlayer {
- ImageView startBtn;
- ImageView loading;
- AnimationDrawable a;
- OnCompleteListener onCompleteListener;
- LinearLayout seekContainer;
- SeekBar seekBar;
- TextView durationText;
- TextView percentText;
- private long currentTime = 0;
- Context context;
- Timer timer ;
- TimerTask timerTask;
- private boolean isSeeked = false;
- private boolean isNoticed = false;
- String TAG = "DuduPlayer";
- public DuduPlayer(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public void setOnCompleteListener(OnCompleteListener onCompleteListener){
- this.onCompleteListener = onCompleteListener;
- }
- @Override
- protected void init(Context context) {
- super.init(context);
- this.context = context;
- mDismissControlTime = 200;
- startBtn = findViewById(R.id.dudu_start_btn);
- loading = findViewById(R.id.loading_anim);
- seekContainer = findViewById(R.id.seek_container);
- durationText = findViewById(R.id.duration_text);
- percentText = findViewById(R.id.percent_text);
- seekBar = findViewById(R.id.seek_bar);
- seekBar.setMax(1000);
- seekBar.setProgress(0);
- seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
- int mProgress = 0;
- if (fromUser) {
- mProgress = progress;
- }
- percentText.setText(mProgress/10 + "%");
- if(progress<=0){
- seekBar.setProgress(10);
- percentText.setText("1%");
- }
- if(progress >= 1000){
- seekBar.setProgress(990);
- percentText.setText("99%");
- }
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- }
- });
- //GSYVideoType.enableMediaCodec();
- //GSYVideoType.enableMediaCodecTexture();
- videoOptimize();
- initLoadingAnimation();
- initTimer();
- Debuger.disable();
- }
- private void videoOptimize(){
- List<VideoOptionModel> list = new ArrayList<>();
- VideoOptionModel videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "allowed_media_types", "video"); //根据媒体类型来配置
- list.add(videoOptionModel);
- videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "mediacodec", 1);
- list.add(videoOptionModel);
- videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "videotoolbox", 0);
- list.add(videoOptionModel);
- videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "timeout", 20000);
- list.add(videoOptionModel);
- videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "buffer_size", 1316);
- list.add(videoOptionModel);
- videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "infbuf", 1); // 无限读
- list.add(videoOptionModel);
- videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzemaxduration", 100);
- list.add(videoOptionModel);
- videoOptionModel = new VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "flush_packets", 1);
- list.add(videoOptionModel);
- GSYVideoManager.instance().setOptionModelList(list);
- }
- private void initLoadingAnimation(){
- loading.setImageResource(R.drawable.loading_animation);
- AnimationDrawable a = (AnimationDrawable)loading.getDrawable();
- a.start();
- }
- @Override
- public void clickStartIcon(){
- super.clickStartIcon();
- }
- @Override
- public int getLayoutId() {
- return R.layout.du_du_player;
- }
- public void seek(int position){
- if(mCurrentState == CURRENT_STATE_PREPAREING || getDuration() == 0 || mCurrentState!= CURRENT_STATE_PLAYING){
- return;
- }
- seekContainer.setVisibility(VISIBLE);
- seekBar.setProgress(position);
- currentTime =Calendar.getInstance().getTimeInMillis();
- cancelTimer();
- initTimer();
- isSeeked = true;
- }
- private void initTimer(){
- if(timer == null){
- timer = new Timer();
- }
- timerTask = new TimerTask() {
- @Override
- public void run() {
- Activity activity = (Activity)context;
- activity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- long myCurrentTime = Calendar.getInstance().getTimeInMillis();
- if(myCurrentTime - currentTime > 3){
- int position = seekBar.getProgress();
- position = position* getDuration()/1000;
- seekTo(position);
- seekContainer.setVisibility(INVISIBLE);
- }
- }
- });
- }
- };
- timer.schedule(timerTask,3000);
- }
- public boolean canSeek(){
- if(mCurrentState == CURRENT_STATE_PREPAREING || getDuration() == 0){
- return false;
- }else{
- return true;
- }
- }
- private void cancelTimer(){
- if(timerTask != null){
- timerTask.cancel();
- }
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- super.onStopTrackingTouch(seekBar);
- }
- @Override
- protected void showWifiDialog() {
- startPlayLogic();
- }
- @Override
- protected void changeUiToPauseShow(){
- Debuger.printfLog("changeUiToPauseShow");
- setViewShowState(mTopContainer, VISIBLE);
- setViewShowState(mBottomContainer, VISIBLE);
- setViewShowState(mStartButton, INVISIBLE);
- setViewShowState(mLoadingProgressBar, INVISIBLE);
- setViewShowState(mThumbImageViewLayout, INVISIBLE);
- setViewShowState(mBottomProgressBar, INVISIBLE);
- setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
- if (mLoadingProgressBar instanceof ENDownloadView) {
- ((ENDownloadView) mLoadingProgressBar).reset();
- }
- updateStartImage();
- updatePauseCover();
- //Log.d(TAG,"changeUiToPauseShow");
- }
- public String convertLongTimeToStr(long time) {
- int ss = 1000;
- int mi = ss * 60;
- int hh = mi * 60;
- long hour = (time) / hh;
- long minute = (time - hour * hh) / mi;
- long second = (time - hour * hh - minute * mi) / ss;
- String strHour = hour < 10 ? "0" + hour : "" + hour;
- String strMinute = minute < 10 ? "0" + minute : "" + minute;
- String strSecond = second < 10 ? "0" + second : "" + second;
- if (hour > 0) {
- return strHour + ":" + strMinute + ":" + strSecond;
- } else {
- return strMinute + ":" + strSecond;
- }
- }
- @Override
- protected void changeUiToPlayingShow() {
- Debuger.printfLog("changeUiToPlayingShow");
- isBuffering = false;
- setViewShowState(mTopContainer, INVISIBLE);
- setViewShowState(mBottomContainer, INVISIBLE);
- setViewShowState(mStartButton, INVISIBLE);
- setViewShowState(mLoadingProgressBar, INVISIBLE);
- setViewShowState(mThumbImageViewLayout, INVISIBLE);
- setViewShowState(mBottomProgressBar, INVISIBLE);
- setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
- durationText.setText(convertLongTimeToStr(getDuration()));
- if (mLoadingProgressBar instanceof ENDownloadView) {
- ((ENDownloadView) mLoadingProgressBar).reset();
- }
- updateStartImage();
- //Log.d(TAG,"changeUiToPreparingShow");
- }
- @Override
- protected void changeUiToNormal() {
- Debuger.printfLog("changeUiToNormal");
- setViewShowState(mTopContainer, VISIBLE);
- setViewShowState(mBottomContainer, VISIBLE);
- setViewShowState(mStartButton, INVISIBLE);
- setViewShowState(mLoadingProgressBar, INVISIBLE);
- setViewShowState(mThumbImageViewLayout, VISIBLE);
- setViewShowState(mBottomProgressBar, INVISIBLE);
- setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
- updateStartImage();
- if (mLoadingProgressBar instanceof ENDownloadView) {
- ((ENDownloadView) mLoadingProgressBar).reset();
- }
- //Log.d(TAG,"changeUiToNormal");
- }
- @Override
- protected void changeUiToCompleteShow() {
- Debuger.printfLog("changeUiToCompleteShow");
- setViewShowState(mTopContainer, VISIBLE);
- setViewShowState(mBottomContainer, VISIBLE);
- setViewShowState(mStartButton, INVISIBLE);
- setViewShowState(mLoadingProgressBar, INVISIBLE);
- setViewShowState(mThumbImageViewLayout, VISIBLE);
- setViewShowState(mBottomProgressBar, INVISIBLE);
- setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
- this.onCompleteListener.onComplete();
- if (mLoadingProgressBar instanceof ENDownloadView) {
- ((ENDownloadView) mLoadingProgressBar).reset();
- }
- updateStartImage();
- //Log.d(TAG,"changeUiToCompleteShow");
- }
- private void Toast(final String msg){
- Activity activity = (Activity)context;
- activity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
- }
- });
- }
- @Override
- public void onError(int what, int extra) {
- if(what == -10000){
- return;
- }
- super.onError(what,extra);
- Log.d(TAG,"what:" + what + "extra:" + extra);
- }
- @Override
- protected void changeUiToError() {
- Debuger.printfLog("changeUiToError");
- setViewShowState(mTopContainer, VISIBLE);
- setViewShowState(mBottomContainer, VISIBLE);
- setViewShowState(mStartButton, INVISIBLE);
- setViewShowState(mLoadingProgressBar, INVISIBLE);
- setViewShowState(mThumbImageViewLayout, INVISIBLE);
- setViewShowState(mBottomProgressBar, INVISIBLE);
- setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
- if (mLoadingProgressBar instanceof ENDownloadView) {
- ((ENDownloadView) mLoadingProgressBar).reset();
- }
- updateStartImage();
- //Log.d(TAG,"changeUiToError");
- Toast("播放器发生错误,请退出播放!");
- onCompleteListener.onComplete();
- }
- long lastBufferingTime = 0;
- boolean isBuffering = false;
- @Override
- protected void changeUiToPlayingBufferingShow() {
- Date date = new Date();
- long currentBufferingTime = date.getTime();
- Debuger.printfLog("changeUiToPlayingBufferingShow,lastBufferingTime" + currentBufferingTime);
- if(currentBufferingTime - lastBufferingTime > 1500){
- setViewShowState(mTopContainer, INVISIBLE);
- setViewShowState(mBottomContainer, INVISIBLE);
- setViewShowState(mStartButton, INVISIBLE);
- setViewShowState(mLoadingProgressBar, VISIBLE);
- setViewShowState(mThumbImageViewLayout, INVISIBLE);
- setViewShowState(mBottomProgressBar, INVISIBLE);
- setViewShowState(mLockScreen, GONE);
- if (mLoadingProgressBar instanceof ENDownloadView) {
- ENDownloadView enDownloadView = (ENDownloadView) mLoadingProgressBar;
- if (enDownloadView.getCurrentState() == ENDownloadView.STATE_PRE) {
- ((ENDownloadView) mLoadingProgressBar).start();
- }
- }
- }
- isSeeked = false;
- lastBufferingTime =currentBufferingTime;
- isBuffering = true;
- if(!isNoticed){
- Toast("提示!视频在缓冲过程中,请不要做其他任何操作,以免发生异常!");
- isNoticed = true;
- }
- // Log.d(TAG,"changeUiToPlayingBufferingShow,");
- }
- @Override
- protected void changeUiToPreparingShow() {
- super.changeUiToPreparingShow();
- // Log.d(TAG,"changeUiToPreparingShow");
- }
- public void playOrPause(){
- if (mCurrentState == CURRENT_STATE_PLAYING) {
- try {
- getGSYVideoManager().pause();
- setStateAndUi(CURRENT_STATE_PAUSE);
- startBtn.setImageResource(R.drawable.btn_icon_right);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }else if (mCurrentState == CURRENT_STATE_PAUSE){
- try {
- getGSYVideoManager().start();
- setStateAndUi(CURRENT_STATE_PLAYING);
- startBtn.setImageResource(R.drawable.btn_icon_stop);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public void play(){
- if (mCurrentState == CURRENT_STATE_PAUSE){
- try {
- getGSYVideoManager().start();
- setStateAndUi(CURRENT_STATE_PLAYING);
- startBtn.setImageResource(R.drawable.btn_icon_stop);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public void pause(){
- if (mCurrentState == CURRENT_STATE_PLAYING || mCurrentState == CURRENT_STATE_PLAYING_BUFFERING_START) {
- try {
- getGSYVideoManager().pause();
- setStateAndUi(CURRENT_STATE_PAUSE);
- startBtn.setImageResource(R.drawable.btn_icon_right);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public void onDestroy(){
- startBtn = null;
- loading = null;
- if(a != null){
- a.stop();
- }
- a = null;
- onCompleteListener = null;
- seekContainer = null;
- seekBar = null;
- durationText = null;
- percentText= null;
- context = null;
- if(timer !=null){
- timer.cancel();
- }
- if(timerTask !=null){
- timerTask.cancel();
- }
- timer = null;
- timerTask = null;
- }
- public boolean isBuffering(){
- return isBuffering;
- }
- public interface OnCompleteListener{
- public void onComplete();
- }
- }
|