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

前端 未结 3 1904
执念已碎
执念已碎 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:18

    You can write a service using $q.defer().

    app.service('service', ['$q', function($q){
        this.play = function play(sound) {
            var deferred = $q.defer();
            sound.play();
            sound.onended = function(e) {
              deferred.resolve(e);
            }
            return deferred.promise;
        }
    }]);
    

    You can just call the service and expecting the promise return. service.play(sound).then(function(e){});

    Full plunker sample - http://plnkr.co/edit/FJYjj4ehslqZQZmYyL1I?p=preview

提交回复
热议问题