I am trying to implement a web audio API. The code works on Chrome 29.0.1547.76, but not on Safari 6.0.5 (8536.30.1). The key is whether I use noteOn (0) or start (0).
I want to use start () to play part of the sound:
asource.start(0, 2, 1);
works great in Chrome (plays immediately, starts at 2 s, plays for 1 s), but leads to
TypeError: 'undefined' is not a function (evaluating 'asource.start(0, 2, 1)')
in Safari. Replacing this single line with
asource.noteOn(0);
working. [Well, I need to call noteOff (0) instead of stop (0).] I get the same error with start (0). So, I assume that Safari does not implement start (0)? But if so, why do some HTML5 Rocks examples that use start (0) work?
, -. /; .
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="utf-8">
<title>Web Audio API Issue</title>
</head>
<body>
<p>Example working on Chrome but not Safari when using start()</p>
<button id="Load" onclick="init()" >Load</button>
<button id="Play" onclick="playSound()" disabled>Play</button>
<button id="Stop" onclick="stopSound()" disabled>Stop</button>
<script>
var web_audio_context;
var abuffer;
var asource;
function init() {
contextClass = (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.msAudioContext);
web_audio_context = new contextClass();
var theURL = './digits.mp3';
var xhr = new XMLHttpRequest();
xhr.open('GET', theURL, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
finishedLoading(this.response);
};
xhr.send();
}
function finishedLoading(arrayBuffer) {
web_audio_context.decodeAudioData(arrayBuffer, function(buffer) {
abuffer = buffer;
document.getElementById('Load').disabled = true;
document.getElementById('Play').disabled = false;
document.getElementById('Stop').disabled = false;
}, function(e) {
console.log('Error decoding file', e);
});
}
function playSound() {
asource = web_audio_context.createBufferSource();
asource.buffer = abuffer;
asource.connect(web_audio_context.destination);
asource.start(0, 2, 1);
}
function stopSound() {
asource.stop(0);
}
</script>
</body>
</html>