question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The AudioContext was not allowed to start

See original GitHub issue

I get a warning on page load even when I haven’t actually called play() yet. It seems like this is an issue in howler because they call new AudioContext() immediately.

Is it possible to somehow workaround this in use-sound?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
andrewmclagancommented, Dec 3, 2020

We ended up rolling our own Howler hooks due to this issue. We came up with an elegant solution: Simply waiting for user interaction as the error states.

First need a hook that tracks first user interaction events according to the specs:

import { useEffect, useState } from 'react';

const events = ['mousedown', 'touchstart'];

export default function useInteraction() {
  const [ready, setReady] = useState(false);

  const listener = () => {
    if (ready === false) {
      setReady(true);
    }
  };

  useEffect(() => {
    events.forEach((event) => {
      document.addEventListener(event, listener);
    });

    return () => {
      events.forEach((event) => {
        document.removeEventListener(event, listener);
      });   
    };
  }, []);

  return ready;
}

Then we wrapped howler in a simple async dynamic import hook that only loads it after the first user interaction.

import { useState, useEffect } from 'react';
import useInteraction from '../useInteraction';

export default function useAudio(options) {
  const [audio, setAudio] = useState();

  const interacted = useInteraction();

  useEffect(() => {
    async function createAudoContext() {
      const { Howl } = await import('howler');
      setAudio(new Howl(options));
    }

    if (interacted) {
      createAudoContext();
    }

    return () => {
      if (audio) {
        audio.unload();
      }
    };
  }, [options]);

  const ready = Boolean(interacted && audio);

  return { audio, ready };
}

All this in only a few Kb of js. Simply use it like this:

const UsersList = ({ users }) => {
  const { audio, ready } = useAudio({ src: "https://g.com/ding.mp3" });

  useEffect(() => {
    if (ready) {
      audio.play()
    }
  }, [users.length, ready]);

  return <div>{users}</div>;
};

(pseudo code)

0reactions
ryan-sirkacommented, Mar 17, 2022
.current?.play()

hello sir, can i see full of your code to play audio?

Read more comments on GitHub >

github_iconTop Results From Across the Web

The AudioContext was not allowed to start - Stack Overflow
I have this Javascript code which I am using to capture audio input from user when they click on a microphone button. This...
Read more >
Follow-up: The AudioContext was not allowed to start. #1744
The error The AudioContext was not allowed to start was thrown by Chrome when there is no user interaction. This seems like our...
Read more >
Autoplay Policy in Google Chrome (Critical Info for Developers)
Unfortunately, if we check the dev tools in Google Chrome, we see that we're getting a warning The AudioContext was not allowed to...
Read more >
The AudioContext was not allowed to start. It ... - Unity Forum
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on t.
Read more >
Why can I see warning messages "The AudioContext was not ...
That is normal behavior and won't affect final users in any way: some browsers won't allow audio to be played by our library...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found