-
Notifications
You must be signed in to change notification settings - Fork 527
/
VideoPlayer.js
1450 lines (1335 loc) · 36.4 KB
/
VideoPlayer.js
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
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
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
995
996
997
998
999
1000
import React, {Component} from 'react';
import Video from 'react-native-video';
import {
TouchableWithoutFeedback,
TouchableHighlight,
ImageBackground,
PanResponder,
StyleSheet,
Animated,
SafeAreaView,
Easing,
Image,
View,
Text,
} from 'react-native';
import padStart from 'lodash/padStart';
export default class VideoPlayer extends Component {
static defaultProps = {
toggleResizeModeOnFullscreen: true,
controlAnimationTiming: 500,
doubleTapTime: 130,
playInBackground: false,
playWhenInactive: false,
resizeMode: 'contain',
isFullscreen: false,
showOnStart: true,
paused: false,
repeat: false,
muted: false,
volume: 1,
title: '',
rate: 1,
showTimeRemaining: true,
showHours: false,
};
constructor(props) {
super(props);
/**
* All of our values that are updated by the
* methods and listeners in this class
*/
this.state = {
// Video
resizeMode: this.props.resizeMode,
paused: this.props.paused,
muted: this.props.muted,
volume: this.props.volume,
rate: this.props.rate,
// Controls
isFullscreen:
this.props.isFullScreen || this.props.resizeMode === 'cover' || false,
showTimeRemaining: this.props.showTimeRemaining,
showHours: this.props.showHours,
volumeTrackWidth: 0,
volumeFillWidth: 0,
seekerFillWidth: 0,
showControls: this.props.showOnStart,
volumePosition: 0,
seekerPosition: 0,
volumeOffset: 0,
seekerOffset: 0,
seeking: false,
originallyPaused: false,
scrubbing: false,
loading: false,
currentTime: 0,
error: false,
duration: 0,
};
/**
* Any options that can be set at init.
*/
this.opts = {
playWhenInactive: this.props.playWhenInactive,
playInBackground: this.props.playInBackground,
repeat: this.props.repeat,
title: this.props.title,
};
/**
* Our app listeners and associated methods
*/
this.events = {
onError: this.props.onError || this._onError.bind(this),
onBack: this.props.onBack || this._onBack.bind(this),
onEnd: this.props.onEnd || this._onEnd.bind(this),
onScreenTouch: this._onScreenTouch.bind(this),
onEnterFullscreen: this.props.onEnterFullscreen,
onExitFullscreen: this.props.onExitFullscreen,
onShowControls: this.props.onShowControls,
onHideControls: this.props.onHideControls,
onLoadStart: this._onLoadStart.bind(this),
onProgress: this._onProgress.bind(this),
onSeek: this._onSeek.bind(this),
onLoad: this._onLoad.bind(this),
onPause: this.props.onPause,
onPlay: this.props.onPlay,
};
/**
* Functions used throughout the application
*/
this.methods = {
toggleFullscreen: this._toggleFullscreen.bind(this),
togglePlayPause: this._togglePlayPause.bind(this),
toggleControls: this._toggleControls.bind(this),
toggleTimer: this._toggleTimer.bind(this),
};
/**
* Player information
*/
this.player = {
controlTimeoutDelay: this.props.controlTimeout || 15000,
volumePanResponder: PanResponder,
seekPanResponder: PanResponder,
controlTimeout: null,
tapActionTimeout: null,
volumeWidth: 150,
iconOffset: 0,
seekerWidth: 0,
ref: Video,
scrubbingTimeStep: this.props.scrubbing || 0,
tapAnywhereToPause: this.props.tapAnywhereToPause,
};
/**
* Various animations
*/
const initialValue = this.props.showOnStart ? 1 : 0;
this.animations = {
bottomControl: {
marginBottom: new Animated.Value(0),
opacity: new Animated.Value(initialValue),
},
topControl: {
marginTop: new Animated.Value(0),
opacity: new Animated.Value(initialValue),
},
video: {
opacity: new Animated.Value(1),
},
loader: {
rotate: new Animated.Value(0),
MAX_VALUE: 360,
},
};
/**
* Various styles that be added...
*/
this.styles = {
videoStyle: this.props.videoStyle || {},
containerStyle: this.props.style || {},
};
}
componentDidUpdate = prevProps => {
const {isFullscreen} = this.props;
if (prevProps.isFullscreen !== isFullscreen) {
this.setState({
isFullscreen,
});
}
};
/**
| -------------------------------------------------------
| Events
| -------------------------------------------------------
|
| These are the events that the <Video> component uses
| and can be overridden by assigning it as a prop.
| It is suggested that you override onEnd.
|
*/
/**
* When load starts we display a loading icon
* and show the controls.
*/
_onLoadStart() {
let state = this.state;
state.loading = true;
this.loadAnimation();
this.setState(state);
if (typeof this.props.onLoadStart === 'function') {
this.props.onLoadStart(...arguments);
}
}
/**
* When load is finished we hide the load icon
* and hide the controls. We also set the
* video duration.
*
* @param {object} data The video meta data
*/
_onLoad(data = {}) {
let state = this.state;
state.duration = data.duration;
state.loading = false;
this.setState(state);
if (state.showControls) {
this.setControlTimeout();
}
if (typeof this.props.onLoad === 'function') {
this.props.onLoad(...arguments);
}
}
/**
* For onprogress we fire listeners that
* update our seekbar and timer.
*
* @param {object} data The video meta data
*/
_onProgress(data = {}) {
let state = this.state;
if (!state.scrubbing) {
state.currentTime = data.currentTime;
if (!state.seeking) {
const position = this.calculateSeekerPosition();
this.setSeekerPosition(position);
}
if (typeof this.props.onProgress === 'function') {
this.props.onProgress(...arguments);
}
this.setState(state);
}
}
/**
* For onSeek we clear scrubbing if set.
*
* @param {object} data The video meta data
*/
_onSeek(data = {}) {
let state = this.state;
if (state.scrubbing) {
state.scrubbing = false;
state.currentTime = data.currentTime;
// Seeking may be false here if the user released the seek bar while the player was still processing
// the last seek command. In this case, perform the steps that have been postponed.
if (!state.seeking) {
this.setControlTimeout();
state.paused = state.originallyPaused;
}
this.setState(state);
}
}
/**
* It is suggested that you override this
* command so your app knows what to do.
* Either close the video or go to a
* new page.
*/
_onEnd() {}
/**
* Set the error state to true which then
* changes our renderError function
*
* @param {object} err Err obj returned from <Video> component
*/
_onError(err) {
let state = this.state;
state.error = true;
state.loading = false;
this.setState(state);
}
/**
* This is a single and double tap listener
* when the user taps the screen anywhere.
* One tap toggles controls and/or toggles pause,
* two toggles fullscreen mode.
*/
_onScreenTouch() {
if (this.player.tapActionTimeout) {
clearTimeout(this.player.tapActionTimeout);
this.player.tapActionTimeout = 0;
this.methods.toggleFullscreen();
const state = this.state;
if (state.showControls) {
this.resetControlTimeout();
}
} else {
this.player.tapActionTimeout = setTimeout(() => {
const state = this.state;
if (this.player.tapAnywhereToPause && state.showControls) {
this.methods.togglePlayPause();
this.resetControlTimeout();
} else {
this.methods.toggleControls();
}
this.player.tapActionTimeout = 0;
}, this.props.doubleTapTime);
}
}
/**
| -------------------------------------------------------
| Methods
| -------------------------------------------------------
|
| These are all of our functions that interact with
| various parts of the class. Anything from
| calculating time remaining in a video
| to handling control operations.
|
*/
/**
* Set a timeout when the controls are shown
* that hides them after a length of time.
* Default is 15s
*/
setControlTimeout() {
this.player.controlTimeout = setTimeout(() => {
this._hideControls();
}, this.player.controlTimeoutDelay);
}
/**
* Clear the hide controls timeout.
*/
clearControlTimeout() {
clearTimeout(this.player.controlTimeout);
}
/**
* Reset the timer completely
*/
resetControlTimeout() {
this.clearControlTimeout();
this.setControlTimeout();
}
/**
* Animation to hide controls. We fade the
* display to 0 then move them off the
* screen so they're not interactable
*/
hideControlAnimation() {
Animated.parallel([
Animated.timing(this.animations.topControl.opacity, {
toValue: 0,
duration: this.props.controlAnimationTiming,
useNativeDriver: false,
}),
Animated.timing(this.animations.topControl.marginTop, {
toValue: -100,
duration: this.props.controlAnimationTiming,
useNativeDriver: false,
}),
Animated.timing(this.animations.bottomControl.opacity, {
toValue: 0,
duration: this.props.controlAnimationTiming,
useNativeDriver: false,
}),
Animated.timing(this.animations.bottomControl.marginBottom, {
toValue: -100,
duration: this.props.controlAnimationTiming,
useNativeDriver: false,
}),
]).start();
}
/**
* Animation to show controls...opposite of
* above...move onto the screen and then
* fade in.
*/
showControlAnimation() {
Animated.parallel([
Animated.timing(this.animations.topControl.opacity, {
toValue: 1,
useNativeDriver: false,
duration: this.props.controlAnimationTiming,
}),
Animated.timing(this.animations.topControl.marginTop, {
toValue: 0,
useNativeDriver: false,
duration: this.props.controlAnimationTiming,
}),
Animated.timing(this.animations.bottomControl.opacity, {
toValue: 1,
useNativeDriver: false,
duration: this.props.controlAnimationTiming,
}),
Animated.timing(this.animations.bottomControl.marginBottom, {
toValue: 0,
useNativeDriver: false,
duration: this.props.controlAnimationTiming,
}),
]).start();
}
/**
* Loop animation to spin loader icon. If not loading then stop loop.
*/
loadAnimation() {
if (this.state.loading) {
Animated.sequence([
Animated.timing(this.animations.loader.rotate, {
toValue: this.animations.loader.MAX_VALUE,
duration: 1500,
easing: Easing.linear,
useNativeDriver: false,
}),
Animated.timing(this.animations.loader.rotate, {
toValue: 0,
duration: 0,
easing: Easing.linear,
useNativeDriver: false,
}),
]).start(this.loadAnimation.bind(this));
}
}
/**
* Function to hide the controls. Sets our
* state then calls the animation.
*/
_hideControls() {
if (this.mounted) {
let state = this.state;
state.showControls = false;
this.hideControlAnimation();
typeof this.events.onHideControls === 'function' &&
this.events.onHideControls();
this.setState(state);
}
}
/**
* Function to toggle controls based on
* current state.
*/
_toggleControls() {
let state = this.state;
state.showControls = !state.showControls;
if (state.showControls) {
this.showControlAnimation();
this.setControlTimeout();
typeof this.events.onShowControls === 'function' &&
this.events.onShowControls();
} else {
this.hideControlAnimation();
this.clearControlTimeout();
typeof this.events.onHideControls === 'function' &&
this.events.onHideControls();
}
this.setState(state);
}
/**
* Toggle fullscreen changes resizeMode on
* the <Video> component then updates the
* isFullscreen state.
*/
_toggleFullscreen() {
let state = this.state;
state.isFullscreen = !state.isFullscreen;
if (this.props.toggleResizeModeOnFullscreen) {
state.resizeMode = state.isFullscreen === true ? 'cover' : 'contain';
}
if (state.isFullscreen) {
typeof this.events.onEnterFullscreen === 'function' &&
this.events.onEnterFullscreen();
} else {
typeof this.events.onExitFullscreen === 'function' &&
this.events.onExitFullscreen();
}
this.setState(state);
}
/**
* Toggle playing state on <Video> component
*/
_togglePlayPause() {
let state = this.state;
state.paused = !state.paused;
if (state.paused) {
typeof this.events.onPause === 'function' && this.events.onPause();
} else {
typeof this.events.onPlay === 'function' && this.events.onPlay();
}
this.setState(state);
}
/**
* Toggle between showing time remaining or
* video duration in the timer control
*/
_toggleTimer() {
let state = this.state;
state.showTimeRemaining = !state.showTimeRemaining;
this.setState(state);
}
/**
* The default 'onBack' function pops the navigator
* and as such the video player requires a
* navigator prop by default.
*/
_onBack() {
if (this.props.navigator && this.props.navigator.pop) {
this.props.navigator.pop();
} else {
console.warn(
'Warning: _onBack requires navigator property to function. Either modify the onBack prop or pass a navigator prop',
);
}
}
/**
* Calculate the time to show in the timer area
* based on if they want to see time remaining
* or duration. Formatted to look as 00:00.
*/
calculateTime() {
if (this.state.showTimeRemaining) {
const time = this.state.duration - this.state.currentTime;
return `-${this.formatTime(time)}`;
}
return this.formatTime(this.state.currentTime);
}
/**
* Format a time string as mm:ss
*
* @param {int} time time in milliseconds
* @return {string} formatted time string in mm:ss format
*/
formatTime(time = 0) {
const symbol = this.state.showRemainingTime ? '-' : '';
time = Math.min(Math.max(time, 0), this.state.duration);
if (!this.state.showHours) {
const formattedMinutes = padStart(Math.floor(time / 60).toFixed(0), 2, 0);
const formattedSeconds = padStart(Math.floor(time % 60).toFixed(0), 2, 0);
return `${symbol}${formattedMinutes}:${formattedSeconds}`;
}
const formattedHours = padStart(Math.floor(time / 3600).toFixed(0), 2, 0);
const formattedMinutes = padStart(
(Math.floor(time / 60) % 60).toFixed(0),
2,
0,
);
const formattedSeconds = padStart(Math.floor(time % 60).toFixed(0), 2, 0);
return `${symbol}${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
}
/**
* Set the position of the seekbar's components
* (both fill and handle) according to the
* position supplied.
*
* @param {float} position position in px of seeker handle}
*/
setSeekerPosition(position = 0) {
let state = this.state;
position = this.constrainToSeekerMinMax(position);
state.seekerFillWidth = position;
state.seekerPosition = position;
if (!state.seeking) {
state.seekerOffset = position;
}
this.setState(state);
}
/**
* Constrain the location of the seeker to the
* min/max value based on how big the
* seeker is.
*
* @param {float} val position of seeker handle in px
* @return {float} constrained position of seeker handle in px
*/
constrainToSeekerMinMax(val = 0) {
if (val <= 0) {
return 0;
} else if (val >= this.player.seekerWidth) {
return this.player.seekerWidth;
}
return val;
}
/**
* Calculate the position that the seeker should be
* at along its track.
*
* @return {float} position of seeker handle in px based on currentTime
*/
calculateSeekerPosition() {
const percent = this.state.currentTime / this.state.duration;
return this.player.seekerWidth * percent;
}
/**
* Return the time that the video should be at
* based on where the seeker handle is.
*
* @return {float} time in ms based on seekerPosition.
*/
calculateTimeFromSeekerPosition() {
const percent = this.state.seekerPosition / this.player.seekerWidth;
return this.state.duration * percent;
}
/**
* Seek to a time in the video.
*
* @param {float} time time to seek to in ms
*/
seekTo(time = 0) {
let state = this.state;
state.currentTime = time;
this.player.ref.seek(time);
this.setState(state);
}
/**
* Set the position of the volume slider
*
* @param {float} position position of the volume handle in px
*/
setVolumePosition(position = 0) {
let state = this.state;
position = this.constrainToVolumeMinMax(position);
state.volumePosition = position + this.player.iconOffset;
state.volumeFillWidth = position;
state.volumeTrackWidth = this.player.volumeWidth - state.volumeFillWidth;
if (state.volumeFillWidth < 0) {
state.volumeFillWidth = 0;
}
if (state.volumeTrackWidth > 150) {
state.volumeTrackWidth = 150;
}
this.setState(state);
}
/**
* Constrain the volume bar to the min/max of
* its track's width.
*
* @param {float} val position of the volume handle in px
* @return {float} contrained position of the volume handle in px
*/
constrainToVolumeMinMax(val = 0) {
if (val <= 0) {
return 0;
} else if (val >= this.player.volumeWidth + 9) {
return this.player.volumeWidth + 9;
}
return val;
}
/**
* Get the volume based on the position of the
* volume object.
*
* @return {float} volume level based on volume handle position
*/
calculateVolumeFromVolumePosition() {
return this.state.volumePosition / this.player.volumeWidth;
}
/**
* Get the position of the volume handle based
* on the volume
*
* @return {float} volume handle position in px based on volume
*/
calculateVolumePositionFromVolume() {
return this.player.volumeWidth * this.state.volume;
}
/**
| -------------------------------------------------------
| React Component functions
| -------------------------------------------------------
|
| Here we're initializing our listeners and getting
| the component ready using the built-in React
| Component methods
|
*/
/**
* Before mounting, init our seekbar and volume bar
* pan responders.
*/
UNSAFE_componentWillMount() {
this.initSeekPanResponder();
this.initVolumePanResponder();
}
/**
* To allow basic playback management from the outside
* we have to handle possible props changes to state changes
*/
UNSAFE_componentWillReceiveProps(nextProps) {
if (this.state.paused !== nextProps.paused) {
this.setState({
paused: nextProps.paused,
});
}
if (this.styles.videoStyle !== nextProps.videoStyle) {
this.styles.videoStyle = nextProps.videoStyle;
}
if (this.styles.containerStyle !== nextProps.style) {
this.styles.containerStyle = nextProps.style;
}
}
/**
* Upon mounting, calculate the position of the volume
* bar based on the volume property supplied to it.
*/
componentDidMount() {
const position = this.calculateVolumePositionFromVolume();
let state = this.state;
this.setVolumePosition(position);
state.volumeOffset = position;
this.mounted = true;
this.setState(state);
}
/**
* When the component is about to unmount kill the
* timeout less it fire in the prev/next scene
*/
componentWillUnmount() {
this.mounted = false;
this.clearControlTimeout();
}
/**
* Get our seekbar responder going
*/
initSeekPanResponder() {
this.player.seekPanResponder = PanResponder.create({
// Ask to be the responder.
onStartShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
/**
* When we start the pan tell the machine that we're
* seeking. This stops it from updating the seekbar
* position in the onProgress listener.
*/
onPanResponderGrant: (evt, gestureState) => {
let state = this.state;
this.clearControlTimeout();
const position = evt.nativeEvent.locationX;
this.setSeekerPosition(position);
state.seeking = true;
state.originallyPaused = state.paused;
state.scrubbing = false;
if (this.player.scrubbingTimeStep > 0) {
state.paused = true;
}
this.setState(state);
},
/**
* When panning, update the seekbar position, duh.
*/
onPanResponderMove: (evt, gestureState) => {
const position = this.state.seekerOffset + gestureState.dx;
this.setSeekerPosition(position);
let state = this.state;
if (
this.player.scrubbingTimeStep > 0 &&
!state.loading &&
!state.scrubbing
) {
const time = this.calculateTimeFromSeekerPosition();
const timeDifference = Math.abs(state.currentTime - time) * 1000;
if (
time < state.duration &&
timeDifference >= this.player.scrubbingTimeStep
) {
state.scrubbing = true;
this.setState(state);
setTimeout(() => {
this.player.ref.seek(time, this.player.scrubbingTimeStep);
}, 1);
}
}
},
/**
* On release we update the time and seek to it in the video.
* If you seek to the end of the video we fire the
* onEnd callback
*/
onPanResponderRelease: (evt, gestureState) => {
const time = this.calculateTimeFromSeekerPosition();
let state = this.state;
if (time >= state.duration && !state.loading) {
state.paused = true;
this.events.onEnd();
} else if (state.scrubbing) {
state.seeking = false;
} else {
this.seekTo(time);
this.setControlTimeout();
state.paused = state.originallyPaused;
state.seeking = false;
}
this.setState(state);
},
});
}
/**
* Initialize the volume pan responder.
*/
initVolumePanResponder() {
this.player.volumePanResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onPanResponderGrant: (evt, gestureState) => {
this.clearControlTimeout();
},
/**
* Update the volume as we change the position.
* If we go to 0 then turn on the mute prop
* to avoid that weird static-y sound.
*/
onPanResponderMove: (evt, gestureState) => {
let state = this.state;
const position = this.state.volumeOffset + gestureState.dx;
this.setVolumePosition(position);
state.volume = this.calculateVolumeFromVolumePosition();
if (state.volume <= 0) {
state.muted = true;
} else {
state.muted = false;
}
this.setState(state);
},
/**
* Update the offset...
*/
onPanResponderRelease: (evt, gestureState) => {
let state = this.state;
state.volumeOffset = state.volumePosition;
this.setControlTimeout();
this.setState(state);
},
});
}
/**
| -------------------------------------------------------
| Rendering
| -------------------------------------------------------
|
| This section contains all of our render methods.
| In addition to the typical React render func
| we also have all the render methods for
| the controls.
|
*/
/**
* Standard render control function that handles
* everything except the sliders. Adds a
* consistent <TouchableHighlight>
* wrapper and styling.
*/
renderControl(children, callback, style = {}) {
return (
<TouchableHighlight
underlayColor="transparent"
activeOpacity={0.3}
onPress={() => {
this.resetControlTimeout();
callback();
}}
style={[styles.controls.control, style]}>
{children}
</TouchableHighlight>
);
}
/**
* Renders an empty control, used to disable a control without breaking the view layout.
*/
renderNullControl() {
return <View style={[styles.controls.control]} />;
}
/**
* Groups the top bar controls together in an animated
* view and spaces them out.
*/
renderTopControls() {
const backControl = this.props.disableBack
? this.renderNullControl()
: this.renderBack();
const volumeControl = this.props.disableVolume
? this.renderNullControl()
: this.renderVolume();
const fullscreenControl = this.props.disableFullscreen
? this.renderNullControl()
: this.renderFullscreen();
return (
<Animated.View
style={[
styles.controls.top,
{
opacity: this.animations.topControl.opacity,
marginTop: this.animations.topControl.marginTop,
},
]}>
<ImageBackground
source={require('./assets/img/top-vignette.png')}
style={[styles.controls.column]}
imageStyle={[styles.controls.vignette]}>
<SafeAreaView style={styles.controls.topControlGroup}>
{backControl}
<View style={styles.controls.pullRight}>
{volumeControl}
{fullscreenControl}
</View>
</SafeAreaView>
</ImageBackground>
</Animated.View>
);
}
/**
* Back button control
*/
renderBack() {
return this.renderControl(
<Image
source={require('./assets/img/back.png')}
style={styles.controls.back}
/>,
this.events.onBack,
styles.controls.back,
);
}