Allow <audio> to play without user interaction in accordance to autoplay setting v2 (#1539)

Adds back setting Chromium's autoplay policy according to Anki's, this time without globals.
This commit is contained in:
nwwt 2021-12-07 23:08:56 +01:00 committed by GitHub
parent 0de24122ad
commit 9becb4c11f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 0 deletions

View file

@ -203,6 +203,7 @@ class Previewer(QDialog):
bodyclass = theme_manager.body_classes_for_card_ord(c.ord) bodyclass = theme_manager.body_classes_for_card_ord(c.ord)
if c.autoplay(): if c.autoplay():
self._web.setPlaybackRequiresGesture(False)
if self._show_both_sides: if self._show_both_sides:
# if we're showing both sides at once, remove any audio # if we're showing both sides at once, remove any audio
# from the answer that's appeared on the question already # from the answer that's appeared on the question already
@ -217,6 +218,7 @@ class Previewer(QDialog):
audio = c.answer_av_tags() audio = c.answer_av_tags()
av_player.play_tags(audio) av_player.play_tags(audio)
else: else:
self._web.setPlaybackRequiresGesture(True)
av_player.clear_queue_and_maybe_interrupt() av_player.clear_queue_and_maybe_interrupt()
txt = self.mw.prepare_card_text_for_display(txt) txt = self.mw.prepare_card_text_for_display(txt)

View file

@ -525,12 +525,14 @@ class CardLayout(QDialog):
self.have_autoplayed = True self.have_autoplayed = True
if c.autoplay(): if c.autoplay():
self.preview_web.setPlaybackRequiresGesture(False)
if self.pform.preview_front.isChecked(): if self.pform.preview_front.isChecked():
audio = c.question_av_tags() audio = c.question_av_tags()
else: else:
audio = c.answer_av_tags() audio = c.answer_av_tags()
av_player.play_tags(audio) av_player.play_tags(audio)
else: else:
self.preview_web.setPlaybackRequiresGesture(True)
av_player.clear_queue_and_maybe_interrupt() av_player.clear_queue_and_maybe_interrupt()
self.updateCardNames() self.updateCardNames()

View file

@ -320,10 +320,12 @@ class Reviewer:
q = c.question() q = c.question()
# play audio? # play audio?
if c.autoplay(): if c.autoplay():
self.web.setPlaybackRequiresGesture(False)
sounds = c.question_av_tags() sounds = c.question_av_tags()
gui_hooks.reviewer_will_play_question_sounds(c, sounds) gui_hooks.reviewer_will_play_question_sounds(c, sounds)
av_player.play_tags(sounds) av_player.play_tags(sounds)
else: else:
self.web.setPlaybackRequiresGesture(True)
av_player.clear_queue_and_maybe_interrupt() av_player.clear_queue_and_maybe_interrupt()
sounds = [] sounds = []
gui_hooks.reviewer_will_play_question_sounds(c, sounds) gui_hooks.reviewer_will_play_question_sounds(c, sounds)

View file

@ -369,6 +369,11 @@ class AnkiWebView(QWebEngineView):
return factor return factor
return 1 return 1
def setPlaybackRequiresGesture(self, value: bool) -> None:
self.settings().setAttribute(
QWebEngineSettings.WebAttribute.PlaybackRequiresUserGesture, value
)
def _getQtIntScale(self, screen: QWidget) -> int: def _getQtIntScale(self, screen: QWidget) -> int:
# try to detect if Qt has scaled the screen # try to detect if Qt has scaled the screen
# - qt will round the scale factor to a whole number, so a dpi of 125% = 1x, # - qt will round the scale factor to a whole number, so a dpi of 125% = 1x,