How can i create a promise for the end of playing sound?

前端 未结 3 1898
执念已碎
执念已碎 2020-12-10 12:54

I have a simple JavaScript that loads sounds:

  prefix = \'modules/sounds/\';
 _sounds = [\'nameOfSound\',\'nameOfSound\',\'nameOfSound\'];


 for (var sound         


        
3条回答
  •  遥遥无期
    2020-12-10 13:23

    There is an 'ended' event that is emitted when the audio has finished playing. Source

     this.play = function(trackName) {
        return new Promise(function(resolve, reject) {
            var audio = _sounds[trackName].audio;
            audio.addEventListener('ended', resolve);
            audio.play();
        });
     };
    

提交回复
热议问题