• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

123
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 3 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 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error