emscripten-core / emscripten-core/emscripten

Play audio in background with OpenAL

Open
#19,394 2 comments 2 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I have an issue regarding OpenAL and audio playback in the background.

When running using `EM_TIMING_RAF`(`requestAnimationFrame`) as scheduling mode for animation, audio stops playing when tab is inactive and goes to background. When looking into the OpenAL implementation, I found that it explicitly stops the audio [library_openal.js](https://github.com/emscripten-core/emscripten/blob/f711f8c408a084460069b81902290de92e2314bb/src/library_openal.js#LL90C1-L95C8).

One of the workarounds which I consider is to switch from `EM_TIMING_RAF` to `EM_TIMING_SETTIMEOUT` and back when visibility changes.

For this I have my callback method:
```cpp
EM_BOOL em_visibilitychange_callback(int, const EmscriptenVisibilityChangeEvent* e, void*) {
if (e->hidden) {
emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, 40);
} else {
emscripten_set_main_loop_timing(EM_TIMING_RAF, 1);
}
return EM_TRUE;
}
```

And then set the callback:
```cpp
emscripten_set_visibilitychange_callback(nullptr, false, em_visibilitychange_callback);
```

Is it the right way to "force" `OpenAL` to play in the background?
Are there other options to consider?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.