You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
import { useEffect, useState } from "react";
|
|
import { Audio, InterruptionModeAndroid, InterruptionModeIOS } from "expo-av";
|
|
|
|
const useSoundEffect = (soundFile) => {
|
|
const [sound, setSound] = useState(null); // Explicitly set initial state to null
|
|
|
|
useEffect(() => {
|
|
const loadSound = async () => {
|
|
// await Audio.setAudioModeAsync({
|
|
// staysActiveInBackground: true,
|
|
// shouldDuckAndroid: true,
|
|
// playThroughEarpieceAndroid: false,
|
|
// interruptionModeIOS: InterruptionModeIOS.DoNotMix,
|
|
// interruptionModeAndroid: InterruptionModeAndroid.DoNotMix,
|
|
// allowsRecordingIOS: false,
|
|
// playsInSilentModeIOS: true,
|
|
// });
|
|
const { sound: newSound } = await Audio.Sound.createAsync(soundFile);
|
|
setSound(newSound);
|
|
};
|
|
|
|
loadSound();
|
|
|
|
return () => {
|
|
sound?.unloadAsync();
|
|
};
|
|
}, [soundFile, sound]); // Include sound in the dependency array
|
|
|
|
const playSound = async () => {
|
|
if (sound) {
|
|
await sound.playAsync();
|
|
}
|
|
};
|
|
|
|
return playSound;
|
|
};
|
|
|
|
export default useSoundEffect;
|