From 9fc8c374144487d3fced2530d762ac3107e94fad Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 30 Mar 2024 09:34:23 -0400 Subject: [PATCH] LibWebView: Handle mutliple audio tracks when audio play state changes For example, if a page has multiple audio elements all actively playing audio, we don't want to broadcast a play state change when only one of them stop playing. --- .../LibWebView/ViewImplementation.cpp | 20 +++++++++++++++++-- .../Libraries/LibWebView/ViewImplementation.h | 1 + 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWebView/ViewImplementation.cpp b/Userland/Libraries/LibWebView/ViewImplementation.cpp index ea386f90f65..2326af89abe 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.cpp +++ b/Userland/Libraries/LibWebView/ViewImplementation.cpp @@ -317,9 +317,25 @@ void ViewImplementation::toggle_media_controls_state() void ViewImplementation::did_change_audio_play_state(Badge, Web::HTML::AudioPlayState play_state) { - m_audio_play_state = play_state; + bool state_changed = false; - if (on_audio_play_state_changed) + switch (play_state) { + case Web::HTML::AudioPlayState::Paused: + if (--m_number_of_elements_playing_audio == 0) { + m_audio_play_state = play_state; + state_changed = true; + } + break; + + case Web::HTML::AudioPlayState::Playing: + if (m_number_of_elements_playing_audio++ == 0) { + m_audio_play_state = play_state; + state_changed = true; + } + break; + } + + if (state_changed && on_audio_play_state_changed) on_audio_play_state_changed(m_audio_play_state); } diff --git a/Userland/Libraries/LibWebView/ViewImplementation.h b/Userland/Libraries/LibWebView/ViewImplementation.h index a53a84fb10a..817c3b037f8 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.h +++ b/Userland/Libraries/LibWebView/ViewImplementation.h @@ -260,6 +260,7 @@ protected: RefPtr> m_pending_screenshot; Web::HTML::AudioPlayState m_audio_play_state { Web::HTML::AudioPlayState::Paused }; + size_t m_number_of_elements_playing_audio { 0 }; }; }