react-nativereact-native-sound

How can I play tracks one after another from the array in React Native?


I used such libraries as react-native-sound and react-native-audio-recorder-player in order to play tracks from an array. I tried mapping the array but it plays just the first track. Is there any method that I can use to play all tracks from the array one after another?

const[id, setId] = useState(1)
 const sound = new Sound(('/data/user/0/com.hay/cache/'  + id + '.mp3'), Sound.MAIN_BUNDLE)

 const playA = () => {
   sound.play(success=> setId(id+1))
 }


  return (
<View style={{flex: 1, backgroundColor: '#FFEDCB', justifyContent: 'center'}}>
<View style={{alignSelf: 'center'}} >
<Image source={globo} style={{width: 340, height: 484}} />
<View style={{  position: 'absolute', top: 403 }}>
<View style={{flexDirection: 'row',position: 'absolute', right: -120 }}>
 <TouchableOpacity onPress={()=>playA() }
 style={styles.iconHeart}><Icon name={ 'play-circle-outline'}
 size={60} color='#F8C56A' /></TouchableOpacity>
</View></View></View></View>

  );
};


Solution

  • It should look like this:

     useEffect( () => {
    let sound = new Sound(('/data/user/0/com.hay/cache/'  + id + '.mp3'), Sound.MAIN_BUNDLE, (error) => {
                if (error) {
                    console.log('failed to load the sound', error);
                } else {
                    sound.play((success) => setId(id+1)); 
                }
            });
    }, [id] )