Ritz Millan
Ritz Millan

Reputation: 11

Playing background music in a React app using Howler

So im using Howler to play background music when my React app is loaded. So I store ID of the instance of Howler(i.e is responsible for background music) using useState hook. I have a button in the app to toggle pause and play in the app. The problem is, when ever my site rerenders, a new instance is started playing and when I pause nothing pauses.

  const [aid, setAid] = useState(null);
  const [sp, setSp] = useState(false);

function handleBgSound() {
    if (sp) {
      amb.pause(aid);
      setSp(false)
    }
     else if (!sp) {
      setAid(amb.play())
      setSp(true)
    } else {
      amb.pause();
    }
  }

Im frustrated with this and would appreciate any help or leads!!

Upvotes: 1

Views: 1208

Answers (0)

Related Questions