bfirsh/jsnes

Sound problems

tvoybro opened this issue · 5 comments

Strange sound problems with Chrome browser. There is no sound if you open the ROM page directly. But if you go to the ROM page from another page, the sound appears.

I believe I have figured that out. "The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page."

Solution for me:

document.documentElement.addEventListener(
  "mousedown", function(){
    mouse_IsDown = true;
    if (audio_ctx.state !== 'running') {
    audio_ctx.resume();
  }});

document.documentElement.addEventListener(
  "keydown", function(){
    if (audio_ctx.state !== 'running') {
    audio_ctx.resume();
  }});

Do you think we need this on jsnes.org?

Do you think we need this on jsnes.org?

I think I do, sir, since it seemed like an annoying thing to me as I was really puzzled at first and couldn't find the reason as I am a complete javascript (and debugging it) noob. It looked like a real bug, and it will definitely scare the user away.
With the solution above, the sound turns on whenever the user has a real interest in a web page and tries to intuitively press keyboard buttons or clicks the mouse button. If the user wants to mute the sound, he can reload the page or press the emulator button, if there is one. Until the user takes any action, the emulator runs in quiet like attract mode.

By the way, it will be nice if you update your page with new things for the NES. During this time, good demos and games were released.

hdb commented

thanks for this project! I'm also noticing sound issues in Safari and webkit-based browsers (seems like this could be the case on jsnes.org too). Adding the webkit prefix to AudioContext fixed it for me:

var AudioContext = window.AudioContext
    || window.webkitAudioContext
    || false;

if (AudioContext) {
    var audio_ctx = new AudioContext;
}