GSYVideoPlayer.java 43.3 KB
Newer Older
S
shuyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package com.shuyu.gsyvideoplayer;

import android.app.Activity;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
S
shuyu 已提交
17
import android.view.Window;
S
shuyu 已提交
18 19 20 21 22 23 24 25
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.danikula.videocache.HttpProxyCacheServer;
S
shuyu 已提交
26
import com.danikula.videocache.file.Md5FileNameGenerator;
S
shuyu 已提交
27
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
S
1.1.9  
shuyu 已提交
28
import com.shuyu.gsyvideoplayer.utils.Debuger;
S
shuyu 已提交
29
import com.shuyu.gsyvideoplayer.utils.StorageUtils;
S
1.1.3  
shuyu 已提交
30
import com.shuyu.gsyvideoplayer.video.GSYBaseVideoPlayer;
S
shuyu 已提交
31

S
1.3.0  
shuyu 已提交
32
import java.io.File;
S
shuyu 已提交
33 34 35 36 37
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import tv.danmaku.ijk.media.player.IMediaPlayer;
38
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
S
shuyu 已提交
39

S
1.2.9  
shuyu 已提交
40
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.getTextSpeed;
S
shuyu 已提交
41
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.hideNavKey;
S
1.2.9  
shuyu 已提交
42

S
shuyu 已提交
43

S
shuyu 已提交
44 45 46 47
/**
 * Created by shuyu on 2016/11/11.
 */

S
1.1.3  
shuyu 已提交
48
public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View.OnClickListener, View.OnTouchListener, SeekBar.OnSeekBarChangeListener, TextureView.SurfaceTextureListener {
S
shuyu 已提交
49 50 51

    public static final String TAG = "GSYVideoPlayer";

S
shuyu 已提交
52

S
shuyu 已提交
53 54 55 56 57 58 59
    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; //错误状态
S
shuyu 已提交
60

S
shuyu 已提交
61
    public static final int FULL_SCREEN_NORMAL_DELAY = 2000;
S
shuyu 已提交
62

S
shuyu 已提交
63
    protected static int BACKUP_PLAYING_BUFFERING_STATE = -1;
S
shuyu 已提交
64

S
shuyu 已提交
65
    protected static boolean IF_FULLSCREEN_FROM_NORMAL = false;
S
shuyu 已提交
66

S
shuyu 已提交
67
    public static boolean IF_RELEASE_WHEN_ON_PAUSE = true;
S
shuyu 已提交
68

S
1.3.3  
shuyu 已提交
69
    protected Timer UPDATE_PROGRESS_TIMER;
S
shuyu 已提交
70

S
shuyu 已提交
71
    protected Surface mSurface;
72

S
shuyu 已提交
73
    protected ProgressTimerTask mProgressTimerTask;
S
1.3.0  
shuyu 已提交
74

S
shuyu 已提交
75
    protected AudioManager mAudioManager; //音频焦点的监听
S
shuyu 已提交
76 77 78

    protected Handler mHandler = new Handler();

79 80 81 82
    protected String mPlayTag = ""; //播放的tag,防止错误,因为普通的url也可能重复

    protected int mPlayPosition = -22; //播放的tag,防止错误,因为普通的url也可能重复

S
shuyu 已提交
83 84 85 86
    protected float mDownX;//触摸的X

    protected float mDownY; //触摸的Y

S
shuyu 已提交
87 88
    protected float mMoveY;

S
shuyu 已提交
89 90 91 92 93 94 95 96 97 98 99 100
    protected float mBrightnessData = -1; //亮度

    protected int mDownPosition; //手指放下的位置

    protected int mGestureDownVolume; //手势调节音量的大小

    protected int mScreenWidth; //屏幕宽度

    protected int mScreenHeight; //屏幕高度

    protected int mThreshold = 80; //手势偏差值

S
1.1.9  
shuyu 已提交
101
    protected int mSeekToInAdvance = -1; //// TODO: 2016/11/13 跳过广告
S
shuyu 已提交
102

S
readme  
shuyu 已提交
103
    protected int mBuffterPoint;//缓存进度
S
1.2.9  
shuyu 已提交
104

S
shuyu 已提交
105 106
    protected int mSeekTimePosition; //手动改变滑动的位置

107 108
    protected int mSeekEndOffset; //手动滑动的起始偏移位置

S
1.4.4  
shuyu 已提交
109 110
    protected long mSeekOnStart = -1; //从哪个开始播放

S
shuyu 已提交
111 112 113
    protected long mPauseTime; //保存暂停时的时间

    protected long mCurrentPosition; //当前的播放位置
S
shuyu 已提交
114

S
shuyu 已提交
115 116 117 118 119 120 121
    protected boolean mTouchingProgressBar = false;

    protected boolean mIsTouchWiget = false;

    protected boolean mChangeVolume = false;//是否改变音量

    protected boolean mChangePosition = false;//是否改变播放进度
S
shuyu 已提交
122

S
shuyu 已提交
123 124
    protected boolean mShowVKey = false; //触摸显示虚拟按键

S
shuyu 已提交
125
    protected boolean mBrightness = false;//是否改变亮度
S
shuyu 已提交
126

S
shuyu 已提交
127
    protected boolean mFirstTouch = false;//是否首次触摸
S
shuyu 已提交
128

S
shuyu 已提交
129 130 131
    /**
     * 当前UI
     */
S
shuyu 已提交
132
    public abstract int getLayoutId();
S
shuyu 已提交
133

S
shuyu 已提交
134 135 136
    /**
     * 开始播放
     */
S
shuyu 已提交
137 138
    public abstract void startPlayLogic();

S
shuyu 已提交
139
    /**
140
     * 1.5.0开始加入,如果需要不同布局区分功能,需要重载
S
shuyu 已提交
141 142 143 144 145 146
     */
    public GSYVideoPlayer(Context context, Boolean fullFlag) {
        super(context, fullFlag);
        init(context);
    }

S
shuyu 已提交
147 148 149 150 151 152 153 154 155 156 157
    public GSYVideoPlayer(Context context) {
        super(context);
        init(context);
    }

    public GSYVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    protected void init(Context context) {
S
shuyu 已提交
158
        this.mContext = context;
S
shuyu 已提交
159
        View.inflate(context, getLayoutId(), this);
S
shuyu 已提交
160
        mStartButton = findViewById(R.id.start);
S
1.1.7  
shuyu 已提交
161
        mSmallClose = findViewById(R.id.small_close);
S
shuyu 已提交
162
        mBackButton = (ImageView) findViewById(R.id.back);
163
        mCoverImageView = (ImageView) findViewById(R.id.cover);
S
shuyu 已提交
164 165 166 167 168 169 170
        mFullscreenButton = (ImageView) findViewById(R.id.fullscreen);
        mProgressBar = (SeekBar) findViewById(R.id.progress);
        mCurrentTimeTextView = (TextView) findViewById(R.id.current);
        mTotalTimeTextView = (TextView) findViewById(R.id.total);
        mBottomContainer = (ViewGroup) findViewById(R.id.layout_bottom);
        mTextureViewContainer = (RelativeLayout) findViewById(R.id.surface_container);
        mTopContainer = (ViewGroup) findViewById(R.id.layout_top);
S
shuyu 已提交
171 172
        if (isInEditMode())
            return;
S
shuyu 已提交
173 174 175 176 177 178 179 180 181
        mStartButton.setOnClickListener(this);
        mFullscreenButton.setOnClickListener(this);
        mProgressBar.setOnSeekBarChangeListener(this);
        mBottomContainer.setOnClickListener(this);
        mTextureViewContainer.setOnClickListener(this);
        mProgressBar.setOnTouchListener(this);

        mTextureViewContainer.setOnTouchListener(this);
        mFullscreenButton.setOnTouchListener(this);
S
shuyu 已提交
182 183
        mScreenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
        mScreenHeight = getContext().getResources().getDisplayMetrics().heightPixels;
S
1.3.3  
shuyu 已提交
184
        mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
185 186

        mSeekEndOffset = CommonUtil.dip2px(getContext(), 50);
S
shuyu 已提交
187 188
    }

S
shuyu 已提交
189 190 191
    /**
     * 设置播放URL
     *
S
false  
shuyu 已提交
192
     * @param url           播放url
S
shuyu 已提交
193
     * @param cacheWithPlay 是否边播边缓存
S
false  
shuyu 已提交
194
     * @param objects       object[0]目前为title
S
shuyu 已提交
195 196
     * @return
     */
S
shuyu 已提交
197
    public boolean setUp(String url, boolean cacheWithPlay, Object... objects) {
S
1.3.0  
shuyu 已提交
198
        return setUp(url, cacheWithPlay, ((File) null), objects);
S
shuyu 已提交
199 200
    }

S
1.3.0  
shuyu 已提交
201

S
shuyu 已提交
202 203 204
    /**
     * 设置播放URL
     *
S
false  
shuyu 已提交
205
     * @param url           播放url
S
shuyu 已提交
206
     * @param cacheWithPlay 是否边播边缓存
S
false  
shuyu 已提交
207 208 209
     * @param cachePath     缓存路径,如果是M3U8或者HLS,请设置为false
     * @param mapHeadData   头部信息
     * @param objects       object[0]目前为title
S
shuyu 已提交
210 211
     * @return
     */
S
1.3.0  
shuyu 已提交
212 213 214
    @Override
    public boolean setUp(String url, boolean cacheWithPlay, File cachePath, Map<String, String> mapHeadData, Object... objects) {
        if (setUp(url, cacheWithPlay, cachePath, objects)) {
S
shuyu 已提交
215
            this.mMapHeadData.clear();
S
1.3.0  
shuyu 已提交
216 217
            if (mapHeadData != null)
                this.mMapHeadData.putAll(mapHeadData);
S
shuyu 已提交
218 219 220 221 222
            return true;
        }
        return false;
    }

S
1.3.0  
shuyu 已提交
223 224 225
    /**
     * 设置播放URL
     *
S
false  
shuyu 已提交
226
     * @param url           播放url
S
1.3.0  
shuyu 已提交
227
     * @param cacheWithPlay 是否边播边缓存
S
false  
shuyu 已提交
228 229
     * @param cachePath     缓存路径,如果是M3U8或者HLS,请设置为false
     * @param objects       object[0]目前为title
S
1.3.0  
shuyu 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
     * @return
     */
    @Override
    public boolean setUp(String url, boolean cacheWithPlay, File cachePath, Object... objects) {
        mCache = cacheWithPlay;
        mCachePath = cachePath;
        if (isCurrentMediaListener() &&
                (System.currentTimeMillis() - CLICK_QUIT_FULLSCREEN_TIME) < FULL_SCREEN_NORMAL_DELAY)
            return false;
        mCurrentState = CURRENT_STATE_NORMAL;
        if (cacheWithPlay && url.startsWith("http") && !url.contains("127.0.0.1")) {
            mOriginUrl = url;
            HttpProxyCacheServer proxy = GSYVideoManager.getProxy(getContext().getApplicationContext(), cachePath);
            url = proxy.getProxyUrl(url);
            mCacheFile = (!url.startsWith("http"));
S
readme  
shuyu 已提交
245
            if (!mCacheFile && GSYVideoManager.instance() != null) {
246 247
                proxy.registerCacheListener(GSYVideoManager.instance(), mOriginUrl);
            }
S
1.3.0  
shuyu 已提交
248 249 250 251 252 253 254
        }
        this.mUrl = url;
        this.mObjects = objects;
        setStateAndUi(CURRENT_STATE_NORMAL);
        return true;
    }

S
shuyu 已提交
255 256 257 258 259
    /**
     * 设置播放显示状态
     *
     * @param state
     */
S
shuyu 已提交
260 261 262 263 264 265 266
    protected void setStateAndUi(int state) {
        mCurrentState = state;
        switch (mCurrentState) {
            case CURRENT_STATE_NORMAL:
                if (isCurrentMediaListener()) {
                    cancelProgressTimer();
                    GSYVideoManager.instance().releaseMediaPlayer();
267
                    releasePauseCoverAndBitmap();
S
readme  
shuyu 已提交
268
                    mBuffterPoint = 0;
S
shuyu 已提交
269
                }
S
1.3.3  
shuyu 已提交
270 271 272
                if (mAudioManager != null) {
                    mAudioManager.abandonAudioFocus(onAudioFocusChangeListener);
                }
S
shuyu 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
                break;
            case CURRENT_STATE_PREPAREING:
                resetProgressAndTime();
                break;
            case CURRENT_STATE_PLAYING:
                startProgressTimer();
                break;
            case CURRENT_STATE_PAUSE:
                startProgressTimer();
                break;
            case CURRENT_STATE_ERROR:
                if (isCurrentMediaListener()) {
                    GSYVideoManager.instance().releaseMediaPlayer();
                }
                break;
            case CURRENT_STATE_AUTO_COMPLETE:
                cancelProgressTimer();
S
shuyu 已提交
290 291
                mProgressBar.setProgress(100);
                mCurrentTimeTextView.setText(mTotalTimeTextView.getText());
S
shuyu 已提交
292 293 294 295 296 297 298
                break;
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
S
shuyu 已提交
299 300 301
        if (mHideKey && mIfCurrentIsFullscreen) {
            hideNavKey(mContext);
        }
S
shuyu 已提交
302 303 304 305 306 307
        if (i == R.id.start) {
            if (TextUtils.isEmpty(mUrl)) {
                Toast.makeText(getContext(), getResources().getString(R.string.no_url), Toast.LENGTH_SHORT).show();
                return;
            }
            if (mCurrentState == CURRENT_STATE_NORMAL || mCurrentState == CURRENT_STATE_ERROR) {
S
shuyu 已提交
308 309
                if (!mUrl.startsWith("file") && !CommonUtil.isWifiConnected(getContext())
                        && mNeedShowWifiTip) {
S
shuyu 已提交
310 311 312 313 314 315 316
                    showWifiDialog();
                    return;
                }
                startButtonLogic();
            } else if (mCurrentState == CURRENT_STATE_PLAYING) {
                GSYVideoManager.instance().getMediaPlayer().pause();
                setStateAndUi(CURRENT_STATE_PAUSE);
S
shuyu 已提交
317
                if (mVideoAllCallBack != null && isCurrentMediaListener()) {
S
shuyu 已提交
318
                    if (mIfCurrentIsFullscreen) {
S
1.1.9  
shuyu 已提交
319
                        Debuger.printfLog("onClickStopFullscreen");
S
shuyu 已提交
320
                        mVideoAllCallBack.onClickStopFullscreen(mUrl, mObjects);
S
shuyu 已提交
321
                    } else {
S
1.1.9  
shuyu 已提交
322
                        Debuger.printfLog("onClickStop");
S
shuyu 已提交
323
                        mVideoAllCallBack.onClickStop(mUrl, mObjects);
S
shuyu 已提交
324 325 326
                    }
                }
            } else if (mCurrentState == CURRENT_STATE_PAUSE) {
S
shuyu 已提交
327
                if (mVideoAllCallBack != null && isCurrentMediaListener()) {
S
shuyu 已提交
328
                    if (mIfCurrentIsFullscreen) {
S
1.1.9  
shuyu 已提交
329
                        Debuger.printfLog("onClickResumeFullscreen");
S
shuyu 已提交
330
                        mVideoAllCallBack.onClickResumeFullscreen(mUrl, mObjects);
S
shuyu 已提交
331
                    } else {
S
1.1.9  
shuyu 已提交
332
                        Debuger.printfLog("onClickResume");
S
shuyu 已提交
333
                        mVideoAllCallBack.onClickResume(mUrl, mObjects);
S
shuyu 已提交
334 335 336 337 338 339 340 341
                    }
                }
                GSYVideoManager.instance().getMediaPlayer().start();
                setStateAndUi(CURRENT_STATE_PLAYING);
            } else if (mCurrentState == CURRENT_STATE_AUTO_COMPLETE) {
                startButtonLogic();
            }
        } else if (i == R.id.surface_container && mCurrentState == CURRENT_STATE_ERROR) {
S
shuyu 已提交
342
            if (mVideoAllCallBack != null) {
S
1.1.9  
shuyu 已提交
343
                Debuger.printfLog("onClickStartError");
S
shuyu 已提交
344
                mVideoAllCallBack.onClickStartError(mUrl, mObjects);
S
shuyu 已提交
345 346 347 348 349
            }
            prepareVideo();
        }
    }

S
shuyu 已提交
350
    protected void showWifiDialog() {
S
shuyu 已提交
351 352
    }

S
shuyu 已提交
353 354 355
    /**
     * 播放按键的逻辑
     */
S
shuyu 已提交
356
    private void startButtonLogic() {
S
shuyu 已提交
357
        if (mVideoAllCallBack != null && mCurrentState == CURRENT_STATE_NORMAL) {
S
1.1.9  
shuyu 已提交
358
            Debuger.printfLog("onClickStartIcon");
S
shuyu 已提交
359 360
            mVideoAllCallBack.onClickStartIcon(mUrl, mObjects);
        } else if (mVideoAllCallBack != null) {
S
1.1.9  
shuyu 已提交
361
            Debuger.printfLog("onClickStartError");
S
shuyu 已提交
362
            mVideoAllCallBack.onClickStartError(mUrl, mObjects);
S
shuyu 已提交
363 364 365 366
        }
        prepareVideo();
    }

S
shuyu 已提交
367 368 369
    /**
     * 开始状态视频播放
     */
S
shuyu 已提交
370 371 372 373 374
    protected void prepareVideo() {
        if (GSYVideoManager.instance().listener() != null) {
            GSYVideoManager.instance().listener().onCompletion();
        }
        GSYVideoManager.instance().setListener(this);
375 376
        GSYVideoManager.instance().setPlayTag(mPlayTag);
        GSYVideoManager.instance().setPlayPosition(mPlayPosition);
S
shuyu 已提交
377 378 379
        addTextureView();
        mAudioManager.requestAudioFocus(onAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
        ((Activity) getContext()).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
S
1.2.8  
shuyu 已提交
380
        GSYVideoManager.instance().prepare(mUrl, mMapHeadData, mLooping, mSpeed);
S
shuyu 已提交
381 382 383
        setStateAndUi(CURRENT_STATE_PREPAREING);
    }

S
shuyu 已提交
384 385 386
    /**
     * 监听是否有外部其他多媒体开始播放
     */
S
shuyu 已提交
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
    private AudioManager.OnAudioFocusChangeListener onAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
        @Override
        public void onAudioFocusChange(int focusChange) {
            switch (focusChange) {
                case AudioManager.AUDIOFOCUS_GAIN:
                    break;
                case AudioManager.AUDIOFOCUS_LOSS:
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            releaseAllVideos();
                        }
                    });
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                    if (GSYVideoManager.instance().getMediaPlayer().isPlaying()) {
                        GSYVideoManager.instance().getMediaPlayer().pause();
                    }
                    break;
                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                    break;
            }
        }
    };


S
shuyu 已提交
413 414 415
    /**
     * 重置
     */
S
shuyu 已提交
416 417 418 419
    public void onVideoReset() {
        setStateAndUi(CURRENT_STATE_NORMAL);
    }

S
shuyu 已提交
420 421 422
    /**
     * 暂停状态
     */
S
shuyu 已提交
423
    @Override
S
shuyu 已提交
424 425 426
    public void onVideoPause() {
        if (GSYVideoManager.instance().getMediaPlayer().isPlaying()) {
            setStateAndUi(CURRENT_STATE_PAUSE);
S
shuyu 已提交
427 428
            mPauseTime = System.currentTimeMillis();
            mCurrentPosition = GSYVideoManager.instance().getMediaPlayer().getCurrentPosition();
S
shuyu 已提交
429 430 431 432 433
            if (GSYVideoManager.instance().getMediaPlayer() != null)
                GSYVideoManager.instance().getMediaPlayer().pause();
        }
    }

S
shuyu 已提交
434 435 436
    /**
     * 恢复暂停状态
     */
S
shuyu 已提交
437
    @Override
S
shuyu 已提交
438
    public void onVideoResume() {
S
shuyu 已提交
439
        mPauseTime = 0;
S
shuyu 已提交
440
        if (mCurrentState == CURRENT_STATE_PAUSE) {
S
shuyu 已提交
441
            if (mCurrentPosition > 0 && GSYVideoManager.instance().getMediaPlayer() != null) {
S
shuyu 已提交
442
                setStateAndUi(CURRENT_STATE_PLAYING);
S
shuyu 已提交
443
                GSYVideoManager.instance().getMediaPlayer().seekTo(mCurrentPosition);
S
shuyu 已提交
444 445 446 447 448
                GSYVideoManager.instance().getMediaPlayer().start();
            }
        }
    }

S
shuyu 已提交
449 450 451
    /**
     * 添加播放的view
     */
S
shuyu 已提交
452
    protected void addTextureView() {
S
shuyu 已提交
453 454
        if (mTextureViewContainer.getChildCount() > 0) {
            mTextureViewContainer.removeAllViews();
S
shuyu 已提交
455
        }
S
shuyu 已提交
456 457 458 459
        mTextureView = null;
        mTextureView = new GSYTextureView(getContext());
        mTextureView.setSurfaceTextureListener(this);
        mTextureView.setRotation(mRotate);
S
shuyu 已提交
460 461 462

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
S
shuyu 已提交
463
        mTextureViewContainer.addView(mTextureView, layoutParams);
S
shuyu 已提交
464 465
    }

S
shuyu 已提交
466 467 468 469 470 471 472 473 474
    /**
     * 小窗口
     **/
    @Override
    protected void setSmallVideoTextureView(View.OnTouchListener onTouchListener) {
        mTextureView.setOnTouchListener(onTouchListener);
        mProgressBar.setOnTouchListener(null);
        mFullscreenButton.setOnTouchListener(null);
        mTextureView.setOnClickListener(null);
S
1.1.7  
shuyu 已提交
475 476 477 478 479 480 481 482
        mSmallClose.setVisibility(VISIBLE);
        mSmallClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSmallVideo();
                releaseAllVideos();
            }
        });
S
shuyu 已提交
483 484
    }

S
shuyu 已提交
485 486 487
    /**
     * 设置界面选择
     */
S
shuyu 已提交
488
    public void setRotationView(int rotate) {
S
shuyu 已提交
489 490
        this.mRotate = rotate;
        mTextureView.setRotation(rotate);
S
shuyu 已提交
491 492 493
    }

    public void refreshVideo() {
S
shuyu 已提交
494 495
        if (mTextureView != null) {
            mTextureView.requestLayout();
S
shuyu 已提交
496 497 498 499 500 501 502
        }
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mSurface = new Surface(surface);
        GSYVideoManager.instance().setDisplay(mSurface);
503 504
        //显示暂停切换显示的图片
        showPauseCover();
S
shuyu 已提交
505 506 507 508 509 510 511 512 513
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
S
shuyu 已提交
514
        GSYVideoManager.instance().setDisplay(null);
S
shuyu 已提交
515
        surface.release();
S
1.3.3  
shuyu 已提交
516
        cancelProgressTimer();
S
shuyu 已提交
517 518 519 520 521
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
522 523
        //如果播放的是暂停全屏了
        releasePauseCover();
S
shuyu 已提交
524 525
    }

S
shuyu 已提交
526 527 528
    /**
     * 亮度、进度、音频
     */
S
shuyu 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        int id = v.getId();
        if (id == R.id.fullscreen) {
            return false;
        }
        if (id == R.id.surface_container) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mTouchingProgressBar = true;
                    mDownX = x;
                    mDownY = y;
S
shuyu 已提交
543
                    mMoveY = 0;
S
shuyu 已提交
544 545
                    mChangeVolume = false;
                    mChangePosition = false;
S
shuyu 已提交
546
                    mShowVKey = false;
S
shuyu 已提交
547
                    mBrightness = false;
S
shuyu 已提交
548
                    mFirstTouch = true;
S
shuyu 已提交
549

S
shuyu 已提交
550 551 552 553 554 555 556 557 558 559 560 561
                    break;
                case MotionEvent.ACTION_MOVE:
                    float deltaX = x - mDownX;
                    float deltaY = y - mDownY;
                    float absDeltaX = Math.abs(deltaX);
                    float absDeltaY = Math.abs(deltaY);

                    if (mIfCurrentIsFullscreen || mIsTouchWiget) {
                        if (!mChangePosition && !mChangeVolume && !mBrightness) {
                            if (absDeltaX > mThreshold || absDeltaY > mThreshold) {
                                cancelProgressTimer();
                                if (absDeltaX >= mThreshold) {
562 563 564 565 566
                                    //防止全屏虚拟按键
                                    int screenWidth = CommonUtil.getScreenWidth(getContext());
                                    if (Math.abs(screenWidth - mDownX) > mSeekEndOffset) {
                                        mChangePosition = true;
                                        mDownPosition = getCurrentPositionWhenPlaying();
S
shuyu 已提交
567 568
                                    } else {
                                        mShowVKey = true;
569
                                    }
S
shuyu 已提交
570
                                } else {
S
shuyu 已提交
571 572
                                    int screenHeight = CommonUtil.getScreenHeight(getContext());
                                    boolean noEnd = Math.abs(screenHeight - mDownY) > mSeekEndOffset;
S
shuyu 已提交
573
                                    if (mFirstTouch) {
S
shuyu 已提交
574
                                        mBrightness = (mDownX < mScreenWidth * 0.5f) && noEnd;
S
shuyu 已提交
575
                                        mFirstTouch = false;
S
shuyu 已提交
576 577
                                    }
                                    if (!mBrightness) {
S
shuyu 已提交
578
                                        mChangeVolume = noEnd;
S
shuyu 已提交
579 580
                                        mGestureDownVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                                    }
S
shuyu 已提交
581
                                    mShowVKey = !noEnd;
S
shuyu 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
                                }
                            }
                        }
                    }
                    if (mChangePosition) {
                        int totalTimeDuration = getDuration();
                        mSeekTimePosition = (int) (mDownPosition + deltaX * totalTimeDuration / mScreenWidth);
                        if (mSeekTimePosition > totalTimeDuration)
                            mSeekTimePosition = totalTimeDuration;
                        String seekTime = CommonUtil.stringForTime(mSeekTimePosition);
                        String totalTime = CommonUtil.stringForTime(totalTimeDuration);
                        showProgressDialog(deltaX, seekTime, mSeekTimePosition, totalTime, totalTimeDuration);
                    } else if (mChangeVolume) {
                        deltaY = -deltaY;
                        int max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                        int deltaV = (int) (max * deltaY * 3 / mScreenHeight);
                        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mGestureDownVolume + deltaV, 0);
                        int volumePercent = (int) (mGestureDownVolume * 100 / max + deltaY * 3 * 100 / mScreenHeight);

                        showVolumeDialog(-deltaY, volumePercent);
                    } else if (!mChangePosition && mBrightness) {
S
shuyu 已提交
603
                        if (Math.abs(deltaY) > mThreshold) {
S
false  
shuyu 已提交
604
                            float percent = (-deltaY / mScreenHeight);
S
shuyu 已提交
605 606 607
                            onBrightnessSlide(percent);
                            mDownY = y;
                        }
S
shuyu 已提交
608 609 610 611 612 613 614 615 616 617 618 619
                    }

                    break;
                case MotionEvent.ACTION_UP:
                    mTouchingProgressBar = false;
                    dismissProgressDialog();
                    dismissVolumeDialog();
                    dismissBrightnessDialog();
                    if (mChangePosition) {
                        GSYVideoManager.instance().getMediaPlayer().seekTo(mSeekTimePosition);
                        int duration = getDuration();
                        int progress = mSeekTimePosition * 100 / (duration == 0 ? 1 : duration);
S
shuyu 已提交
620
                        mProgressBar.setProgress(progress);
S
1.1.9  
shuyu 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633
                        if (mVideoAllCallBack != null && isCurrentMediaListener()) {
                            Debuger.printfLog("onTouchScreenSeekPosition");
                            mVideoAllCallBack.onTouchScreenSeekPosition(mUrl, mObjects);
                        }
                    } else if (mBrightness) {
                        if (mVideoAllCallBack != null && isCurrentMediaListener()) {
                            Debuger.printfLog("onTouchScreenSeekLight");
                            mVideoAllCallBack.onTouchScreenSeekLight(mUrl, mObjects);
                        }
                    } else if (mChangeVolume) {
                        if (mVideoAllCallBack != null && isCurrentMediaListener()) {
                            Debuger.printfLog("onTouchScreenSeekVolume");
                            mVideoAllCallBack.onTouchScreenSeekVolume(mUrl, mObjects);
S
shuyu 已提交
634 635
                        }
                    }
S
1.1.9  
shuyu 已提交
636
                    startProgressTimer();
S
shuyu 已提交
637
                    //不要和隐藏虚拟按键后,滑出虚拟按键冲突
S
shuyu 已提交
638
                    if (mHideKey && mShowVKey) {
S
shuyu 已提交
639 640
                        return true;
                    }
S
shuyu 已提交
641 642
                    break;
            }
S
shuyu 已提交
643
        } else if (id == R.id.progress) {
S
shuyu 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    cancelProgressTimer();
                    ViewParent vpdown = getParent();
                    while (vpdown != null) {
                        vpdown.requestDisallowInterceptTouchEvent(true);
                        vpdown = vpdown.getParent();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    startProgressTimer();
                    ViewParent vpup = getParent();
                    while (vpup != null) {
                        vpup.requestDisallowInterceptTouchEvent(false);
                        vpup = vpup.getParent();
                    }
S
shuyu 已提交
661
                    mBrightnessData = -1f;
S
shuyu 已提交
662 663 664 665 666 667 668
                    break;
            }
        }

        return false;
    }

669 670 671 672 673 674 675
    /**
     * 显示暂停切换显示的bitmap
     */
    protected void showPauseCover() {
        try {
            if (mCurrentState == CURRENT_STATE_PAUSE && mFullPauseBitmap != null
                    && !mFullPauseBitmap.isRecycled()) {
676
                mCoverImageView.setRotation(mRotate);
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
                mCoverImageView.setImageBitmap(mFullPauseBitmap);
                mCoverImageView.setVisibility(VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 销毁暂停切换显示的bitmap
     */
    protected void releasePauseCover() {
        try {
            if (mCurrentState != CURRENT_STATE_PAUSE && mFullPauseBitmap != null
                    && !mFullPauseBitmap.isRecycled()) {
                mCoverImageView.setImageResource(R.drawable.empty_drawable);
                mCoverImageView.setVisibility(GONE);
694
                //如果在这里销毁,可能会draw a recycler bitmap error
695 696 697 698 699 700
                mFullPauseBitmap = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
S
shuyu 已提交
701

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    /**
     * 销毁暂停切换显示的bitmap
     */
    protected void releasePauseCoverAndBitmap() {
        try {
            if (mCurrentState != CURRENT_STATE_PAUSE && mFullPauseBitmap != null
                    && !mFullPauseBitmap.isRecycled()) {
                mCoverImageView.setImageResource(R.drawable.empty_drawable);
                mCoverImageView.setVisibility(GONE);
                mFullPauseBitmap.recycle();
                mFullPauseBitmap = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


S
shuyu 已提交
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
    protected void showProgressDialog(float deltaX,
                                      String seekTime, int seekTimePosition,
                                      String totalTime, int totalTimeDuration) {
    }

    protected void dismissProgressDialog() {

    }

    protected void showVolumeDialog(float deltaY, int volumePercent) {

    }

    protected void dismissVolumeDialog() {

    }

    protected void showBrightnessDialog(float percent) {

    }

    protected void dismissBrightnessDialog() {

    }

S
shuyu 已提交
745 746 747 748
    protected void onClickUiToggle() {

    }

S
shuyu 已提交
749 750 751 752 753 754 755 756 757 758

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

S
shuyu 已提交
759 760 761
    /***
     * 拖动进度条
     */
S
shuyu 已提交
762 763
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
S
1.1.9  
shuyu 已提交
764 765 766 767 768 769 770 771 772
        if (mVideoAllCallBack != null && isCurrentMediaListener()) {
            if (isIfCurrentIsFullscreen()) {
                Debuger.printfLog("onClickSeekbarFullscreen");
                mVideoAllCallBack.onClickSeekbarFullscreen(mUrl, mObjects);
            } else {
                Debuger.printfLog("onClickSeekbar");
                mVideoAllCallBack.onClickSeekbar(mUrl, mObjects);
            }
        }
773
        if (GSYVideoManager.instance().getMediaPlayer() != null && mHadPlay) {
S
shuyu 已提交
774 775 776 777 778 779 780 781
            int time = seekBar.getProgress() * getDuration() / 100;
            GSYVideoManager.instance().getMediaPlayer().seekTo(time);
        }
    }

    @Override
    public void onPrepared() {
        if (mCurrentState != CURRENT_STATE_PREPAREING) return;
S
1.3.4  
shuyu 已提交
782 783 784 785 786 787

        if (GSYVideoManager.instance().getMediaPlayer() != null) {
            GSYVideoManager.instance().getMediaPlayer().start();
        }

        if (GSYVideoManager.instance().getMediaPlayer() != null && mSeekToInAdvance != -1) {
S
shuyu 已提交
788 789
            GSYVideoManager.instance().getMediaPlayer().seekTo(mSeekToInAdvance);
            mSeekToInAdvance = -1;
S
shuyu 已提交
790
        }
S
1.3.4  
shuyu 已提交
791

S
shuyu 已提交
792
        startProgressTimer();
S
1.3.4  
shuyu 已提交
793

S
shuyu 已提交
794
        setStateAndUi(CURRENT_STATE_PLAYING);
S
1.3.4  
shuyu 已提交
795

S
1.2.2  
shuyu 已提交
796 797 798 799
        if (mVideoAllCallBack != null && isCurrentMediaListener()) {
            Debuger.printfLog("onPrepared");
            mVideoAllCallBack.onPrepared(mUrl, mObjects);
        }
S
1.3.4  
shuyu 已提交
800 801 802

        if (GSYVideoManager.instance().getMediaPlayer() != null && mSeekOnStart > 0) {
            GSYVideoManager.instance().getMediaPlayer().seekTo(mSeekOnStart);
S
1.4.4  
shuyu 已提交
803
            mSeekOnStart = 0;
S
1.3.4  
shuyu 已提交
804 805
        }

S
1.2.7  
shuyu 已提交
806
        mHadPlay = true;
S
shuyu 已提交
807 808 809 810
    }

    @Override
    public void onAutoCompletion() {
S
shuyu 已提交
811
        if (mVideoAllCallBack != null && isCurrentMediaListener()) {
S
1.1.9  
shuyu 已提交
812 813
            Debuger.printfLog("onAutoComplete");
            mVideoAllCallBack.onAutoComplete(mUrl, mObjects);
S
shuyu 已提交
814 815
        }
        setStateAndUi(CURRENT_STATE_AUTO_COMPLETE);
S
shuyu 已提交
816 817
        if (mTextureViewContainer.getChildCount() > 0) {
            mTextureViewContainer.removeAllViews();
S
shuyu 已提交
818 819
        }

S
shuyu 已提交
820
        if (IF_FULLSCREEN_FROM_NORMAL) {
S
shuyu 已提交
821
            IF_FULLSCREEN_FROM_NORMAL = false;
S
1.3.1  
shuyu 已提交
822 823 824
            if (GSYVideoManager.instance().lastListener() != null) {
                GSYVideoManager.instance().lastListener().onAutoCompletion();
            }
S
shuyu 已提交
825
        }
S
1.3.1  
shuyu 已提交
826 827
        if (!mIfCurrentIsFullscreen)
            GSYVideoManager.instance().setLastListener(null);
S
shuyu 已提交
828 829 830 831 832 833 834 835
        mAudioManager.abandonAudioFocus(onAudioFocusChangeListener);
        ((Activity) getContext()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void onCompletion() {
        //make me normal first
        setStateAndUi(CURRENT_STATE_NORMAL);
S
shuyu 已提交
836 837
        if (mTextureViewContainer.getChildCount() > 0) {
            mTextureViewContainer.removeAllViews();
S
shuyu 已提交
838 839 840 841
        }

        if (IF_FULLSCREEN_FROM_NORMAL) {//如果在进入全屏后播放完就初始化自己非全屏的控件
            IF_FULLSCREEN_FROM_NORMAL = false;
S
1.3.1  
shuyu 已提交
842 843 844 845 846 847 848
            if (GSYVideoManager.instance().lastListener() != null) {
                GSYVideoManager.instance().lastListener().onCompletion();//回到上面的onAutoCompletion
            }
        }
        if (!mIfCurrentIsFullscreen) {
            GSYVideoManager.instance().setListener(null);
            GSYVideoManager.instance().setLastListener(null);
S
shuyu 已提交
849 850 851 852 853 854 855 856 857 858 859
        }
        GSYVideoManager.instance().setCurrentVideoHeight(0);
        GSYVideoManager.instance().setCurrentVideoWidth(0);

        mAudioManager.abandonAudioFocus(onAudioFocusChangeListener);
        ((Activity) getContext()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void onBufferingUpdate(int percent) {
        if (mCurrentState != CURRENT_STATE_NORMAL && mCurrentState != CURRENT_STATE_PREPAREING) {
S
shuyu 已提交
860 861
            if (percent != 0) {
                setTextAndProgress(percent);
S
readme  
shuyu 已提交
862
                mBuffterPoint = percent;
863
                Debuger.printfLog("Net speed: " + getNetSpeedText() + " percent " + percent);
S
shuyu 已提交
864
            }
S
1.2.6  
shuyu 已提交
865
            //循环清除进度
S
shuyu 已提交
866
            if (mLooping && mHadPlay && percent == 0 && mProgressBar.getProgress() >= (mProgressBar.getMax() - 1)) {
S
1.2.6  
shuyu 已提交
867 868
                loopSetProgressAndTime();
            }
S
shuyu 已提交
869 870 871 872 873 874 875 876 877 878 879 880
        }
    }

    @Override
    public void onSeekComplete() {

    }

    @Override
    public void onError(int what, int extra) {
        if (what != 38 && what != -38) {
            setStateAndUi(CURRENT_STATE_ERROR);
S
shuyu 已提交
881
            deleteCacheFileWhenError();
882 883 884
            if (mVideoAllCallBack != null) {
                mVideoAllCallBack.onPlayError(mUrl, mObjects);
            }
S
shuyu 已提交
885 886 887 888 889 890 891
        }
    }

    @Override
    public void onInfo(int what, int extra) {
        if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
            BACKUP_PLAYING_BUFFERING_STATE = mCurrentState;
S
1.2.7  
shuyu 已提交
892 893 894 895 896
            if (mLooping && mHadPlay) {
                //循环在播放的不显示
            } else {
                setStateAndUi(CURRENT_STATE_PLAYING_BUFFERING_START);
            }
S
shuyu 已提交
897 898
        } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
            if (BACKUP_PLAYING_BUFFERING_STATE != -1) {
S
1.2.7  
shuyu 已提交
899 900 901 902 903
                if (mLooping && mHadPlay) {
                    //循环在播放的不显示
                } else {
                    setStateAndUi(BACKUP_PLAYING_BUFFERING_STATE);
                }
S
shuyu 已提交
904 905 906
                BACKUP_PLAYING_BUFFERING_STATE = -1;
            }
        } else if (what == IMediaPlayer.MEDIA_INFO_VIDEO_ROTATION_CHANGED) {
S
shuyu 已提交
907 908 909
            mRotate = extra;
            if (mTextureView != null)
                mTextureView.setRotation(mRotate);
S
shuyu 已提交
910 911 912 913 914 915 916 917
        }
    }

    @Override
    public void onVideoSizeChanged() {
        int mVideoWidth = GSYVideoManager.instance().getCurrentVideoWidth();
        int mVideoHeight = GSYVideoManager.instance().getCurrentVideoHeight();
        if (mVideoWidth != 0 && mVideoHeight != 0) {
S
shuyu 已提交
918
            mTextureView.requestLayout();
S
shuyu 已提交
919 920 921 922 923
        }
    }

    @Override
    public void onBackFullscreen() {
S
shuyu 已提交
924

S
shuyu 已提交
925 926
    }

S
shuyu 已提交
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
    /**
     * 清除当前缓存
     */
    public void clearCurrentCache() {
        if (mCacheFile) {
            //是否为缓存文件
            Debuger.printfError(" mCacheFile Local Error " + mUrl);
            //可能是因为缓存文件除了问题
            CommonUtil.deleteFile(mUrl.replace("file://", ""));
            mUrl = mOriginUrl;
        } else if (mUrl.contains("127.0.0.1")) {
            //是否为缓存了未完成的文件
            Md5FileNameGenerator md5FileNameGenerator = new Md5FileNameGenerator();
            String name = md5FileNameGenerator.generate(mOriginUrl);
            if (mCachePath != null) {
                String path = mCachePath.getAbsolutePath() + File.separator + name + ".download";
                CommonUtil.deleteFile(path);
            } else {
                String path = StorageUtils.getIndividualCacheDirectory
                        (getContext().getApplicationContext()).getAbsolutePath()
                        + File.separator + name + ".download";
                CommonUtil.deleteFile(path);
            }
        }

    }


    /**
     * 播放错误的时候,删除缓存文件
     */
    private void deleteCacheFileWhenError() {
        clearCurrentCache();
        Debuger.printfError("Link Or mCache Error, Please Try Again" + mUrl);
        mUrl = mOriginUrl;
    }

S
shuyu 已提交
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
    protected void startProgressTimer() {
        cancelProgressTimer();
        UPDATE_PROGRESS_TIMER = new Timer();
        mProgressTimerTask = new ProgressTimerTask();
        UPDATE_PROGRESS_TIMER.schedule(mProgressTimerTask, 0, 300);
    }

    protected void cancelProgressTimer() {
        if (UPDATE_PROGRESS_TIMER != null) {
            UPDATE_PROGRESS_TIMER.cancel();
        }
        if (mProgressTimerTask != null) {
            mProgressTimerTask.cancel();
        }

    }

    protected class ProgressTimerTask extends TimerTask {
        @Override
        public void run() {
            if (mCurrentState == CURRENT_STATE_PLAYING || mCurrentState == CURRENT_STATE_PAUSE) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        setTextAndProgress(0);
                    }
                });
            }
        }
    }

S
1.2.2  
shuyu 已提交
995 996 997 998
    /**
     * 获取当前播放进度
     */
    public int getCurrentPositionWhenPlaying() {
S
shuyu 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
        int position = 0;
        if (mCurrentState == CURRENT_STATE_PLAYING || mCurrentState == CURRENT_STATE_PAUSE) {
            try {
                position = (int) GSYVideoManager.instance().getMediaPlayer().getCurrentPosition();
            } catch (IllegalStateException e) {
                e.printStackTrace();
                return position;
            }
        }
        return position;
    }

S
1.2.2  
shuyu 已提交
1011 1012 1013 1014
    /**
     * 获取当前总时长
     */
    public int getDuration() {
S
shuyu 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
        int duration = 0;
        try {
            duration = (int) GSYVideoManager.instance().getMediaPlayer().getDuration();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            return duration;
        }
        return duration;
    }

    protected void setTextAndProgress(int secProgress) {
        int position = getCurrentPositionWhenPlaying();
        int duration = getDuration();
        int progress = position * 100 / (duration == 0 ? 1 : duration);
        setProgressAndTime(progress, secProgress, position, duration);
    }

    protected void setProgressAndTime(int progress, int secProgress, int currentTime, int totalTime) {
        if (!mTouchingProgressBar) {
S
shuyu 已提交
1034
            if (progress != 0) mProgressBar.setProgress(progress);
S
shuyu 已提交
1035
        }
1036
        if (secProgress > 94) secProgress = 100;
1037 1038 1039
        if (secProgress != 0 && !mCacheFile) {
            mProgressBar.setSecondaryProgress(secProgress);
        }
S
shuyu 已提交
1040
        mTotalTimeTextView.setText(CommonUtil.stringForTime(totalTime));
S
shuyu 已提交
1041 1042
        if (currentTime > 0)
            mCurrentTimeTextView.setText(CommonUtil.stringForTime(currentTime));
S
shuyu 已提交
1043 1044
    }

S
1.2.6  
shuyu 已提交
1045

S
shuyu 已提交
1046
    protected void resetProgressAndTime() {
S
shuyu 已提交
1047 1048 1049 1050
        mProgressBar.setProgress(0);
        mProgressBar.setSecondaryProgress(0);
        mCurrentTimeTextView.setText(CommonUtil.stringForTime(0));
        mTotalTimeTextView.setText(CommonUtil.stringForTime(0));
S
shuyu 已提交
1051 1052
    }

S
1.2.6  
shuyu 已提交
1053 1054 1055 1056 1057 1058 1059

    protected void loopSetProgressAndTime() {
        mProgressBar.setProgress(0);
        mProgressBar.setSecondaryProgress(0);
        mCurrentTimeTextView.setText(CommonUtil.stringForTime(0));
    }

S
1.1.9  
shuyu 已提交
1060 1061 1062
    /**
     * 页面销毁了记得调用是否所有的video
     */
S
shuyu 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    public static void releaseAllVideos() {
        if (IF_RELEASE_WHEN_ON_PAUSE) {
            if (GSYVideoManager.instance().listener() != null) {
                GSYVideoManager.instance().listener().onCompletion();
            }
            GSYVideoManager.instance().releaseMediaPlayer();
        } else {
            IF_RELEASE_WHEN_ON_PAUSE = true;
        }
    }

    /**
     * if I am playing release me
     */
    public void release() {
        if (isCurrentMediaListener() &&
                (System.currentTimeMillis() - CLICK_QUIT_FULLSCREEN_TIME) > FULL_SCREEN_NORMAL_DELAY) {
            releaseAllVideos();
        }
S
1.2.7  
shuyu 已提交
1082
        mHadPlay = false;
S
shuyu 已提交
1083 1084
    }

1085

S
shuyu 已提交
1086 1087 1088 1089 1090
    protected boolean isCurrentMediaListener() {
        return GSYVideoManager.instance().listener() != null
                && GSYVideoManager.instance().listener() == this;
    }

S
shuyu 已提交
1091

S
shuyu 已提交
1092 1093 1094 1095 1096 1097
    /**
     * 滑动改变亮度
     *
     * @param percent
     */
    private void onBrightnessSlide(float percent) {
S
shuyu 已提交
1098
        //if (mBrightnessData < 0) {
S
shuyu 已提交
1099 1100 1101 1102 1103 1104
        mBrightnessData = ((Activity) (mContext)).getWindow().getAttributes().screenBrightness;
        if (mBrightnessData <= 0.00f) {
            mBrightnessData = 0.50f;
        } else if (mBrightnessData < 0.01f) {
            mBrightnessData = 0.01f;
        }
S
shuyu 已提交
1105
        //}
S
shuyu 已提交
1106 1107
        WindowManager.LayoutParams lpa = ((Activity) (mContext)).getWindow().getAttributes();
        lpa.screenBrightness = mBrightnessData + percent;
S
shuyu 已提交
1108 1109 1110 1111 1112 1113
        if (lpa.screenBrightness > 1.0f) {
            lpa.screenBrightness = 1.0f;
        } else if (lpa.screenBrightness < 0.01f) {
            lpa.screenBrightness = 0.01f;
        }
        showBrightnessDialog(lpa.screenBrightness);
S
shuyu 已提交
1114
        ((Activity) (mContext)).getWindow().setAttributes(lpa);
S
shuyu 已提交
1115 1116 1117 1118 1119 1120
    }

    public boolean isTouchWiget() {
        return mIsTouchWiget;
    }

S
1.1.4  
shuyu 已提交
1121 1122 1123
    /**
     * 是否可以滑动界面改变进度,声音等
     */
S
shuyu 已提交
1124 1125 1126
    public void setIsTouchWiget(boolean isTouchWiget) {
        this.mIsTouchWiget = isTouchWiget;
    }
S
shuyu 已提交
1127

S
shuyu 已提交
1128 1129 1130
    /**
     * 获取播放按键
     */
S
shuyu 已提交
1131
    public View getStartButton() {
S
shuyu 已提交
1132
        return mStartButton;
S
shuyu 已提交
1133 1134
    }

S
shuyu 已提交
1135 1136 1137
    /**
     * 获取全屏按键
     */
1138
    public ImageView getFullscreenButton() {
S
shuyu 已提交
1139
        return mFullscreenButton;
S
shuyu 已提交
1140
    }
S
shuyu 已提交
1141

S
shuyu 已提交
1142 1143 1144
    /**
     * 获取返回按键
     */
S
shuyu 已提交
1145
    public ImageView getBackButton() {
S
shuyu 已提交
1146
        return mBackButton;
S
shuyu 已提交
1147 1148
    }

S
shuyu 已提交
1149 1150 1151
    /**
     * 获取当前播放状态
     */
S
shuyu 已提交
1152 1153 1154
    public int getCurrentState() {
        return mCurrentState;
    }
S
shuyu 已提交
1155

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
    /**
     * 播放tag防止错误,因为普通的url也可能重复
     */
    public String getPlayTag() {
        return mPlayTag;
    }

    /**
     * 播放tag防止错误,因为普通的url也可能重复
     *
     * @param playTag 保证不重复就好
     */
    public void setPlayTag(String playTag) {
        this.mPlayTag = playTag;
    }


    public int getPlayPosition() {
        return mPlayPosition;
    }

S
1.1.3  
shuyu 已提交
1177 1178 1179 1180 1181
    /**
     * 设置播放位置防止错位
     */
    public void setPlayPosition(int playPosition) {
        this.mPlayPosition = playPosition;
1182 1183
    }

S
shuyu 已提交
1184
    /**
S
1.1.9  
shuyu 已提交
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
     * 显示小窗口的关闭按键
     */
    public void setSmallCloseShow() {
        mSmallClose.setVisibility(VISIBLE);
    }

    /**
     * 隐藏小窗口的关闭按键
     */
    public void setSmallCloseHide() {
        mSmallClose.setVisibility(GONE);
    }

    /**
     * 退出全屏,主要用于返回键
S
shuyu 已提交
1200 1201
     *
     * @return 返回是否全屏
S
shuyu 已提交
1202 1203 1204 1205 1206 1207 1208
     */
    public static boolean backFromWindowFull(Context context) {
        boolean backFrom = false;
        ViewGroup vp = (ViewGroup) (CommonUtil.scanForActivity(context)).findViewById(Window.ID_ANDROID_CONTENT);
        View oldF = vp.findViewById(FULLSCREEN_ID);
        if (oldF != null) {
            backFrom = true;
S
shuyu 已提交
1209
            hideNavKey(context);
S
1.3.1  
shuyu 已提交
1210 1211 1212
            if (GSYVideoManager.instance().lastListener() != null) {
                GSYVideoManager.instance().lastListener().onBackFullscreen();
            }
S
shuyu 已提交
1213 1214 1215 1216
        }
        return backFrom;
    }

S
1.2.9  
shuyu 已提交
1217 1218 1219 1220 1221 1222
    /**
     * 网络速度
     * 注意,这里如果是开启了缓存,因为读取本地代理,缓存成功后还是存在速度的
     * 再打开已经缓存的本地文件,网络速度才会回0.因为是播放本地文件了
     */
    public long getNetSpeed() {
1223 1224 1225
        if (GSYVideoManager.instance().getMediaPlayer() != null
                && (GSYVideoManager.instance().getMediaPlayer() instanceof IjkMediaPlayer)) {
            return ((IjkMediaPlayer)GSYVideoManager.instance().getMediaPlayer()).getTcpSpeed();
S
shuyu 已提交
1226 1227 1228
        } else {
            return -1;
        }
S
1.2.9  
shuyu 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237

    }

    /**
     * 网络速度
     * 注意,这里如果是开启了缓存,因为读取本地代理,缓存成功后还是存在速度的
     * 再打开已经缓存的本地文件,网络速度才会回0.因为是播放本地文件了
     */
    public String getNetSpeedText() {
S
shuyu 已提交
1238
        long speed = getNetSpeed();
S
1.2.9  
shuyu 已提交
1239 1240
        return getTextSpeed(speed);
    }
S
shuyu 已提交
1241

S
1.4.4  
shuyu 已提交
1242
    public long getSeekOnStart() {
S
1.3.4  
shuyu 已提交
1243 1244 1245 1246 1247
        return mSeekOnStart;
    }

    /**
     * 从哪里开始播放
1248
     * 目前有时候前几秒有跳动问题,毫秒
S
1.3.4  
shuyu 已提交
1249
     */
S
1.4.4  
shuyu 已提交
1250
    public void setSeekOnStart(long seekOnStart) {
S
1.3.4  
shuyu 已提交
1251 1252
        this.mSeekOnStart = seekOnStart;
    }
S
readme  
shuyu 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261


    /**
     * 缓冲进度/缓存进度
     */
    public int getBuffterPoint() {
        return mBuffterPoint;
    }

S
shuyu 已提交
1262
}