i am actually trying to use currentSong.audio in the playSound function but the error is this :
Error: Cannot find module './screens/samples/clap_2.wav'
i console log the state and i can see the link of the audio but when i put it in the require, it won't use it to play the sound.
It works when replace it by the url of the song directly require('../samples/clap_2.wav') so i am sure that i have the right path to the song.
I am starting learning react native and i think it's because of async or something i don't understand yet..
Any help will be appreciated
Thanks in advance,
import React, {useState, useEffect} from 'react';
import { View, Text, ImageBackground, StyleSheet, TouchableOpacity} from 'react-native';
import { Audio } from 'expo-av';
const Sampler = ({currentSong, songs}) => {
const [sound, setSound] = useState();
async function playSound() {
let url = (currentSong.audio);
console.log(url)
console.log('Loading Sound');
const { sound } = await Audio.Sound.createAsync(
require(url)
);
setSound(sound);
console.log('Playing Sound');
await sound.playAsync(); }
useEffect(() => {
return sound
? () => {
console.log('Unloading Sound');
sound.unloadAsync(); }
: undefined;
}, [sound]);
return(
<View style ={styles.container}>
<TouchableOpacity onPress={playSound}>
<ImageBackground source={{uri:currentSong.cover}} style={styles.image}>
</ImageBackground>
</TouchableOpacity>
</View>
)
}
const styles = StyleSheet.create({
container:{
resizeMode: "cover",
},
image:{
width: 50,
height: 50,
padding: 10,
borderRadius: 70,
justifyContent: "center"
}
})
export default Sampler;
It seems like is looking for the file in the same directory where is Sampler, you could try to pass an absolute path instead. This links explains how to do it.
https://betterprogramming.pub/using-absolute-paths-in-react-native-3be369244fb1