Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!