Skip to content

Commit e2e8884

Browse files
committed
better handling play promise
1 parent c590dd4 commit e2e8884

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

src/js/player.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,10 @@ class APlayer {
207207

208208
this.template.list.scrollTop = this.playIndex * 33;
209209

210-
this.audio.src = this.options.music[this.playIndex].url;
211-
this.seek(0);
210+
this.handlePlayPromise(() => {
211+
this.audio.src = this.options.music[this.playIndex].url;
212+
this.seek(0);
213+
});
212214
if (this.paused) {
213215
this.pause();
214216
}
@@ -250,10 +252,11 @@ class APlayer {
250252
}, 100);
251253
}
252254

253-
const playedPromise = Promise.resolve(this.audio.play());
254-
playedPromise.catch(() => {
255-
this.pause();
256-
}).then(() => {
255+
this.handlePlayPromise(() => {
256+
this.playedPromise = Promise.resolve(this.audio.play());
257+
this.playedPromise.catch(() => {
258+
this.pause();
259+
});
257260
});
258261

259262
this.timer.enable('progress');
@@ -282,7 +285,9 @@ class APlayer {
282285
}, 100);
283286
}
284287

285-
this.audio.pause();
288+
this.handlePlayPromise(() => {
289+
this.audio.pause();
290+
});
286291
this.timer.disable('progress');
287292
}
288293

@@ -444,6 +449,17 @@ class APlayer {
444449
}
445450
}
446451
}
452+
453+
handlePlayPromise (callback) {
454+
if (this.playedPromise) {
455+
this.playedPromise = this.playedPromise.then(() => {
456+
callback();
457+
});
458+
}
459+
else {
460+
callback();
461+
}
462+
}
447463
}
448464

449465
export default APlayer;

0 commit comments

Comments
 (0)