Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda