better handling play promise

This commit is contained in:
DIYgod
2018-03-06 16:23:55 +08:00
parent c590dd4080
commit e2e8884a45
+23 -7
View File
@@ -207,8 +207,10 @@ class APlayer {
this.template.list.scrollTop = this.playIndex * 33;
this.audio.src = this.options.music[this.playIndex].url;
this.seek(0);
this.handlePlayPromise(() => {
this.audio.src = this.options.music[this.playIndex].url;
this.seek(0);
});
if (this.paused) {
this.pause();
}
@@ -250,10 +252,11 @@ class APlayer {
}, 100);
}
const playedPromise = Promise.resolve(this.audio.play());
playedPromise.catch(() => {
this.pause();
}).then(() => {
this.handlePlayPromise(() => {
this.playedPromise = Promise.resolve(this.audio.play());
this.playedPromise.catch(() => {
this.pause();
});
});
this.timer.enable('progress');
@@ -282,7 +285,9 @@ class APlayer {
}, 100);
}
this.audio.pause();
this.handlePlayPromise(() => {
this.audio.pause();
});
this.timer.disable('progress');
}
@@ -444,6 +449,17 @@ class APlayer {
}
}
}
handlePlayPromise (callback) {
if (this.playedPromise) {
this.playedPromise = this.playedPromise.then(() => {
callback();
});
}
else {
callback();
}
}
}
export default APlayer;