I'm trying to implement 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() so that I can play part of a sound:
asource.start(0, 2, 1);
works fine in Chrome (plays immediately, starts at the 2 s mark, plays for 1 s) but results in
TypeError: 'undefined' is not a function (evaluating 'asource.start(0, 2, 1)')
on Safari. Replacing that one line with
asource.noteOn(0);
works. [Well, I need to call noteOff(0) instead of stop(0).] I get the same error with start(0). So, I'm assuming that Safari does not implement start(0)? But if so, why do some of the examples at HTML5 Rocks that use start(0) work?
For reference, here's the plete web page. I've tried many different sounds/formats; all result in the same error.
<!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>
I'm trying to implement 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() so that I can play part of a sound:
asource.start(0, 2, 1);
works fine in Chrome (plays immediately, starts at the 2 s mark, plays for 1 s) but results in
TypeError: 'undefined' is not a function (evaluating 'asource.start(0, 2, 1)')
on Safari. Replacing that one line with
asource.noteOn(0);
works. [Well, I need to call noteOff(0) instead of stop(0).] I get the same error with start(0). So, I'm assuming that Safari does not implement start(0)? But if so, why do some of the examples at HTML5 Rocks that use start(0) work?
For reference, here's the plete web page. I've tried many different sounds/formats; all result in the same error.
<!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>
Share
Improve this question
asked Sep 29, 2013 at 20:58
RPWRPW
3352 gold badges5 silver badges16 bronze badges
2
- 1 According to developer.mozilla/en/docs/Web/API/AudioBufferSourceNode the AudioBufferSourceNode has the state "unknow" on Safari. In this case, i imagine that is not yes totaly implemented. – Paul Rad Commented Sep 29, 2013 at 21:11
- 1 Safari 6.1 (released October 22) fixes this. The code now works in both Chrome and Safari. – RPW Commented Oct 22, 2013 at 19:47
2 Answers
Reset to default 5In newer Web Audio API versions the method noteOn
is renamed to start
. Safari still uses the older version while Chrome uses a more current one.
Just try:
asource.start ? asource.start(0, 2, 1) : asource.noteOn(0, 2, 1);
Try adding Web Audio API monkey patch library by Chris Wilson to your project. It might help.