De hecho, estoy tratando de usar currentSong.audio en la función playSound, pero el error es este:
Error: Cannot find module './screens/samples/clap_2.wav'
En la consola registro el estado y puedo ver el enlace del audio, pero cuando lo pongo en la solicitud, no lo usará para reproducir el sonido. Funciona cuando se reemplaza por la URL de la canción directamente require('../samples/clap_2.wav') así que estoy seguro de que tengo el camino correcto a la canción.
Estoy empezando a aprender a reaccionar de forma nativa y creo que es por async o algo que aún no entiendo.
Cualquier ayuda será apreciada
Gracias por adelantado,
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;
Parece que está buscando el archivo en el mismo directorio donde está Sampler, podría intentar pasar una ruta absoluta en su lugar. Este enlace explica cómo hacerlo.
https://betterprogramming.pub/using-absolute-paths-in-react-native-3be369244fb1