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

0

135
Views
No puedo usar accesorios de estado en la función Requerir de sonido reaccionar nativo

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;
about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

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