/
MovieView.java
509 lines (452 loc) · 17.5 KB
/
MovieView.java
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
377
378
379
380
381
382
383
384
385
386
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
* Copyright (C) 2017 The Android Open Source Project
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.example.android.pictureinpicture.widget;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import android.transition.TransitionManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import com.example.android.pictureinpicture.R;
import java.io.IOException;
import java.lang.ref.WeakReference;
/**
* Provides video playback. There is nothing directly related to Picture-in-Picture here.
*
* <p>This is similar to {@link android.widget.VideoView}, but it comes with a custom control
* (play/pause, fast forward, and fast rewind).
*/
public class MovieView extends RelativeLayout {
/** Monitors all events related to {@link MovieView}. */
public abstract static class MovieListener {
/** Called when the video is started or resumed. */
public void onMovieStarted() {}
/** Called when the video is paused or finished. */
public void onMovieStopped() {}
/** Called when this view should be minimized. */
public void onMovieMinimized() {}
}
private static final String TAG = "MovieView";
/** The amount of time we are stepping forward or backward for fast-forward and fast-rewind. */
private static final int FAST_FORWARD_REWIND_INTERVAL = 5000; // ms
/** The amount of time until we fade out the controls. */
private static final int TIMEOUT_CONTROLS = 3000; // ms
/** Shows the video playback. */
private final SurfaceView mSurfaceView;
// Controls
private final ImageButton mToggle;
private final View mShade;
private final ImageButton mFastForward;
private final ImageButton mFastRewind;
private final ImageButton mMinimize;
/** This plays the video. This will be null when no video is set. */
MediaPlayer mMediaPlayer;
/** The resource ID for the video to play. */
@RawRes private int mVideoResourceId;
/** The title of the video */
private String mTitle;
/** Whether we adjust our view bounds or we fill the remaining area with black bars */
private boolean mAdjustViewBounds;
/** Handles timeout for media controls. */
TimeoutHandler mTimeoutHandler;
/** The listener for all the events we publish. */
MovieListener mMovieListener;
private int mSavedCurrentPosition;
public MovieView(Context context) {
this(context, null);
}
public MovieView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MovieView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setBackgroundColor(Color.BLACK);
// Inflate the content
inflate(context, R.layout.view_movie, this);
mSurfaceView = findViewById(R.id.surface);
mShade = findViewById(R.id.shade);
mToggle = findViewById(R.id.toggle);
mFastForward = findViewById(R.id.fast_forward);
mFastRewind = findViewById(R.id.fast_rewind);
mMinimize = findViewById(R.id.minimize);
final TypedArray attributes =
context.obtainStyledAttributes(
attrs,
R.styleable.MovieView,
defStyleAttr,
R.style.Widget_PictureInPicture_MovieView);
setVideoResourceId(attributes.getResourceId(R.styleable.MovieView_android_src, 0));
setAdjustViewBounds(
attributes.getBoolean(R.styleable.MovieView_android_adjustViewBounds, false));
setTitle(attributes.getString(R.styleable.MovieView_android_title));
attributes.recycle();
// Bind view events
final OnClickListener listener =
new OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.surface:
toggleControls();
break;
case R.id.toggle:
toggle();
break;
case R.id.fast_forward:
fastForward();
break;
case R.id.fast_rewind:
fastRewind();
break;
case R.id.minimize:
if (mMovieListener != null) {
mMovieListener.onMovieMinimized();
}
break;
}
// Start or reset the timeout to hide controls
if (mMediaPlayer != null) {
if (mTimeoutHandler == null) {
mTimeoutHandler = new TimeoutHandler(MovieView.this);
}
mTimeoutHandler.removeMessages(TimeoutHandler.MESSAGE_HIDE_CONTROLS);
if (mMediaPlayer.isPlaying()) {
mTimeoutHandler.sendEmptyMessageDelayed(
TimeoutHandler.MESSAGE_HIDE_CONTROLS, TIMEOUT_CONTROLS);
}
}
}
};
mSurfaceView.setOnClickListener(listener);
mToggle.setOnClickListener(listener);
mFastForward.setOnClickListener(listener);
mFastRewind.setOnClickListener(listener);
mMinimize.setOnClickListener(listener);
// Prepare video playback
mSurfaceView
.getHolder()
.addCallback(
new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
openVideo(holder.getSurface());
}
@Override
public void surfaceChanged(
SurfaceHolder holder, int format, int width, int height) {
// Do nothing
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mMediaPlayer != null) {
mSavedCurrentPosition = mMediaPlayer.getCurrentPosition();
}
closeVideo();
}
});
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mMediaPlayer != null) {
final int videoWidth = mMediaPlayer.getVideoWidth();
final int videoHeight = mMediaPlayer.getVideoHeight();
if (videoWidth != 0 && videoHeight != 0) {
final float aspectRatio = (float) videoHeight / videoWidth;
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int height = MeasureSpec.getSize(heightMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (mAdjustViewBounds) {
if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) {
super.onMeasure(
widthMeasureSpec,
MeasureSpec.makeMeasureSpec(
(int) (width * aspectRatio), MeasureSpec.EXACTLY));
} else if (widthMode != MeasureSpec.EXACTLY
&& heightMode == MeasureSpec.EXACTLY) {
super.onMeasure(
MeasureSpec.makeMeasureSpec(
(int) (height / aspectRatio), MeasureSpec.EXACTLY),
heightMeasureSpec);
} else {
super.onMeasure(
widthMeasureSpec,
MeasureSpec.makeMeasureSpec(
(int) (width * aspectRatio), MeasureSpec.EXACTLY));
}
} else {
final float viewRatio = (float) height / width;
if (aspectRatio > viewRatio) {
int padding = (int) ((width - height / aspectRatio) / 2);
setPadding(padding, 0, padding, 0);
} else {
int padding = (int) ((height - width * aspectRatio) / 2);
setPadding(0, padding, 0, padding);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
return;
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDetachedFromWindow() {
if (mTimeoutHandler != null) {
mTimeoutHandler.removeMessages(TimeoutHandler.MESSAGE_HIDE_CONTROLS);
mTimeoutHandler = null;
}
super.onDetachedFromWindow();
}
/**
* Sets the listener to monitor movie events.
*
* @param movieListener The listener to be set.
*/
public void setMovieListener(@Nullable MovieListener movieListener) {
mMovieListener = movieListener;
}
/**
* Sets the title of the video to play.
*
* @param title of the video.
*/
public void setTitle(String title) {
this.mTitle = title;
}
/**
* The title of the video to play.
*
* @return title of the video.
*/
public String getTitle() {
return mTitle;
}
/**
* The raw resource id of the video to play.
*
* @return ID of the video resource.
*/
public int getVideoResourceId() {
return mVideoResourceId;
}
/**
* Sets the raw resource ID of video to play.
*
* @param id The raw resource ID.
*/
public void setVideoResourceId(@RawRes int id) {
if (id == mVideoResourceId) {
return;
}
mVideoResourceId = id;
Surface surface = mSurfaceView.getHolder().getSurface();
if (surface != null && surface.isValid()) {
closeVideo();
openVideo(surface);
}
}
public void setAdjustViewBounds(boolean adjustViewBounds) {
if (mAdjustViewBounds == adjustViewBounds) {
return;
}
mAdjustViewBounds = adjustViewBounds;
if (adjustViewBounds) {
setBackground(null);
} else {
setBackgroundColor(Color.BLACK);
}
requestLayout();
}
/** Shows all the controls. */
public void showControls() {
TransitionManager.beginDelayedTransition(this);
mShade.setVisibility(View.VISIBLE);
mToggle.setVisibility(View.VISIBLE);
mFastForward.setVisibility(View.VISIBLE);
mFastRewind.setVisibility(View.VISIBLE);
mMinimize.setVisibility(View.VISIBLE);
}
/** Hides all the controls. */
public void hideControls() {
TransitionManager.beginDelayedTransition(this);
mShade.setVisibility(View.INVISIBLE);
mToggle.setVisibility(View.INVISIBLE);
mFastForward.setVisibility(View.INVISIBLE);
mFastRewind.setVisibility(View.INVISIBLE);
mMinimize.setVisibility(View.INVISIBLE);
}
/** Fast-forward the video. */
public void fastForward() {
if (mMediaPlayer == null) {
return;
}
mMediaPlayer.seekTo(mMediaPlayer.getCurrentPosition() + FAST_FORWARD_REWIND_INTERVAL);
}
/** Fast-rewind the video. */
public void fastRewind() {
if (mMediaPlayer == null) {
return;
}
mMediaPlayer.seekTo(mMediaPlayer.getCurrentPosition() - FAST_FORWARD_REWIND_INTERVAL);
}
/**
* Returns the current position of the video. If the the player has not been created, then
* assumes the beginning of the video.
*
* @return The current position of the video.
*/
public int getCurrentPosition() {
if (mMediaPlayer == null) {
return 0;
}
return mMediaPlayer.getCurrentPosition();
}
public boolean isPlaying() {
return mMediaPlayer != null && mMediaPlayer.isPlaying();
}
public void play() {
if (mMediaPlayer == null) {
return;
}
mMediaPlayer.start();
adjustToggleState();
setKeepScreenOn(true);
if (mMovieListener != null) {
mMovieListener.onMovieStarted();
}
}
public void pause() {
if (mMediaPlayer == null) {
adjustToggleState();
return;
}
mMediaPlayer.pause();
adjustToggleState();
setKeepScreenOn(false);
if (mMovieListener != null) {
mMovieListener.onMovieStopped();
}
}
void openVideo(Surface surface) {
if (mVideoResourceId == 0) {
return;
}
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setSurface(surface);
startVideo();
}
/** Restarts playback of the video. */
public void startVideo() {
mMediaPlayer.reset();
try (AssetFileDescriptor fd = getResources().openRawResourceFd(mVideoResourceId)) {
mMediaPlayer.setDataSource(fd);
mMediaPlayer.setOnPreparedListener(
new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// Adjust the aspect ratio of this view
requestLayout();
if (mSavedCurrentPosition > 0) {
mediaPlayer.seekTo(mSavedCurrentPosition);
mSavedCurrentPosition = 0;
} else {
// Start automatically
play();
}
}
});
mMediaPlayer.setOnCompletionListener(
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
adjustToggleState();
setKeepScreenOn(false);
if (mMovieListener != null) {
mMovieListener.onMovieStopped();
}
}
});
mMediaPlayer.prepare();
} catch (IOException e) {
Log.e(TAG, "Failed to open video", e);
}
}
void closeVideo() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
void toggle() {
if (mMediaPlayer == null) {
return;
}
if (mMediaPlayer.isPlaying()) {
pause();
} else {
play();
}
}
void toggleControls() {
if (mShade.getVisibility() == View.VISIBLE) {
hideControls();
} else {
showControls();
}
}
void adjustToggleState() {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mToggle.setContentDescription(getResources().getString(R.string.pause));
mToggle.setImageResource(R.drawable.ic_pause_64dp);
} else {
mToggle.setContentDescription(getResources().getString(R.string.play));
mToggle.setImageResource(R.drawable.ic_play_arrow_64dp);
}
}
private static class TimeoutHandler extends Handler {
static final int MESSAGE_HIDE_CONTROLS = 1;
private final WeakReference<MovieView> mMovieViewRef;
TimeoutHandler(MovieView view) {
mMovieViewRef = new WeakReference<>(view);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_HIDE_CONTROLS:
MovieView movieView = mMovieViewRef.get();
if (movieView != null) {
movieView.hideControls();
}
break;
default:
super.handleMessage(msg);
}
}
}
}