leoOrion
leoOrion

Reputation: 1957

Embedding audio

I have created a Pomodoro clock(timer). I want to play a sound after each session or break ends.

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;
    }
  }
}

I have tried embedding an audio src through html and playing it via js. Also I have tried using an audio object. Is there any other way to do it??

Full Code here:http://codepen.io/jpninanjohn/pen/WwBWpO?editors=1010

Upvotes: 0

Views: 42

Answers (1)

JSchirrmacher
JSchirrmacher

Reputation: 3354

Convert to mp3 format and use the HTML5 audio element:

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

Another solution is to create the Audio object directly without any html elements:

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

Upvotes: 1

Related Questions