aciddoll
aciddoll

Reputation: 841

Web Audio API: no sound in Chrome

I'm using Chrome 22.0.1229.94 and can't figure out why I get no audio on any pages using the Web Audio API. I have checked in the flags but there seems to be no flag to enable the API, do I need to do something else to get it to work?

I tried running a simple script to get some audio happening but no sound.

<script type="text/javascript">
  var context = new webkitAudioContext(),
  oscillator = context.createOscillator();

  oscillator.type = 2;
  oscillator.frequency.value = 500;
  oscillator.connect(context.destination);
  oscillator.noteOn(0);
</script>

Upvotes: 1

Views: 3083

Answers (4)

Miguel L.
Miguel L.

Reputation: 738

In order to use the current version of the API, you need to use start() instead of noteOn(), stop() instead of noteOff(), and specify the oscillator type as a string.

<script type="text/javascript">

var context = new webkitAudioContext();
var oscillator = context.createOscillator();

oscillator.type = "square";
oscillator.frequency.value = 500;
oscillator.connect(context.destination);

var now = context.currentTime;
oscillator.start(now);
oscillator.stop(now + 1);

</script>

Specification is found at : http://webaudio.github.io/web-audio-api/

Upvotes: 1

snapfractalpop
snapfractalpop

Reputation: 2134

According to the latest spec, noteOn() has been changed to start().

Upvotes: 2

uKolka
uKolka

Reputation: 38750

It could be a problem with your Chrome but I see an issue with the script anyway. You should tell it to wait for window to load completely before using Web Audio API. Here is a version of your script that does it:

var init = function () {
    var context, oscillator;
    try {
        context = new webkitAudioContext();
    } catch (e) {
        alert("Your browser does not support Web Audio API!");
        return;
    }
    oscillator = context.createOscillator();

    oscillator.type = 2;
    oscillator.frequency.value = 500;
    oscillator.connect(context.destination);
    oscillator.noteOn(0);
    oscillator.noteOff(1);
}
window.addEventListener("load", init);

Upvotes: 0

William
William

Reputation: 4588

You forgot to add noteOn(0);

<script type="text/javascript">
var context = new webkitAudioContext(),
oscillator = context.createOscillator();

oscillator.type = 2;
oscillator.frequency.value = 500;
oscillator.connect(context.destination);
oscillator.noteOn(0);

</script>

Upvotes: 0

Related Questions