Angular语言 - 音频结束后更改按钮



我想点击播放音频的按钮。如果音频结束,我可以将按钮从播放按钮更改为重播按钮。

这是我的代码。

audioObj = new Audio();
playAudio() {
if (!this.isPlaying) {
this.isPlaying = true;
this.audioObj.play();

// I'm already tried using this.audioObj.play().then(()=>{...}) but it's not working. 
It doesn't wait for the end of audio but executes it immediately after the play button is clicked.
}
}

您需要附加一个事件侦听器,然后在回调中执行操作,如下所示:

audioObj = new Audio();
playAudio() {
this.audioObj.addEventListener('ended', () => {
// action here
})
if (!this.isPlaying) {
this.isPlaying = true;
this.audioObj.play();
}
}
});

最新更新