chrismaltby / gb-studio

A quick and easy to use drag and drop retro game creator for your favourite handheld video game system
https://www.gbstudio.dev
MIT License
8.49k stars 469 forks source link

"Export Web" does not play audio on latest version of iOS, iPadOS, and Safari on macOS #788

Open nekobungi opened 3 years ago

nekobungi commented 3 years ago

Describe the bug Any game projects exported as web does not play audio at all, no matter what the players do.

To Reproduce Steps to reproduce the behavior:

  1. Create a new project, or open an existing one
  2. Set an event to play track anywhere in the game.
  3. Select "Game" → "Export As" → "Export Web" on the menu bar
  4. Upload the exported files on to any web server
  5. Open the uploaded URL with one of the browsers described later.

Expected behavior Sound plays normally after a mouse click, a screen touch or a keyboard input

Platform (please complete the following information):

Platforms for playing exported games:

Additional context The reason why there is no sound is due to Safari's mechanism to block auto-playing medias with sound. The latest version of Safari has more stricter blocking mechanism, so the games that were fine before will not work properly now. On iOS/iPadOS, the problem occurs in all browsers because they all use the same layout engine as Safari.


By the way, you can avoid the problem by modifying one of the exported files by following the steps:

  1. Open the exported folder after "Export Web" is done
  2. Open js/other/mobile.js with any text editor
  3. Comment-out the following lines: line 91, 95, 100, 101, 102, 103, 104, 105
    
      // window.audioContext.resume().then(() => {
        settings[0] = true;
        gameboy.initSound();
        soundReady = true;
      // });
    }
    }
    }

// var soundInitTimer = setInterval(function() { // if (GameBoyEmulatorInitialized()) { // initSound(); // clearTimeout(soundInitTimer); // } // }, 16);


4. Save the file, upload the file to the web server again, clear the browser cache if you need
5. the game is now playing sound normally!

This works fine for me, but it is only a temporary solution and may cause other problems on other platforms.
maxoakland commented 3 years ago

This is an issue I've had on iOS since day one and I should've thought to report it. Audio just doesn't play on iOS. Very, very occasionally it will work but it seems random. Even setting it so the game doesn't autoplay any audio, and only plays audio on player input doesn't solve the issue

It's really cool that you found a solution to the problem. I'm going to implement that in my next version