我想播放列表中的长音频文件,文件在第一次点击时下载,所有接下来的点击播放音频。但问题是,当播放一个文件并点击另一个文件时,第一个文件仍在播放,下一个文件也在播放。当我点击任何列表项时,有没有办法停止所有当前正在播放的文件?
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');
});
}