第193行: |
第193行: |
| audio.append(button); | | audio.append(button); |
| }; | | }; |
− | })();
| |
− |
| |
− | (function (){
| |
− | var context = new (window.AudioContext || window.webkitAudioContext)();
| |
− | var source;
| |
− | var isPlaying = false;
| |
− |
| |
− | function loadAudio(url, callback) {
| |
− | var request = new XMLHttpRequest();
| |
− | request.open('GET', url, true);
| |
− | request.responseType = 'arraybuffer';
| |
− |
| |
− | request.onload = function() {
| |
− | context.decodeAudioData(request.response, function(buffer) {
| |
− | callback(buffer);
| |
− | });
| |
− | };
| |
− |
| |
− | request.send();
| |
− | }
| |
− |
| |
− | function play() {
| |
− | source = context.createBufferSource();
| |
− | source.buffer = buffer;
| |
− | source.connect(context.destination);
| |
− | source.start(0);
| |
− | isPlaying = true;
| |
− | }
| |
− |
| |
− | function pause() {
| |
− | source.stop(0);
| |
− | isPlaying = false;
| |
− | }
| |
− |
| |
− | function togglePlay() {
| |
− | if (!isPlaying) {
| |
− | play();
| |
− | document.getElementById('playButton').innerHTML = 'Pause';
| |
− | } else {
| |
− | pause();
| |
− | document.getElementById('playButton').innerHTML = 'Play';
| |
− | }
| |
− | }
| |
− |
| |
− | var audioFilePath = document.getElementById('audioFilePath').getAttribute('data-src');
| |
− | var buffer;
| |
− | loadAudio(audioFilePath, function(decodedBuffer) {
| |
− | buffer = decodedBuffer;
| |
− | });
| |
| })(); | | })(); |