Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
i can't use state props in require function of sound react native

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;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda