admin
2020-07-14 7af22bf20c862c8ab2270cfeef8f3530f174ac9f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.wpc.library.videocomponents;
 
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.VideoView;
 
/**
 * This is the actual video player. It is the top-level content of the
 * activity's view hierarchy, going under the status bar and nav bar areas.
 */
public class SimpleVideoView extends VideoView implements
        View.OnSystemUiVisibilityChangeListener, View.OnClickListener,
        ActionBar.OnMenuVisibilityListener, MediaPlayer.OnPreparedListener,
        MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
    private Activity mActivity;
    private MediaController mMediaController;
    private GestureMediaController mGestureController;
    private boolean mAddedMenuListener;
    private int mLastSystemUiVis;
 
    public SimpleVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mActivity = (Activity) context;
        setOnSystemUiVisibilityChangeListener(this);
        setOnClickListener(this);
        setOnPreparedListener(this);
        setOnCompletionListener(this);
        setOnErrorListener(this);
    }
 
    public void setMediaController(MediaController mediaController) {
        // This called by the containing activity to supply the surrounding
        // state of the video player that it will interact with.
        mMediaController = mediaController;
        pause();
    }
    
    public void setGestureMediaController(GestureMediaController gestureController) {
        mGestureController = gestureController;
    }
 
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mActivity != null) {
            mAddedMenuListener = true;
            mActivity.getActionBar().addOnMenuVisibilityListener(this);
        }
    }
 
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mAddedMenuListener) {
            mActivity.getActionBar().removeOnMenuVisibilityListener(this);
        }
    }
 
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Detect when we go out of nav-hidden mode, to clear our state
        // back to having the full UI chrome up. Only do this when
        // the state is changing and nav is no longer hidden.
        int diff = mLastSystemUiVis ^ visibility;
        mLastSystemUiVis = visibility;
        if ((diff & SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0
                && (visibility & SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
            setNavVisibility(true);
        }
    }
 
    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        // When we become visible or invisible, play is paused.
        pause();
    }
 
    @Override
    public void onClick(View v) {
        // Clicking anywhere makes the navigation visible.
        setNavVisibility(true);
    }
    
    @Override
    @SuppressLint("ClickableViewAccessibility")
    public boolean onTouchEvent(MotionEvent ev) {
        boolean result = super.onTouchEvent(ev);
        if (mGestureController != null) {
            result = mGestureController.onTouchEvent(ev);
        }
        return result;
    }
 
    @Override
    public void onMenuVisibilityChanged(boolean isVisible) {
        setNavVisibility(true);
    }
 
    @Override
    public void onPrepared(MediaPlayer mp) {
        mMediaController.setEnabled(true);
    }
 
    @Override
    public void onCompletion(MediaPlayer mp) {
        pause();
    }
 
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        pause();
        return false;
    }
 
    @Override
    public void start() {
        super.start();
        setKeepScreenOn(true);
        setNavVisibility(true);
        mMediaController.show();
        mMediaController.refresh();
    }
 
    @Override
    public void pause() {
        super.pause();
        setKeepScreenOn(false);
        setNavVisibility(true);
        mMediaController.show();
        mMediaController.refresh();
    }
 
    void setNavVisibility(boolean visible) {
        int newVis = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | SYSTEM_UI_FLAG_LAYOUT_STABLE;
        if (!visible) {
            newVis |= SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_FULLSCREEN
                    | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }
 
        // Set the new desired visibility.
        setSystemUiVisibility(newVis);
    }
}