fix wrong index and play status after removeAudio

This commit is contained in:
DIYgod 2018-03-09 15:15:30 +08:00
parent ddc529397d
commit 93e28a37ee
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
1 changed files with 20 additions and 4 deletions

View File

@ -210,12 +210,22 @@ class APlayer {
this.audio.src = this.options.music[this.playIndex].url;
this.seek(0);
let playPromise;
if (!this.paused) {
playPromise = Promise.resolve(this.audio.play()).catch((err) => {
console.error(err);
this.pause();
});
}
this.lrc && this.lrc.switch(this.playIndex);
// set duration time
if (this.audio.duration !== 1) { // compatibility: Android browsers will output 1 at first
this.template.dtime.innerHTML = this.audio.duration ? utils.secondToTime(this.audio.duration) : '00:00';
}
return playPromise;
});
}
@ -227,7 +237,12 @@ class APlayer {
this.audio.currentTime = time;
this.bar.set('played', time / this.audio.duration, 'width');
if (isNaN(this.audio.duration)) {
this.bar.set('played', 0, 'width');
}
else {
this.bar.set('played', time / this.audio.duration, 'width');
}
this.template.ptime.innerHTML = utils.secondToTime(time);
}
@ -407,9 +422,12 @@ class APlayer {
removeAudio (index) {
if (this.options.music[index] && this.options.music.length > 1) {
const list = this.container.querySelectorAll('.aplayer-list li');
this.options.music.splice(index, 1);
if (index === this.playIndex) {
if (this.options.music[index + 1]) {
this.switchAudio(index + 1);
this.switchAudio(index);
}
else {
this.switchAudio(index - 1);
@ -419,8 +437,6 @@ class APlayer {
this.playIndex--;
}
this.options.music.splice(index, 1);
list[index].remove();
for (let i = index; i < list.length; i++) {
list[i].getElementsByClassName('aplayer-list-index')[0].textContent = i;