<audio/> demo

by juberti

HTML

<button onclick="play();">Play</button>
<button onclick="pause();">Pause</button>
<button onclick="stop();">Stop</button>
<button onclick="skip(5);">Skip</button>

JavaScript

var src = 'https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_1MG.mp3';

// https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement
var audioElement = new Audio(src);

// https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement#Methods
function play() {
  if (audioElement.paused) audioElement.play();
}

function pause() {
	if (!audioElement.paused) audioElement.pause();
}

function stop() {
	if (!audioElement.played.length) return;
  if (!audioElement.paused) audioElement.pause();
  audioElement.currentTime = 0;
}

function skip(second) {
	audioElement.currentTime += second;
}