I am using Expo Audio to play some short sounds from a list.
async playAudio(file) {
try {
await Audio.setIsEnabledAsync(true);
const sound = new Audio.Sound();
await sound.loadAsync(file);
await sound.playAsync();
} catch(error) {
console.error(error);
}
}
Which I'm calling from a list rendered with list.map()
renderTheList = (item, i) => {
return (
<View key={i}>
<TouchableOpacity onPress={ () => { this.onAudioSelected(item.audio) }}>
</TouchableOpacity>
</View>
)
}
onAudioSelected(audio) {
// Audio.clearSounds() <-- something like this
playAudio(audio)
...
}
The sounds play fine, but when I select the next item in the list, the previous sound does not stop. So if I touch a bunch in a row, a bunch of sounds play at once.
How can I stop all currently playing sounds?