Skip to content

Instantly share code, notes, and snippets.

@yoshiiz
Last active August 11, 2023 07:04
Show Gist options
  • Save yoshiiz/6134482f8fd72063e7221b7b82d27e67 to your computer and use it in GitHub Desktop.
Save yoshiiz/6134482f8fd72063e7221b7b82d27e67 to your computer and use it in GitHub Desktop.
[JavaScript] 複数の音楽ファイルを連続で再生しループする。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>連続再生ループ</title>
</head>
<body>
<audio id='audio'></audio>
<script type="text/javascript">
'use strict';
var playlist = [
'./sample1.wav',
'./sample2.wav',
'./sample3.wav',
'./sample4.wav',
'./sample5.wav'
]
var audio = document.createElement('audio');
document.body.appendChild(audio);
audio.style.width = '100%';
audio.style.height = 'auto';
audio.controls = true;
audio.volume = 0.2;
audio.src = playlist[0];
audio.play();
var index = 0;
audio.addEventListener('ended', function(){
index++;
if (index < playlist.length) {
audio.src = playlist[index];
audio.play();
}
else {
audio.src = playlist[0];
audio.play();
index = 0;
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment