Last active
August 29, 2015 13:56
-
-
Save kuu/9067606 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=yes" /> | |
</head> | |
<body> | |
<button id="loadBtn">Load</button> | |
<button id="playBtn">Play</button> | |
<script> | |
document.addEventListener("DOMContentLoaded", function() { | |
var tLoadButton = document.getElementById('loadBtn'); | |
var tPlayButton = document.getElementById('playBtn'); | |
var tBGM = new Audio(); | |
tLoadButton.addEventListener('click', function () { | |
var tXHR = new XMLHttpRequest(); | |
tXHR.open('GET', 'bgm.mp3'); | |
tXHR.responseType = 'arraybuffer'; | |
tXHR.onreadystatechange = function() { | |
if (tXHR.readyState === 4 && tXHR.status === 200) { | |
var binary = '' | |
var bytes = new Uint8Array(tXHR.response) | |
var len = bytes.byteLength; | |
for (var i = 0; i < len; i++) { | |
binary += String.fromCharCode( bytes[ i ] ) | |
} | |
tBGM.src = 'data:audio/mpeg;base64,' + btoa(binary); | |
console.log('XHR#onload', tBGM.src); | |
tBGM.load(); | |
} | |
}; | |
tXHR.send(); | |
}, false); | |
tPlayButton.addEventListener('click', function (e) { | |
if (e.target.innerHTML === 'Play') { | |
tBGM.play(); | |
e.target.innerHTML = 'Pause'; | |
} else { | |
tBGM.pause(); | |
e.target.innerHTML = 'Play'; | |
} | |
}, false); | |
tBGM.addEventListener('loadeddata', function () { | |
console.log('HTMLAudio#loadeddata'); | |
tBGM.currentTime = 5.5; | |
}, false); | |
}, false); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment