aboutsummaryrefslogtreecommitdiffstats
path: root/libs/fullscreenvideoview/src/main/java/com/github/rtoshiro/view/video/FullscreenVideoLayout.java
blob: f4113ce488d7e1ab4866e3ab2773ce40b8780418 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
 * Copyright (C) 2016 Toshiro Sugii
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.rtoshiro.view.video;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.Locale;

public class FullscreenVideoLayout extends FullscreenVideoView implements View.OnClickListener, SeekBar.OnSeekBarChangeListener, MediaPlayer.OnPreparedListener {

    /**
     * Log cat TAG name
     */
    private final static String TAG = "FullscreenVideoLayout";

    /**
     * RelativeLayout that contains all control related views
     */
    public View videoControlsView;

    /**
     * SeekBar reference (from videoControlsView)
     */
    protected SeekBar seekBar;

    /**
     * Reference to ImageButton play
     */
    protected ImageButton imgplay;

    /**
     * Reference to ImageButton fullscreen
     */
    protected ImageButton imgfullscreen;

    /**
     * Reference to TextView for elapsed time and total time
     */
    protected TextView textTotal, textElapsed;

    /**
     * Handler and Runnable to keep tracking on elapsed time
     */
    protected static final Handler TIME_THREAD = new Handler();
    protected Runnable updateTimeRunnable = new Runnable() {
        public void run() {
            updateCounter();

            TIME_THREAD.postDelayed(this, 200);
        }
    };

    public FullscreenVideoLayout(Context context) {
        super(context);
    }

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

    public FullscreenVideoLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void release() {
        super.release();
    }

    @Override
    protected void initObjects() {
        super.initObjects();

        if (this.videoControlsView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            this.videoControlsView = inflater.inflate(R.layout.view_videocontrols, this, false);
        }

        if (videoControlsView != null) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(ALIGN_PARENT_BOTTOM);
            addView(videoControlsView, params);

            this.seekBar = this.videoControlsView.findViewById(R.id.vcv_seekbar);
            this.imgfullscreen = this.videoControlsView.findViewById(R.id.vcv_img_fullscreen);
            this.imgplay = this.videoControlsView.findViewById(R.id.vcv_img_play);
            this.textTotal = this.videoControlsView.findViewById(R.id.vcv_txt_total);
            this.textElapsed = this.videoControlsView.findViewById(R.id.vcv_txt_elapsed);
        }

        if (this.imgplay != null)
            this.imgplay.setOnClickListener(this);
        if (this.imgfullscreen != null)
            this.imgfullscreen.setOnClickListener(this);
        if (this.seekBar != null)
            this.seekBar.setOnSeekBarChangeListener(this);

        // Start controls invisible. Make it visible when it is prepared
        if (this.videoControlsView != null)
            this.videoControlsView.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void releaseObjects() {
        super.releaseObjects();

        if (this.videoControlsView != null)
            removeView(this.videoControlsView);
    }

    protected void startCounter() {
        Log.d(TAG, "startCounter");

        TIME_THREAD.postDelayed(updateTimeRunnable, 200);
    }

    protected void stopCounter() {
        Log.d(TAG, "stopCounter");

        TIME_THREAD.removeCallbacks(updateTimeRunnable);
    }

    protected void updateCounter() {
        if (this.textElapsed == null)
            return;

        int elapsed = getCurrentPosition();
        // getCurrentPosition is a little bit buggy :(
        if (elapsed > 0 && elapsed < getDuration()) {
            seekBar.setProgress(elapsed);

            elapsed = Math.round(elapsed / 1000.f);
            long s = elapsed % 60;
            long m = (elapsed / 60) % 60;
            long h = (elapsed / (60 * 60)) % 24;

            if (h > 0)
                textElapsed.setText(String.format(Locale.US, "%d:%02d:%02d", h, m, s));
            else
                textElapsed.setText(String.format(Locale.US, "%02d:%02d", m, s));
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.d(TAG, "onCompletion");

        super.onCompletion(mp);
        stopCounter();
        updateControls();
        if (currentState != State.ERROR)
            updateCounter();
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        boolean result = super.onError(mp, what, extra);
        stopCounter();
        updateControls();
        return result;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (getCurrentState() == State.END) {
            Log.d(TAG, "onDetachedFromWindow END");
            stopCounter();
        }
    }

    @Override
    protected void tryToPrepare() {
        Log.d(TAG, "tryToPrepare");
        super.tryToPrepare();

        if (getCurrentState() == State.PREPARED || getCurrentState() == State.STARTED) {
            if (textElapsed != null && textTotal != null) {
                int total = getDuration();
                if (total > 0) {
                    seekBar.setMax(total);
                    seekBar.setProgress(0);

                    total = total / 1000;
                    long s = total % 60;
                    long m = (total / 60) % 60;
                    long h = (total / (60 * 60)) % 24;
                    if (h > 0) {
                        textElapsed.setText("00:00:00");
                        textTotal.setText(String.format(Locale.US, "%d:%02d:%02d", h, m, s));
                    } else {
                        textElapsed.setText("00:00");
                        textTotal.setText(String.format(Locale.US, "%02d:%02d", m, s));
                    }
                }
            }

            if (videoControlsView != null)
                videoControlsView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void start() throws IllegalStateException {
        Log.d(TAG, "start");

        if (!isPlaying()) {
            super.start();
            startCounter();
            updateControls();
        }
    }

    @Override
    public void pause() throws IllegalStateException {
        Log.d(TAG, "pause");

        if (isPlaying()) {
            stopCounter();
            super.pause();
            updateControls();
        }
    }

    @Override
    public void reset() {
        Log.d(TAG, "reset");

        super.reset();

        stopCounter();
        updateControls();
    }

    @Override
    public void stop() throws IllegalStateException {
        Log.d(TAG, "stop");

        super.stop();
        stopCounter();
        updateControls();
    }

    protected void updateControls() {
        if (imgplay == null) return;

        Drawable icon;
        if (getCurrentState() == State.STARTED) {
            icon = context.getResources().getDrawable(R.drawable.fvl_selector_pause);
        } else {
            icon = context.getResources().getDrawable(R.drawable.fvl_selector_play);
        }
        imgplay.setBackgroundDrawable(icon);
    }

    public void hideControls() {
        Log.d(TAG, "hideControls");
        if (videoControlsView != null) {
            videoControlsView.setVisibility(View.INVISIBLE);
        }
    }

    public void showControls() {
        Log.d(TAG, "showControls");
        if (videoControlsView != null) {
            videoControlsView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * Onclick action
     * Controls play button and fullscreen button.
     *
     * @param v View defined in XML
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.vcv_img_play) {
            if (isPlaying()) {
                pause();
            } else {
                start();
            }
        } else {
            setFullscreen(!isFullscreen());
        }
    }

    /**
     * SeekBar Listener
     */
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        Log.d(TAG, "onProgressChanged " + progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        stopCounter();
        Log.d(TAG, "onStartTrackingTouch");

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int progress = seekBar.getProgress();
        seekTo(progress);
        Log.d(TAG, "onStopTrackingTouch");

    }
}