I want to play long audio files from the list, the files are downloaded on first tap, all next taps play the audio. But the problem is that when playing one file and I tap another, the first one is still playing and the next one is playing too. Is there a way to stop all currently playing files when I tap any list item?
onPressHandler = (file, title) => {
let name = file.substring(file.lastIndexOf("/") + 1);
RNFetchBlob.fs.exists(`/storage/sdcard0/Android/data/com.myapp/files/Download/${name}.mp3`)
.then((exist) => {
if (exist) {
/* I want to stop all playing audios here */
var whoosh = new Sound(`/storage/sdcard0/Android/data/com.myapp/files/Download/${name}.mp3`, '', (error) => {
if (error) {
alert('failed to load the sound', error);
return;
}
alert('duration in seconds: ' + whoosh.getDuration() + 'number of channels: ' + whoosh.getNumberOfChannels());
alert(whoosh.isLoaded());
whoosh.play((success) => {
if (success) {
alert('successfully finished playing');
} else {
alert('playback failed due to audio decoding errors');
whoosh.reset();
}
});
});
} else {
const url = `http://myresourcesite/${file}.mp3`;
const headers = {};
const config = {
downloadTitle: name,
downloadDescription: 'Downloading ...',
saveAsName: `${name}.mp3`,
allowedInRoaming: true,
allowedInMetered: true,
showInDownloads: true
};
downloadManager.download(url, headers, config).then((response) => {
alert('Download success!');
}).catch(err => {
alert('Download failed!');
});
}
})
.catch(() => {
alert('error has occured');
});
}