@@ -13,11 +13,14 @@ import Question from '../Question';
13
13
import TriviaLoader from '../TriviaLoader' ;
14
14
import * as actions from '../../actions' ;
15
15
import { capitalizeFirstLetter } from '../../Utils' ;
16
- import { scale , moderateScale , verticalScale } from '../../Scaling' ;
16
+ import { scale , moderateScale } from '../../Scaling' ;
17
17
18
- const CORRECT_SOUND = require ( '../../../assets/sounds/correct.wav' ) ;
19
- const INCORRECT_SOUND = require ( '../../../assets/sounds/incorrect.wav' ) ;
20
- const TIMEOUT_SOUND = require ( '../../../assets/sounds/timeout.wav' ) ;
18
+ // Sound setup
19
+ const AVAILABLE_SOUNDS = {
20
+ correct : require ( '../../../assets/sounds/correct.wav' ) ,
21
+ incorrect : require ( '../../../assets/sounds/incorrect.wav' ) ,
22
+ timeout : require ( '../../../assets/sounds/timeout.wav' )
23
+ } ;
21
24
22
25
const COUNTDOWN_TIME = 10 ;
23
26
@@ -40,6 +43,7 @@ class TriviaGame extends React.Component {
40
43
answerType : 'correct' ,
41
44
fontLoaded : false ,
42
45
countdownTime : COUNTDOWN_TIME ,
46
+ soundController : null ,
43
47
} ;
44
48
}
45
49
@@ -56,15 +60,22 @@ class TriviaGame extends React.Component {
56
60
) ;
57
61
}
58
62
63
+ async componentDidMount ( ) {
64
+ // Preload sound controller
65
+ await Audio . setIsEnabledAsync ( true ) ;
66
+ this . setState ( {
67
+ soundController : new Audio . Sound ( )
68
+ } ) ;
69
+ }
70
+
59
71
/**
60
- * Play the correct sound based on answer status type
72
+ * Play the correct sound based on answer status type.
61
73
*/
62
74
playSound = async ( type ) => {
63
- await Audio . setIsEnabledAsync ( true ) ;
64
- const soundObject = new Audio . Sound ( ) ;
65
75
try {
66
- await soundObject . loadAsync ( ( type === 'correct' ) ? CORRECT_SOUND : ( type === 'incorrect' ) ? INCORRECT_SOUND : TIMEOUT_SOUND ) ;
67
- await soundObject . playAsync ( ) ;
76
+ await this . state . soundController . unloadAsync ( ) ;
77
+ await this . state . soundController . loadAsync ( AVAILABLE_SOUNDS [ type ] ) ;
78
+ await this . state . soundController . playAsync ( ) ;
68
79
} catch ( error ) {
69
80
// An error occurred!
70
81
console . log ( error ) ;
0 commit comments