Skip to content

Commit 5fb7327

Browse files
committed
part9
1 parent 6e1071b commit 5fb7327

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

app/index.jsx

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { View, ScrollView, StyleSheet, FlatList } from "react-native";
2-
import { useState, useEffect, useMemo } from 'react';
2+
import { useState, useEffect, useMemo, useRef } from 'react';
33
import { SafeAreaView } from "react-native-safe-area-context";
44
import { Accelerometer } from "expo-sensors";
5+
import { Audio } from "expo-av";
56

67
import DicePicker from "@/components/DicePicker";
78
import DiceList from "@/components/DiceList";
@@ -14,8 +15,14 @@ export default function Index() {
1415
const [key, setKey] = useState(0);
1516
const [rolling, setRolling] = useState(false);
1617
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+
}
1725

18-
const rollDice = () => {
1926
setDice(dice => dice.map((die) => {
2027
let faceValue = Math.random();
2128

@@ -38,6 +45,12 @@ export default function Index() {
3845
setTimeout(() => { setRolling(false); }, 1000);
3946
}
4047

48+
const loadAudio = async () => {
49+
rollSfx.current = await Audio.Sound.createAsync(
50+
require("@/assets/audio/dice_roll.wav")
51+
);
52+
}
53+
4154
useEffect(() => {
4255
let lastRollTime = Date.now();
4356

@@ -49,7 +62,13 @@ export default function Index() {
4962
}
5063
});
5164

52-
return () => accel.remove();
65+
loadAudio();
66+
67+
return () => {
68+
if (rollSfx.current)
69+
rollSfx.current.sound.unloadAsync();
70+
accel.remove();
71+
}
5372
}, []);
5473

5574
const onAddDice = () => {

assets/audio/dice_roll.wav

507 KB
Binary file not shown.

package-lock.json

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"expo-haptics": "~14.0.1",
2626
"expo-linking": "~7.0.5",
2727
"expo-router": "~4.0.17",
28+
"expo-sensors": "~14.0.2",
2829
"expo-splash-screen": "~0.29.22",
2930
"expo-status-bar": "~2.0.1",
3031
"expo-symbols": "~0.2.2",
@@ -41,7 +42,7 @@
4142
"react-native-svg": "15.8.0",
4243
"react-native-web": "~0.19.13",
4344
"react-native-webview": "13.12.5",
44-
"expo-sensors": "~14.0.2"
45+
"expo-av": "~15.0.2"
4546
},
4647
"devDependencies": {
4748
"@babel/core": "^7.25.2",

0 commit comments

Comments
 (0)