today I wanted to experiment with the audio in the React-native. I want to start my sound and then stop it after 5 sec so i follow instructions in expo website for audio (expo-av) so my code so far looks like that -
const [sound, setSound] = React.useState();
async function playSound() {
console.log("Loading Sound");
const { sound } = await Audio.Sound.createAsync(
require("./assets/sound.mp3")
);
setSound(sound);
console.log("Playing Sound");
await sound.playAsync();
}
React.useEffect(() => {
return sound
? () => {
console.log("Unloading Sound");
sound.unloadAsync();
}
: undefined;
}, [sound]);
.
.
.
<Button
title={"Play"}
onPress={async () => {
await playSound();
setTimeout(async () => {
await sound.stopAsync();
}, 5000);
}}
/>
but i got error
[Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'sound.stopAsync')]
Why? I can`t understad?