From 3ba6f08986af5298c0bc1a4f759ffaeef1a7391c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Dani=C3=ABl=20Maat?= Date: Sun, 31 Jul 2022 14:58:33 +0100 Subject: [PATCH] Fix playing music on Chrome --- src/music/MusicPlayer.tsx | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/music/MusicPlayer.tsx b/src/music/MusicPlayer.tsx index 59fc4f6..bc04191 100644 --- a/src/music/MusicPlayer.tsx +++ b/src/music/MusicPlayer.tsx @@ -67,14 +67,22 @@ class MusicPlayer extends React.Component { } if (this.props.playing) { - source - .play() - .then(() => { - console.info("Started playing audio"); - }) - .catch((error) => { - console.error(`Could not play audio: ${error}`); - }); + // Chrome is super awkward about AudioContext, and won't + // even allow creating one without complaining about + // wanting user input, so we need to resume the context + // before we can actually play. + // + // Luckily, this has no adverse effects on Firefox. + context.resume().then(() => { + source + .play() + .then(() => { + console.info("Started playing audio"); + }) + .catch((error) => { + console.error(`Could not play audio: ${error}`); + }); + }); } else { source.pause(); }