Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Web Audio API source.start(0) supported on Safari (it works just fine on Chrome)?

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 complete 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>
like image 948
RPW Avatar asked Mar 23 '23 06:03

RPW


1 Answers

In 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);
like image 171
dersvenhesse Avatar answered Apr 25 '23 16:04

dersvenhesse