increpare / PuzzleScript

Open Source HTML5 Puzzle Game Engine
MIT License
903 stars 159 forks source link

Sound issue on Chrome for Windows #419

Closed samanthaeschaffer closed 3 years ago

samanthaeschaffer commented 6 years ago

I've been working on a game the past couple of days and when I went to add sound, I realized that the editor wasn't playing anything. I exported the game and ran the game on its own and still no sound. After trying all the obvious user error solutions (computer muted? headphones plugged in? etc), I noticed this warning in the console:

An AudioContext in a cross origin iframe must be created or resumed from a user gesture to enable audio output.

I opened up a friend's game on itch.io that I know was also made in PuzzleScript and sure enough, still no sound and the same warning.

If I refresh and as it's loading click on the page fast enough, I can get it to load without the warning and sound successfully plays.

Adding console.log(AUDIO_CONTEXT.state) after the AudioContext constructor shows the state as suspended when the warning appears and running when I manage to click fast enough and the warning doesn't appear.

I tried just adding AUDIO_CONTEXT.resume() in a click event, but even though the console logged that it successfully transitioned from suspended to running, there was still no sound. Javascript isn't really in my wheelhouse, so it's very possible I missed something in my quick hacky fix attempt.

Browser is Google Chrome (Version 63.0.3239.132 (Official Build) (64-bit)). Platform is PC (Windows 10).

Lemme know if I can provide any more info to help.~

increpare commented 6 years ago

Thanks for letting me know about this.

Ah, so the problem is that it's broken when a puzzle script game is embedded itch.io? Could you link me to a page that has this error?

S

2018-01-08 12:32 GMT+01:00 Samantha Schaffer notifications@github.com:

I've been working on a game the past couple of days and when I went to add sound, I realized that the editor wasn't playing anything. I exported the game and ran the game on its own and still no sound. After trying all the obvious user error solutions (computer muted? headphones plugged in? etc), I noticed this warning in the console:

An AudioContext in a cross origin iframe must be created or resumed from a user gesture to enable audio output.

I opened up a friend's game on itch.io that I know was also made in PuzzleScript and sure enough, still no sound and the same warning.

If I refresh and as it's loading click on the page fast enough, I can get it to load without the warning and sound successfully plays.

Adding console.log(AUDIO_CONTEXT.state) after the AudioContext constructor https://github.com/increpare/PuzzleScript/blob/master/js/sfxr.js#L19 shows the state as suspended when the warning appears and running when I manage to click fast enough and the warning doesn't appear.

I tried just adding AUDIO_CONTEXT.resume() in a click event, but even though the console logged that it successfully transitioned from suspended to running, there was still no sound. Javascript isn't really in my wheelhouse, so it's very possible I missed something in my quick hacky fix attempt.

Browser is Google Chrome (Version 63.0.3239.132 (Official Build) (64-bit) ). Platform is PC (Windows 10).

Lemme know if I can provide any more info to help.~

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/increpare/PuzzleScript/issues/419, or mute the thread https://github.com/notifications/unsubscribe-auth/AAca4FIqx_J4qyY9pdteRyyKzYYkhFDRks5tIfzmgaJpZM4RWP2K .

samanthaeschaffer commented 6 years ago

From what I can see, it's broken pretty much everywhere?

Editor

There's also an unrelated bonus unsafe script error.

puzzlescript-issue419-editor1

Exported Local File

puzzlescript-issue419-exported1 puzzlescript-issue419-exported2

Embedded

Link to the game pictured. Interestingly if I go into settings on itch.io and enable Require a click to run HTML5 game embeds (pictured below), the warning doesn't appear and sound plays normally.

puzzlescript-issue419-embedded1 puzzlescript-issue419-embedded2

Would it be possible to initialize the AudioContext in response to the game starting (eg the first user input) rather than immediately on load?

increpare commented 6 years ago

Thanks for the detailed reply!

There's also an unrelated bonus unsafe script error. Good catch. Fixed - thanks :)

Would it be possible to initialize the AudioContext in response to the game starting (eg the first user input) rather than immediately on load?

Ok, for whatever reason I'm not able to reproduce this behaviour on my chrome, but I updated a version of the engine that tries to make an audiocontext in response to buttonpress events. If you try export again (force refresh the page first to make sure everything's loaded) and the problem with exported things is gone, then I'll close the bug. Fingers crossed...

2018-01-09 9:57 GMT+01:00 Samantha Schaffer notifications@github.com:

From what I can see, it's broken pretty much everywhere? Editor

There's also an unrelated bonus unsafe script error.

[image: puzzlescript-issue419-editor1] https://user-images.githubusercontent.com/3683085/34711971-29e2556c-f571-11e7-8e03-7626e3f4e3b4.png Exported Local File

[image: puzzlescript-issue419-exported1] https://user-images.githubusercontent.com/3683085/34712018-525d858e-f571-11e7-8554-f1516c42e9bd.png [image: puzzlescript-issue419-exported2] https://user-images.githubusercontent.com/3683085/34712019-528f5fe6-f571-11e7-925e-5ea9ab1965f9.png Embedded

Link to the game pictured. https://hypernexus.itch.io/exploding-barrels Interestingly if I go into settings on itch.io and enable Require a click to run HTML5 game embeds (pictured below), the warning doesn't appear and sound plays normally.

[image: puzzlescript-issue419-embedded1] https://user-images.githubusercontent.com/3683085/34712178-e0d07114-f571-11e7-9fe7-6a30b1eade3f.png [image: puzzlescript-issue419-embedded2] https://user-images.githubusercontent.com/3683085/34712179-e1057800-f571-11e7-82cf-6a1a50038949.png

Would it be possible to initialize the AudioContext in response to the game starting (eg the first user input) rather than immediately on load?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/increpare/PuzzleScript/issues/419#issuecomment-356220453, or mute the thread https://github.com/notifications/unsubscribe-auth/AAca4AbUq_GrR8SZaR9X_-LvIbNiygr2ks5tIynfgaJpZM4RWP2K .

samanthaeschaffer commented 6 years ago

That didn't quite fix it, but playing with the code in the exported html file, I found that the following change to that method you added checkAudioContextExists does~

function checkAudioContextExists() {
    try {
        null == AUDIO_CONTEXT && ("undefined" != typeof AudioContext ? AUDIO_CONTEXT = new AudioContext : "undefined" != typeof webkitAudioContext && (AUDIO_CONTEXT = new webkitAudioContext))
        if(AUDIO_CONTEXT != null && AUDIO_CONTEXT.state == "suspended") {
            AUDIO_CONTEXT.resume().then(function() { 
                window.console.log("Resumed AudioContext")
        });
        }
    } catch (e) {
        window.console.log(e)
    }
}

The problem was that AUDIO_CONTEXT wasn't null; it was just in the suspended state, preventing it from playing sound.

Lemme know when that goes live on the editor, and I'll be able to confirm that it's fixed.~

increpare commented 3 years ago

I think this is all good now