1
1
import { View , ScrollView , StyleSheet , FlatList } from "react-native" ;
2
- import { useState , useEffect , useMemo } from 'react' ;
2
+ import { useState , useEffect , useMemo , useRef } from 'react' ;
3
3
import { SafeAreaView } from "react-native-safe-area-context" ;
4
4
import { Accelerometer } from "expo-sensors" ;
5
+ import { Audio } from "expo-av" ;
5
6
6
7
import DicePicker from "@/components/DicePicker" ;
7
8
import DiceList from "@/components/DiceList" ;
@@ -14,8 +15,14 @@ export default function Index() {
14
15
const [ key , setKey ] = useState ( 0 ) ;
15
16
const [ rolling , setRolling ] = useState ( false ) ;
16
17
const [ isModalVisible , setModalVisible ] = useState ( false ) ;
18
+ const rollSfx = useRef ( null ) ;
19
+
20
+ const rollDice = async ( ) => {
21
+ if ( rollSfx . current ) {
22
+ await rollSfx . current . sound . setPositionAsync ( 0 ) ;
23
+ await rollSfx . current . sound . playAsync ( ) ;
24
+ }
17
25
18
- const rollDice = ( ) => {
19
26
setDice ( dice => dice . map ( ( die ) => {
20
27
let faceValue = Math . random ( ) ;
21
28
@@ -38,6 +45,12 @@ export default function Index() {
38
45
setTimeout ( ( ) => { setRolling ( false ) ; } , 1000 ) ;
39
46
}
40
47
48
+ const loadAudio = async ( ) => {
49
+ rollSfx . current = await Audio . Sound . createAsync (
50
+ require ( "@/assets/audio/dice_roll.wav" )
51
+ ) ;
52
+ }
53
+
41
54
useEffect ( ( ) => {
42
55
let lastRollTime = Date . now ( ) ;
43
56
@@ -49,7 +62,13 @@ export default function Index() {
49
62
}
50
63
} ) ;
51
64
52
- return ( ) => accel . remove ( ) ;
65
+ loadAudio ( ) ;
66
+
67
+ return ( ) => {
68
+ if ( rollSfx . current )
69
+ rollSfx . current . sound . unloadAsync ( ) ;
70
+ accel . remove ( ) ;
71
+ }
53
72
} , [ ] ) ;
54
73
55
74
const onAddDice = ( ) => {
0 commit comments