0

I read an article introducing HTML5 Web Audio API.

It's very interesting with my desktop Chrome.

However, is this not working for mobile?

AGamePlayer
  • 7,404
  • 19
  • 62
  • 119

1 Answers1

0

The article seems to be about three years old. And back then the autoplay policy of browsers wasn't really affecting the usage of the Web Audio API. That has changed. Now it is required to have a user gesture (typically a click event) to start an AudioContext.

The first example of the referenced article would than look like this:

const $playButton = document.getElementById('play-button');

$playButton.addEventListener('click', () => {
    const context = new AudioContext();
    const oscillator = context.createOscillator();

    oscillator.type = 'sine';
    oscillator.connect(context.destination);
    oscillator.start();
};

This does assume that you have a button with the id play-button somewhere in your HTML.

<button id="play-button">play</button>
chrisguttandin
  • 7,025
  • 15
  • 21
  • I tried it but still not working. Code here: https://codepen.io/agameplayer/pen/GRRqqMe – AGamePlayer Oct 18 '19 at 11:01
  • By coincidence I just commented on the other thread (https://stackoverflow.com/questions/58335956/why-the-web-audio-output-from-oscillator-is-not-working-as-expected) where you posted the same codepen about an hour ago: https://stackoverflow.com/questions/58335956/why-the-web-audio-output-from-oscillator-is-not-working-as-expected#comment103235535_58396621. Does it work if you use the factory function? – chrisguttandin Oct 18 '19 at 11:19