diff options
author | Leon Krause <lk@leonkrause.com> | 2019-01-20 14:25:15 +0100 |
---|---|---|
committer | Leon Krause <lk@leonkrause.com> | 2019-01-20 14:25:15 +0100 |
commit | 8f1669e77182af21a0c46e5314f61cc9a83beb27 (patch) | |
tree | 2bcfc1f8ae9b141dec2af7a37ab0b476925cf6f9 | |
parent | 09e4d7e9ebb3aa1f1d8b7adf96778a9568797518 (diff) |
Deal with Google's HTML5 autoplay policy
Resume audio context after mouse, touch or key input.
-rw-r--r-- | platform/javascript/audio_driver_javascript.cpp | 9 | ||||
-rw-r--r-- | platform/javascript/audio_driver_javascript.h | 1 | ||||
-rw-r--r-- | platform/javascript/os_javascript.cpp | 6 |
3 files changed, 16 insertions, 0 deletions
diff --git a/platform/javascript/audio_driver_javascript.cpp b/platform/javascript/audio_driver_javascript.cpp index fcfc75280d..16fdc267f3 100644 --- a/platform/javascript/audio_driver_javascript.cpp +++ b/platform/javascript/audio_driver_javascript.cpp @@ -146,6 +146,15 @@ void AudioDriverJavaScript::start() { /* clang-format on */ } +void AudioDriverJavaScript::resume() { + /* clang-format off */ + EM_ASM({ + if (_audioDriver_audioContext.resume) + _audioDriver_audioContext.resume(); + }); + /* clang-format on */ +} + int AudioDriverJavaScript::get_mix_rate() const { /* clang-format off */ diff --git a/platform/javascript/audio_driver_javascript.h b/platform/javascript/audio_driver_javascript.h index bf7e2bcce6..9dcba02c96 100644 --- a/platform/javascript/audio_driver_javascript.h +++ b/platform/javascript/audio_driver_javascript.h @@ -49,6 +49,7 @@ public: virtual Error init(); virtual void start(); + void resume(); virtual int get_mix_rate() const; virtual SpeakerMode get_speaker_mode() const; virtual void lock(); diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 6824ed1803..b92634c8d6 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -245,6 +245,8 @@ EM_BOOL OS_JavaScript::keydown_callback(int p_event_type, const EmscriptenKeyboa return false; } os->input->parse_input_event(ev); + // Resume audio context after input in case autoplay was denied. + os->audio_driver_javascript.resume(); return true; } @@ -335,6 +337,8 @@ EM_BOOL OS_JavaScript::mouse_button_callback(int p_event_type, const EmscriptenM ev->set_button_mask(mask); os->input->parse_input_event(ev); + // Resume audio context after input in case autoplay was denied. + os->audio_driver_javascript.resume(); // Prevent multi-click text selection and wheel-click scrolling anchor. // Context menu is prevented through contextmenu event. return true; @@ -663,6 +667,8 @@ EM_BOOL OS_JavaScript::touch_press_callback(int p_event_type, const EmscriptenTo os->input->parse_input_event(ev); } + // Resume audio context after input in case autoplay was denied. + os->audio_driver_javascript.resume(); return true; } |