I'm trying to play and audio file but I'd like to load the file dynamically without having to hardcode the import for each file on my project folder assets/audio.
The code below is working when I use the import, but not when I use the "file".
const Sound = require('react-native-sound');
import t1 from './assets/audio/t1.mp3';
import t2 from './assets/audio/t2.mp3';
Sound.setCategory('Playback', true); // true = mixWithOthers
export const playSound = () => {
const file = './assets/audio/t1.mp3';
// const s = new Sound(file, (error) => { // does not work
const s = new Sound(t1, (error) => { // works
if (error) {
console.log('error', error);
return;
}
s.play(() => {
s.release()
});
});
};
How can I provide the filename during runtime so that I don't need to import every audio file?