Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audio: browser does not play audio until refresh #3122

Closed
1 of 11 tasks
kazzmir opened this issue Oct 2, 2024 · 7 comments
Closed
1 of 11 tasks

audio: browser does not play audio until refresh #3122

kazzmir opened this issue Oct 2, 2024 · 7 comments
Milestone

Comments

@kazzmir
Copy link

kazzmir commented Oct 2, 2024

Ebitengine Version

2.8.0

Operating System

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • OpenBSD
  • Android
  • iOS
  • Nintendo Switch
  • PlayStation 5
  • Xbox
  • Web Browsers

Go Version (go version)

go version go1.23.0 linux/amd64

What steps will reproduce the problem?

ebiten $ wasmserve ./examples/audio

Load localhost:8080 in chrome and click on the page. No audio is heard. Refresh the page and then click on the game area, now sound plays.

chrome Version 129.0.6668.58 (Official Build) (64-bit)
I also tested in firefox 130.0.1 (64-bit) with the same result.

What is the expected result?

Audio should play without having to refresh the page

What happens instead?

No audio until a page refresh is done.

Anything else you feel useful to add?

These messages appear in the chrome dev console

The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu

image
I can't seem to copy/paste this text, but here is an image of the longer stack trace.

@kazzmir kazzmir added the bug label Oct 2, 2024
@github-actions github-actions bot added the os:js label Oct 2, 2024
@hajimehoshi hajimehoshi added this to the v2.8.1 milestone Oct 2, 2024
@hajimehoshi
Copy link
Owner

hajimehoshi commented Oct 2, 2024

Thanks. Did you say the warning "The AudioContext was not allowed to start." was shown twice at the first load? I need both stacktraces.

@kazzmir
Copy link
Author

kazzmir commented Oct 2, 2024

When I open the browser to localhost:8080 the first time I get this in the dev console

wasm_exec.js:390 The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu
syscall/js.valueNew @ wasm_exec.js:390

Then when I click into the wasm game I see

wasm_exec.js:356 The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu
syscall/js.valueCall @ wasm_exec.js:356
$syscall_js.valueCall @ 03aaed6e:0x1eecc6
$syscall_js.Value.Call @ 03aaed6e:0x1ee6e2
$github.com_ebitengine_oto_v3.newContext.func3.1 @ 03aaed6e:0x60759d
$syscall_js.handleEvent @ 03aaed6e:0x1eafe7
$runtime.handleEvent @ 03aaed6e:0x28899
$wasm_export_resume @ 03aaed6e:0x125205
_resume @ wasm_exec.js:545
(anonymous) @ wasm_exec.js:556

Then I reload the window and see

wasm_exec.js:390 The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu
syscall/js.valueNew @ wasm_exec.js:390
$syscall_js.valueNew @ 03aaed6e:0x1ef7cc
$syscall_js.Value.New @ 03aaed6e:0x1ef506
$github.com_ebitengine_oto_v3.newContext @ 03aaed6e:0x605c60
$github.com_ebitengine_oto_v3.NewContext @ 03aaed6e:0x604d90
$github.com_hajimehoshi_ebiten_v2_audio.newContext @ 03aaed6e:0x610059
$github.com_hajimehoshi_ebiten_v2_audio.__playerFactory_.initContextIfNeeded @ 03aaed6e:0x611592
$github.com_hajimehoshi_ebiten_v2_audio.__playerImpl_.ensurePlayer @ 03aaed6e:0x611a3f
$github.com_hajimehoshi_ebiten_v2_audio.__playerImpl_.Play @ 03aaed6e:0x612193
$main.NewPlayer @ 03aaed6e:0x69ac18
$main.NewGame @ 03aaed6e:0x69dbea
$main.main @ 03aaed6e:0x69e916
$runtime.main @ 03aaed6e:0x963c7
$wasm_pc_f_loop @ 03aaed6e:0x12513a
$wasm_export_run @ 03aaed6e:0x1251fb
run @ wasm_exec.js:534
(anonymous) @ (index):16
await in (anonymous)
(anonymous) @ (index):23

After this, I can click into the window and hear audio.

@hajimehoshi
Copy link
Owner

Unfortunately I failed to reproduce this. I'll try to investigate this more. Thanks!

@hajimehoshi
Copy link
Owner

My current guess is that the event handler for the user input is not invoked directly but indirectly due to the Go scheduler.

hajimehoshi added a commit to ebitengine/oto that referenced this issue Oct 2, 2024
AudioContext.resume might fail in theory. This change addresses the
issue where resuming fails by ensuring the promise result.

Updates hajimehoshi/ebiten#3122
hajimehoshi added a commit that referenced this issue Oct 2, 2024
@hajimehoshi
Copy link
Owner

@kazzmir Could you try

go get github.com/hajimehoshi/ebiten/v2@fd3a253d884910359f1b2002a677002b75040c08 

? The first click might not start the audio, but multiple clicks should start.

@kazzmir
Copy link
Author

kazzmir commented Oct 2, 2024

With that new version the audio starts on the first click.

@hajimehoshi
Copy link
Owner

OK thanks!

hajimehoshi added a commit to ebitengine/oto that referenced this issue Oct 2, 2024
AudioContext.resume might fail in theory. This change addresses the
issue where resuming fails by ensuring the promise result.

Updates hajimehoshi/ebiten#3122
hajimehoshi added a commit that referenced this issue Oct 2, 2024
hajimehoshi added a commit that referenced this issue Oct 5, 2024
hajimehoshi added a commit that referenced this issue Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants