2016-05-19 102 views
0

我創建了一個番茄鍾(定時器)。我想在每場或休息結束後播放聲音。嵌入音頻

function countDownTimer() { 

    timer = setInterval(function() { 

    if (!pause) { 

     if (sec == 0) { 

     if (minutes !== 0) 
      minutes--; 

     if (minutes == 0 && hours !== 0) { 

      display(hours, minutes, sec); 
      hours--; 
      minutes = 59; 

     } 

     display(hours, minutes, sec = 59); 

     } else { 

     sec--; 
     display(hours, minutes, sec); 

     } 
    } 

    }, 1000); 

} 


function display(hr, min, sec) { 

    checkIfOver(hr,min,sec); 
    if (hr < 10) 
    hr = "0" + hr; 
    if (min < 10) 
    min = "0" + min; 
    if (sec < 10) 
    sec = "0" + sec; 

    document.getElementById("clock").innerHTML = hr + ":" + min + ":" + sec; 

}  

function checkIfOver(hr,min,sec){ 

    if ((hr == 0 && min == 0 & sec == 0) && session == true) { 

    session = false; 
    breaks = true; 
    if (confirm("Session is over. Start Break??")) 
     setTime(); 
    else { 
     clearInterval(timer); 
     document.getElementById("start").disabled = false; 
    } 
    } else if ((hr == 0 && min == 0 & sec == 0) && breaks == true) { 

    session = true; 
    breaks = false; 
    if (confirm("Break is over. Start Session??")) 
     setTime(); 
    else { 
     clearInterval(timer); 
     document.getElementById("start").disabled = false; 
    } 
    } 
} 

我已經嘗試通過html嵌入音頻src並通過js播放它。我也嘗試過使用音頻對象。有沒有其他的方式來做到這一點?

全部代碼在這裏:http://codepen.io/jpninanjohn/pen/WwBWpO?editors=1010

回答

1

轉換成MP3格式,並使用HTML5音頻元素:

<audio src="success.mp3"> 
    Your browser does not support the audio element. 
</audio> 

另一種解決方案是直接沒有任何HTML元素創建音頻對象:

var audio = new Audio('success.mp3'); 
audio.play(); 
+0

幫助?還是有什麼缺失? –