GSYVideoPlayer.java 33.8 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 26 27 28
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;
import com.shuyu.gsyvideoplayer.listener.GSYMediaPlayerListener;
import com.shuyu.gsyvideoplayer.listener.VideoAllCallBack;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
S
1.1.3  
shuyu 已提交
29
import com.shuyu.gsyvideoplayer.video.GSYBaseVideoPlayer;
S
shuyu 已提交
30 31 32 33 34 35 36 37

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import tv.danmaku.ijk.media.player.IMediaPlayer;

S
shuyu 已提交
38

S
shuyu 已提交
39 40 41 42
/**
 * Created by shuyu on 2016/11/11.
 */

S
1.1.3  
shuyu 已提交
43
public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View.OnClickListener, View.OnTouchListener, SeekBar.OnSeekBarChangeListener, TextureView.SurfaceTextureListener {
S
shuyu 已提交
44 45 46

    public static final String TAG = "GSYVideoPlayer";

S
shuyu 已提交
47

S
shuyu 已提交
48 49 50 51 52 53 54
    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 已提交
55

S
shuyu 已提交
56
    public static final int FULL_SCREEN_NORMAL_DELAY = 2000;
S
shuyu 已提交
57

S
shuyu 已提交
58
    protected static int BACKUP_PLAYING_BUFFERING_STATE = -1;
S
shuyu 已提交
59

S
shuyu 已提交
60
    protected static boolean IF_FULLSCREEN_FROM_NORMAL = false;
S
shuyu 已提交
61

S
shuyu 已提交
62
    public static boolean IF_RELEASE_WHEN_ON_PAUSE = true;
S
shuyu 已提交
63 64 65

    public static boolean WIFI_TIP_DIALOG_SHOWED = false;

S
shuyu 已提交
66

S
shuyu 已提交
67
    protected static Timer UPDATE_PROGRESS_TIMER;
S
shuyu 已提交
68 69


S
shuyu 已提交
70
    protected View mStartButton;
S
shuyu 已提交
71 72 73 74 75
    protected SeekBar mProgressBar;
    protected ImageView mFullscreenButton;
    protected TextView mCurrentTimeTextView, mTotalTimeTextView;
    protected ViewGroup mTopContainer, mBottomContainer;
    protected GSYTextureView mTextureView;
S
shuyu 已提交
76
    protected Surface mSurface;
S
shuyu 已提交
77
    protected ImageView mBackButton;
S
shuyu 已提交
78 79 80

    protected Map<String, String> mMapHeadData = new HashMap<>();
    protected ProgressTimerTask mProgressTimerTask;
S
shuyu 已提交
81 82
    protected AudioManager mAudioManager; //音频焦点的监听
    protected VideoAllCallBack mVideoAllCallBack;
S
shuyu 已提交
83 84 85

    protected Handler mHandler = new Handler();

86 87 88 89
    protected String mPlayTag = ""; //播放的tag,防止错误,因为普通的url也可能重复

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

S
shuyu 已提交
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
    protected float mDownX;//触摸的X

    protected float mDownY; //触摸的Y

    protected float mBrightnessData = -1; //亮度

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

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

    protected int mScreenWidth; //屏幕宽度

    protected int mScreenHeight; //屏幕高度

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

    protected int mSeekToInAdvance = -1; //// TODO: 2016/11/13 调过广告

    protected int mRotate = 0; //针对某些视频的旋转信息做了旋转处理

    protected int mSeekTimePosition; //手动改变滑动的位置

    protected long mPauseTime; //保存暂停时的时间

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

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

    protected boolean mIsTouchWiget = false;

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

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

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

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

S
shuyu 已提交
128
    protected boolean mLooping = false;//// TODO: 2016/11/13 循环
S
shuyu 已提交
129

S
shuyu 已提交
130 131 132 133

    /**
     * 当前UI
     */
S
shuyu 已提交
134
    public abstract int getLayoutId();
S
shuyu 已提交
135

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

S
shuyu 已提交
141 142 143 144 145 146 147 148 149 150 151
    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 已提交
152
        this.mContext = context;
S
shuyu 已提交
153
        View.inflate(context, getLayoutId(), this);
S
shuyu 已提交
154
        mStartButton = findViewById(R.id.start);
S
1.1.7  
shuyu 已提交
155
        mSmallClose = findViewById(R.id.small_close);
S
shuyu 已提交
156 157 158 159 160 161 162 163
        mBackButton = (ImageView) findViewById(R.id.back);
        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 已提交
164 165
        if (isInEditMode())
            return;
S
shuyu 已提交
166 167 168 169 170 171 172 173 174
        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 已提交
175 176 177 178 179
        mScreenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
        mScreenHeight = getContext().getResources().getDisplayMetrics().heightPixels;
        mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
    }

S
shuyu 已提交
180 181 182 183 184 185 186 187
    /**
     * 设置播放URL
     *
     * @param url
     * @param cacheWithPlay 是否边播边缓存
     * @param objects
     * @return
     */
S
shuyu 已提交
188
    public boolean setUp(String url, boolean cacheWithPlay, Object... objects) {
S
shuyu 已提交
189
        mCache = cacheWithPlay;
S
shuyu 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203
        if (isCurrentMediaListener() &&
                (System.currentTimeMillis() - CLICK_QUIT_FULLSCREEN_TIME) < FULL_SCREEN_NORMAL_DELAY)
            return false;
        mCurrentState = CURRENT_STATE_NORMAL;
        if (cacheWithPlay && url.startsWith("http")) {
            HttpProxyCacheServer proxy = GSYVideoManager.getProxy(getContext().getApplicationContext());
            url = proxy.getProxyUrl(url);
        }
        this.mUrl = url;
        this.mObjects = objects;
        setStateAndUi(CURRENT_STATE_NORMAL);
        return true;
    }

S
shuyu 已提交
204 205 206 207 208 209 210 211 212 213
    /**
     * 设置播放URL
     *
     * @param url
     * @param cacheWithPlay 是否边播边缓存
     * @param mapHeadData
     * @param objects
     * @return
     */

S
shuyu 已提交
214 215 216 217 218 219 220 221 222
    public boolean setUp(String url, boolean cacheWithPlay, Map<String, String> mapHeadData, Object... objects) {
        if (setUp(url, cacheWithPlay, objects)) {
            this.mMapHeadData.clear();
            this.mMapHeadData.putAll(mapHeadData);
            return true;
        }
        return false;
    }

S
shuyu 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    /**
     * 设置播放过程中的回调
     *
     * @param mVideoAllCallBack
     */
    protected void setVideoAllCallBack(VideoAllCallBack mVideoAllCallBack) {
        this.mVideoAllCallBack = mVideoAllCallBack;
    }


    /**
     * 设置播放显示状态
     *
     * @param state
     */
S
shuyu 已提交
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
    protected void setStateAndUi(int state) {
        mCurrentState = state;
        switch (mCurrentState) {
            case CURRENT_STATE_NORMAL:
                if (isCurrentMediaListener()) {
                    cancelProgressTimer();
                    GSYVideoManager.instance().releaseMediaPlayer();
                }
                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 已提交
263 264
                mProgressBar.setProgress(100);
                mCurrentTimeTextView.setText(mTotalTimeTextView.getText());
S
shuyu 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
                break;
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        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) {
                if (!mUrl.startsWith("file") && !CommonUtil.isWifiConnected(getContext()) && !WIFI_TIP_DIALOG_SHOWED) {
                    showWifiDialog();
                    return;
                }
                startButtonLogic();
            } else if (mCurrentState == CURRENT_STATE_PLAYING) {
                GSYVideoManager.instance().getMediaPlayer().pause();
                setStateAndUi(CURRENT_STATE_PAUSE);
S
shuyu 已提交
286
                if (mVideoAllCallBack != null && isCurrentMediaListener()) {
S
shuyu 已提交
287
                    if (mIfCurrentIsFullscreen) {
S
shuyu 已提交
288
                        mVideoAllCallBack.onClickStopFullscreen(mUrl, mObjects);
S
shuyu 已提交
289
                    } else {
S
shuyu 已提交
290
                        mVideoAllCallBack.onClickStop(mUrl, mObjects);
S
shuyu 已提交
291 292 293
                    }
                }
            } else if (mCurrentState == CURRENT_STATE_PAUSE) {
S
shuyu 已提交
294
                if (mVideoAllCallBack != null && isCurrentMediaListener()) {
S
shuyu 已提交
295
                    if (mIfCurrentIsFullscreen) {
S
shuyu 已提交
296
                        mVideoAllCallBack.onClickResumeFullscreen(mUrl, mObjects);
S
shuyu 已提交
297
                    } else {
S
shuyu 已提交
298
                        mVideoAllCallBack.onClickResume(mUrl, mObjects);
S
shuyu 已提交
299 300 301 302 303 304 305 306
                    }
                }
                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 已提交
307 308
            if (mVideoAllCallBack != null) {
                mVideoAllCallBack.onClickStartError(mUrl, mObjects);
S
shuyu 已提交
309 310 311 312 313
            }
            prepareVideo();
        }
    }

S
shuyu 已提交
314
    protected void showWifiDialog() {
S
shuyu 已提交
315 316
    }

S
shuyu 已提交
317 318 319
    /**
     * 播放按键的逻辑
     */
S
shuyu 已提交
320
    private void startButtonLogic() {
S
shuyu 已提交
321 322 323 324
        if (mVideoAllCallBack != null && mCurrentState == CURRENT_STATE_NORMAL) {
            mVideoAllCallBack.onClickStartIcon(mUrl, mObjects);
        } else if (mVideoAllCallBack != null) {
            mVideoAllCallBack.onClickStartError(mUrl, mObjects);
S
shuyu 已提交
325 326 327 328
        }
        prepareVideo();
    }

S
shuyu 已提交
329 330 331
    /**
     * 开始状态视频播放
     */
S
shuyu 已提交
332 333 334 335 336
    protected void prepareVideo() {
        if (GSYVideoManager.instance().listener() != null) {
            GSYVideoManager.instance().listener().onCompletion();
        }
        GSYVideoManager.instance().setListener(this);
337 338
        GSYVideoManager.instance().setPlayTag(mPlayTag);
        GSYVideoManager.instance().setPlayPosition(mPlayPosition);
S
shuyu 已提交
339 340 341 342 343 344 345 346 347
        addTextureView();
        AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
        mAudioManager.requestAudioFocus(onAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

        ((Activity) getContext()).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        GSYVideoManager.instance().prepare(mUrl, mMapHeadData, mLooping);
        setStateAndUi(CURRENT_STATE_PREPAREING);
    }

S
shuyu 已提交
348 349 350
    /**
     * 监听是否有外部其他多媒体开始播放
     */
S
shuyu 已提交
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
    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 已提交
377 378 379
    /**
     * 重置
     */
S
shuyu 已提交
380 381 382 383
    public void onVideoReset() {
        setStateAndUi(CURRENT_STATE_NORMAL);
    }

S
shuyu 已提交
384 385 386
    /**
     * 暂停状态
     */
S
shuyu 已提交
387 388 389
    public void onVideoPause() {
        if (GSYVideoManager.instance().getMediaPlayer().isPlaying()) {
            setStateAndUi(CURRENT_STATE_PAUSE);
S
shuyu 已提交
390 391
            mPauseTime = System.currentTimeMillis();
            mCurrentPosition = GSYVideoManager.instance().getMediaPlayer().getCurrentPosition();
S
shuyu 已提交
392 393 394 395 396
            if (GSYVideoManager.instance().getMediaPlayer() != null)
                GSYVideoManager.instance().getMediaPlayer().pause();
        }
    }

S
shuyu 已提交
397 398 399
    /**
     * 恢复暂停状态
     */
S
shuyu 已提交
400
    public void onVideoResume() {
S
shuyu 已提交
401
        mPauseTime = 0;
S
shuyu 已提交
402
        if (mCurrentState == CURRENT_STATE_PAUSE) {
S
shuyu 已提交
403
            if (mCurrentPosition > 0 && GSYVideoManager.instance().getMediaPlayer() != null) {
S
shuyu 已提交
404
                setStateAndUi(CURRENT_STATE_PLAYING);
S
shuyu 已提交
405
                GSYVideoManager.instance().getMediaPlayer().seekTo(mCurrentPosition);
S
shuyu 已提交
406 407 408 409 410
                GSYVideoManager.instance().getMediaPlayer().start();
            }
        }
    }

S
shuyu 已提交
411 412 413
    /**
     * 添加播放的view
     */
S
shuyu 已提交
414
    protected void addTextureView() {
S
shuyu 已提交
415 416
        if (mTextureViewContainer.getChildCount() > 0) {
            mTextureViewContainer.removeAllViews();
S
shuyu 已提交
417
        }
S
shuyu 已提交
418 419 420 421
        mTextureView = null;
        mTextureView = new GSYTextureView(getContext());
        mTextureView.setSurfaceTextureListener(this);
        mTextureView.setRotation(mRotate);
S
shuyu 已提交
422 423 424

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
S
shuyu 已提交
425
        mTextureViewContainer.addView(mTextureView, layoutParams);
S
shuyu 已提交
426 427
    }

S
shuyu 已提交
428 429 430 431 432 433 434 435 436
    /**
     * 小窗口
     **/
    @Override
    protected void setSmallVideoTextureView(View.OnTouchListener onTouchListener) {
        mTextureView.setOnTouchListener(onTouchListener);
        mProgressBar.setOnTouchListener(null);
        mFullscreenButton.setOnTouchListener(null);
        mTextureView.setOnClickListener(null);
S
1.1.7  
shuyu 已提交
437 438 439 440 441 442 443 444
        mSmallClose.setVisibility(VISIBLE);
        mSmallClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSmallVideo();
                releaseAllVideos();
            }
        });
S
shuyu 已提交
445 446 447
    }


S
1.1.7  
shuyu 已提交
448 449 450 451 452 453 454 455
    public void setSmallCloseShow() {
        mSmallClose.setVisibility(VISIBLE);
    }

    public void setSmallCloseHide() {
        mSmallClose.setVisibility(GONE);
    }

S
shuyu 已提交
456 457 458
    /**
     * 设置界面选择
     */
S
shuyu 已提交
459
    public void setRotationView(int rotate) {
S
shuyu 已提交
460 461
        this.mRotate = rotate;
        mTextureView.setRotation(rotate);
S
shuyu 已提交
462 463 464
    }

    public void refreshVideo() {
S
shuyu 已提交
465 466
        if (mTextureView != null) {
            mTextureView.requestLayout();
S
shuyu 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
        }
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mSurface = new Surface(surface);
        GSYVideoManager.instance().setDisplay(mSurface);
    }

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

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
S
shuyu 已提交
483
        GSYVideoManager.instance().setDisplay(null);
S
shuyu 已提交
484 485 486 487 488 489 490 491 492
        surface.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

S
shuyu 已提交
493 494 495
    /**
     * 亮度、进度、音频
     */
S
shuyu 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    @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;
                    mChangeVolume = false;
                    mChangePosition = false;
                    mBrightness = false;
S
shuyu 已提交
513
                    mFirstTouch = true;
S
shuyu 已提交
514

S
shuyu 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528
                    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) {
                                    mChangePosition = true;
                                    mDownPosition = getCurrentPositionWhenPlaying();
S
shuyu 已提交
529 530
                                    if (mVideoAllCallBack != null && isCurrentMediaListener()) {
                                        mVideoAllCallBack.onTouchScreenSeekPosition(mUrl, mObjects);
S
shuyu 已提交
531 532
                                    }
                                } else {
S
shuyu 已提交
533
                                    if (mFirstTouch) {
S
shuyu 已提交
534
                                        mBrightness = mDownX < mScreenWidth * 0.5f;
S
shuyu 已提交
535
                                        mFirstTouch = false;
S
shuyu 已提交
536 537 538 539
                                    }
                                    if (!mBrightness) {
                                        mChangeVolume = true;
                                        mGestureDownVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
S
shuyu 已提交
540 541
                                        if (mVideoAllCallBack != null && isCurrentMediaListener()) {
                                            mVideoAllCallBack.onTouchScreenSeekVolume(mUrl, mObjects);
S
shuyu 已提交
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
                                        }
                                    }
                                }
                            }
                        }
                    }
                    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) {
                        float percent = -deltaY / mScreenHeight;
                        onBrightnessSlide(percent);
                    }

                    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 已提交
580
                        mProgressBar.setProgress(progress);
S
shuyu 已提交
581 582
                    }
                    startProgressTimer();
S
shuyu 已提交
583
                    if (mVideoAllCallBack != null && isCurrentMediaListener()) {
S
shuyu 已提交
584
                        if (mIfCurrentIsFullscreen) {
S
shuyu 已提交
585
                            mVideoAllCallBack.onClickSeekbarFullscreen(mUrl, mObjects);
S
shuyu 已提交
586
                        } else {
S
shuyu 已提交
587
                            mVideoAllCallBack.onClickSeekbar(mUrl, mObjects);
S
shuyu 已提交
588 589 590 591
                        }
                    }
                    break;
            }
S
shuyu 已提交
592
        } else if (id == R.id.progress) {
S
shuyu 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
            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 已提交
610
                    mBrightnessData = -1f;
S
shuyu 已提交
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
                    break;
            }
        }

        return false;
    }


    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 已提交
644 645 646 647
    protected void onClickUiToggle() {

    }

S
shuyu 已提交
648 649 650 651 652 653 654 655 656 657

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

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

S
shuyu 已提交
658 659 660
    /***
     * 拖动进度条
     */
S
shuyu 已提交
661 662 663 664 665 666 667 668 669 670 671 672
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if (GSYVideoManager.instance().getMediaPlayer() != null && GSYVideoManager.instance().getMediaPlayer().isPlaying()) {
            int time = seekBar.getProgress() * getDuration() / 100;
            GSYVideoManager.instance().getMediaPlayer().seekTo(time);
        }
    }

    @Override
    public void onPrepared() {
        if (mCurrentState != CURRENT_STATE_PREPAREING) return;
        GSYVideoManager.instance().getMediaPlayer().start();
S
shuyu 已提交
673 674 675
        if (mSeekToInAdvance != -1) {
            GSYVideoManager.instance().getMediaPlayer().seekTo(mSeekToInAdvance);
            mSeekToInAdvance = -1;
S
shuyu 已提交
676 677 678 679 680 681 682
        }
        startProgressTimer();
        setStateAndUi(CURRENT_STATE_PLAYING);
    }

    @Override
    public void onAutoCompletion() {
S
shuyu 已提交
683
        if (mVideoAllCallBack != null && isCurrentMediaListener()) {
S
shuyu 已提交
684
            if (mIfCurrentIsFullscreen) {
S
shuyu 已提交
685
                mVideoAllCallBack.onAutoCompleteFullscreen(mUrl, mObjects);
S
shuyu 已提交
686
            } else {
S
shuyu 已提交
687
                mVideoAllCallBack.onAutoComplete(mUrl, mObjects);
S
shuyu 已提交
688 689 690
            }
        }
        setStateAndUi(CURRENT_STATE_AUTO_COMPLETE);
S
shuyu 已提交
691 692
        if (mTextureViewContainer.getChildCount() > 0) {
            mTextureViewContainer.removeAllViews();
S
shuyu 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
        }

        if (IF_FULLSCREEN_FROM_NORMAL) {//如果在进入全屏后播放完就初始化自己非全屏的控件
            IF_FULLSCREEN_FROM_NORMAL = false;
            GSYVideoManager.instance().lastListener().onAutoCompletion();
        }
        GSYVideoManager.instance().setLastListener(null);
        AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
        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 已提交
709 710
        if (mTextureViewContainer.getChildCount() > 0) {
            mTextureViewContainer.removeAllViews();
S
shuyu 已提交
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
        }

        if (IF_FULLSCREEN_FROM_NORMAL) {//如果在进入全屏后播放完就初始化自己非全屏的控件
            IF_FULLSCREEN_FROM_NORMAL = false;
            GSYVideoManager.instance().lastListener().onCompletion();
        }
        GSYVideoManager.instance().setListener(null);
        GSYVideoManager.instance().setLastListener(null);
        GSYVideoManager.instance().setCurrentVideoHeight(0);
        GSYVideoManager.instance().setCurrentVideoWidth(0);

        AudioManager mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
        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) {
            setTextAndProgress(percent);
        }
    }

    @Override
    public void onSeekComplete() {

    }

    @Override
    public void onError(int what, int extra) {
        if (what != 38 && what != -38) {
            setStateAndUi(CURRENT_STATE_ERROR);
        }
    }

    @Override
    public void onInfo(int what, int extra) {
        if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
            BACKUP_PLAYING_BUFFERING_STATE = mCurrentState;
            setStateAndUi(CURRENT_STATE_PLAYING_BUFFERING_START);
        } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
            if (BACKUP_PLAYING_BUFFERING_STATE != -1) {
                setStateAndUi(BACKUP_PLAYING_BUFFERING_STATE);
                BACKUP_PLAYING_BUFFERING_STATE = -1;
            }
        } else if (what == IMediaPlayer.MEDIA_INFO_VIDEO_ROTATION_CHANGED) {
S
shuyu 已提交
757 758 759
            mRotate = extra;
            if (mTextureView != null)
                mTextureView.setRotation(mRotate);
S
shuyu 已提交
760 761 762 763 764 765 766 767
        }
    }

    @Override
    public void onVideoSizeChanged() {
        int mVideoWidth = GSYVideoManager.instance().getCurrentVideoWidth();
        int mVideoHeight = GSYVideoManager.instance().getCurrentVideoHeight();
        if (mVideoWidth != 0 && mVideoHeight != 0) {
S
shuyu 已提交
768
            mTextureView.requestLayout();
S
shuyu 已提交
769 770 771 772 773
        }
    }

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

S
shuyu 已提交
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
    }

    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);
                    }
                });
            }
        }
    }

    protected int getCurrentPositionWhenPlaying() {
        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;
    }

    protected int getDuration() {
        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 已提交
841
            if (progress != 0) mProgressBar.setProgress(progress);
S
shuyu 已提交
842 843
        }
        if (secProgress > 95) secProgress = 100;
S
shuyu 已提交
844 845 846
        if (secProgress != 0) mProgressBar.setSecondaryProgress(secProgress);
        mCurrentTimeTextView.setText(CommonUtil.stringForTime(currentTime));
        mTotalTimeTextView.setText(CommonUtil.stringForTime(totalTime));
S
shuyu 已提交
847 848 849
    }

    protected void resetProgressAndTime() {
S
shuyu 已提交
850 851 852 853
        mProgressBar.setProgress(0);
        mProgressBar.setSecondaryProgress(0);
        mCurrentTimeTextView.setText(CommonUtil.stringForTime(0));
        mTotalTimeTextView.setText(CommonUtil.stringForTime(0));
S
shuyu 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
    }

    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();
        }
    }

877

S
shuyu 已提交
878 879 880 881 882
    protected boolean isCurrentMediaListener() {
        return GSYVideoManager.instance().listener() != null
                && GSYVideoManager.instance().listener() == this;
    }

S
shuyu 已提交
883

S
shuyu 已提交
884 885 886 887 888 889
    /**
     * 滑动改变亮度
     *
     * @param percent
     */
    private void onBrightnessSlide(float percent) {
S
shuyu 已提交
890 891 892 893 894 895
        if (mBrightnessData < 0) {
            mBrightnessData = ((Activity) (mContext)).getWindow().getAttributes().screenBrightness;
            if (mBrightnessData <= 0.00f) {
                mBrightnessData = 0.50f;
            } else if (mBrightnessData < 0.01f) {
                mBrightnessData = 0.01f;
S
shuyu 已提交
896 897
            }
        }
S
shuyu 已提交
898 899
        WindowManager.LayoutParams lpa = ((Activity) (mContext)).getWindow().getAttributes();
        lpa.screenBrightness = mBrightnessData + percent;
S
shuyu 已提交
900 901 902 903 904 905
        if (lpa.screenBrightness > 1.0f) {
            lpa.screenBrightness = 1.0f;
        } else if (lpa.screenBrightness < 0.01f) {
            lpa.screenBrightness = 0.01f;
        }
        showBrightnessDialog(lpa.screenBrightness);
S
shuyu 已提交
906
        ((Activity) (mContext)).getWindow().setAttributes(lpa);
S
shuyu 已提交
907 908 909 910 911 912
    }

    public boolean isTouchWiget() {
        return mIsTouchWiget;
    }

S
1.1.4  
shuyu 已提交
913 914 915
    /**
     * 是否可以滑动界面改变进度,声音等
     */
S
shuyu 已提交
916 917 918
    public void setIsTouchWiget(boolean isTouchWiget) {
        this.mIsTouchWiget = isTouchWiget;
    }
S
shuyu 已提交
919

S
shuyu 已提交
920 921 922
    /**
     * 获取播放按键
     */
S
shuyu 已提交
923
    public View getStartButton() {
S
shuyu 已提交
924
        return mStartButton;
S
shuyu 已提交
925 926
    }

S
shuyu 已提交
927 928 929
    /**
     * 获取全屏按键
     */
930
    public ImageView getFullscreenButton() {
S
shuyu 已提交
931
        return mFullscreenButton;
S
shuyu 已提交
932
    }
S
shuyu 已提交
933

S
shuyu 已提交
934 935 936
    /**
     * 获取返回按键
     */
S
shuyu 已提交
937
    public ImageView getBackButton() {
S
shuyu 已提交
938
        return mBackButton;
S
shuyu 已提交
939 940
    }

S
shuyu 已提交
941 942 943
    /**
     * 获取当前播放状态
     */
S
shuyu 已提交
944 945 946
    public int getCurrentState() {
        return mCurrentState;
    }
S
shuyu 已提交
947

948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
    /**
     * 播放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 已提交
969 970 971 972 973
    /**
     * 设置播放位置防止错位
     */
    public void setPlayPosition(int playPosition) {
        this.mPlayPosition = playPosition;
974 975
    }

S
shuyu 已提交
976 977
    /**
     * 退出全屏
S
shuyu 已提交
978 979
     *
     * @return 返回是否全屏
S
shuyu 已提交
980 981 982 983 984 985 986 987 988 989 990 991 992
     */
    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;
            GSYVideoManager.instance().lastListener().onBackFullscreen();
        }
        return backFrom;
    }


S
shuyu 已提交
993
}