2017-12-21 13 views
0

오디오가 종료되면 작동하는 기능을 얻으려고합니다. 이 함수를 사용하여 사운드를 생성합니다.JS 오디오는 오디오가 끝날 때 트리거하지 않습니다.

function sound(src,volume,repeat) { 
    volume = volume || 1 
    repeat = repeat || false; 
    this.sound = document.createElement("audio"); 
    this.sound.volume = volume; 
    this.sound.src = src; 
    this.sound.setAttribute("preload", ""); 
    this.sound.setAttribute("class", "gamesound"); 
    if(repeat)this.sound.setAttribute("loop",""); 
    this.sound.style.display = "none"; 
    document.body.appendChild(this.sound); 
    this.play = function(){ 
     this.sound.play(); 
    } 
    this.stop = function(){ 
     this.sound.pause(); 
    } 
    this.pause = function(){ 
     this.sound.pause(); 
    } 
    this.onend = function (s){ 
     this.sound.onended = s;     
    } 
    this.load = function(){ 
     this.sound.load(); 
    } 
    this.currentTime = function (t){ 
     this.sound.currentTime = t; 
    } 
} 

그래서이

var song = new sound("sound.mp3",0.2,true); 
song.ended(function(){ 
    alert("ended"); 
}) 

처럼 사용할하지만 그 어디에서 잘못 여기려고하고, 트리거 할 수는 없습니다.

답변

0

루프 옵션이 켜져 있으면 이벤트가 실행되지 않으며,이 경우에는 사용자가 프리젠 테이션 중입니다.

+0

나는 그것이 어리석은 것을 알았다. @ Yousef 감사합니다. – MasterT