1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
4
+ const DELAY = 500 ;
5
+
4
6
export default class Visualizer extends React . PureComponent {
5
7
rafId = null ;
6
8
timerId = null ;
@@ -17,17 +19,26 @@ export default class Visualizer extends React.PureComponent {
17
19
} ;
18
20
}
19
21
20
- componentDidMount ( ) {
21
- this . initiateEQ ( ) ;
22
- this . createVisualizer ( ) ;
23
- this . startDrawing ( ) ;
24
- }
25
22
26
- componentWillUnmount ( ) {
27
- this . stopDrawing ( ) ;
28
- this . state . eq . analyser . disconnect ( ) ;
29
- this . state . eq . src . disconnect ( ) ;
30
- this . state . eq . context . close ( ) ;
23
+ // In order to get around some mobile browser limitations,
24
+ // we can only generate a lot
25
+ // of the audio context stuff AFTER the audio has been triggered.
26
+ // We can't see it until
27
+ // then anyway so it makes no difference to desktop.
28
+ componentDidUpdate ( prevProps ) {
29
+ if ( prevProps . playing !== this . props . playing ) {
30
+ if ( this . props . playing ) {
31
+ this . initiateEQ ( ) ;
32
+ this . createVisualizer ( ) ;
33
+ this . startDrawing ( ) ;
34
+ } else {
35
+ // Workaround of componentWillUnmount to delay the clean up and achieve fadeout animation
36
+ setTimeout ( ( ) => {
37
+ this . stopDrawing ( ) ;
38
+ this . reset ( ) ;
39
+ } , DELAY )
40
+ }
41
+ }
31
42
}
32
43
33
44
initiateEQ ( ) {
@@ -53,6 +64,12 @@ export default class Visualizer extends React.PureComponent {
53
64
this . setState ( { eq } ) ;
54
65
}
55
66
67
+ reset = ( ) => {
68
+ this . rafId = null ;
69
+ this . state . eq . analyser . disconnect ( ) ;
70
+ this . state . eq . src . disconnect ( ) ;
71
+ }
72
+
56
73
/** *
57
74
* The equalizer bands available need to be updated
58
75
* constantly in order to ensure that the value for any
@@ -161,5 +178,6 @@ export default class Visualizer extends React.PureComponent {
161
178
}
162
179
163
180
Visualizer . propTypes = {
164
- player : PropTypes . object
181
+ player : PropTypes . object ,
182
+ playing : PropTypes . bool
165
183
} ;
0 commit comments