diff --git a/README.md b/README.md index bd7282e..09b82c0 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,11 @@ Extends Drupal Ableplayer module (https://www.drupal.org/project/ableplayer/) with additional configurations. * Show transcript by default * Show timestamp +* No text-to-speech blip on page load in Firefox. Able Player speaks a blank + utterance to load the browser's voices while it sets up each player. Firefox + makes that audible, while Chrome and Edge block it. This module skips that + page-load call (`js/ableplayer_speech_init.js`, added to the + `ableplayer/ableplayer` library), and speech still starts once the user + presses play or opens the preferences. This has been tested with Ableplayer 3.1.0 and Drupal's default olivero theme. diff --git a/ableplayer_extend.module b/ableplayer_extend.module index ddcdcd1..1e9edbc 100755 --- a/ableplayer_extend.module +++ b/ableplayer_extend.module @@ -24,6 +24,22 @@ function ableplayer_extend_help($route_name, RouteMatchInterface $route_match) { } } +/** + * Implements hook_library_info_alter(). + * + * Appends js/ableplayer_speech_init.js to Able Player's own library, so the + * fix loads wherever Able Player does. See that file for why it is needed. + */ +function ableplayer_extend_library_info_alter(&$libraries, $extension) { + if ($extension !== 'ableplayer' || !isset($libraries['ableplayer']['js'])) { + return; + } + // A leading slash makes the path relative to the Drupal root, not to the + // ableplayer module. + $path = '/' . \Drupal::service('extension.list.module')->getPath('ableplayer_extend') . '/js/ableplayer_speech_init.js'; + $libraries['ableplayer']['js'][$path] = []; +} + /** * Implements hook_field_formatter_third_party_settings_form(). * diff --git a/js/ableplayer_speech_init.js b/js/ableplayer_speech_init.js new file mode 100644 index 0000000..d33391f --- /dev/null +++ b/js/ableplayer_speech_init.js @@ -0,0 +1,42 @@ +/** + * @file + * Stops Able Player from speaking a blank utterance when the page loads. + * + * While it sets up each player, Able Player calls initSpeech('init'), which + * speaks " " through the Web Speech API to load the browser's voice list (and + * does it again on the first click anywhere on the page). Chrome and Edge + * refuse speechSynthesis.speak() before the user has interacted with the + * page, so nothing is heard there. Firefox passes the utterance to the + * system speech engine, which makes an audible blip on every page load. + * + * Only the 'init' call is skipped. Able Player calls initSpeech() again with + * 'play', 'prefs' or 'desc' when the user presses play, opens the + * preferences, or a description has to be spoken. Each of those follows a + * user action, so audio description and caption speech still work. + * + * This file is appended to the ableplayer/ableplayer library by + * ableplayer_extend_library_info_alter(), so it runs after ableplayer.min.js + * and before Able Player creates its players on document ready. + */ +(function (AblePlayer) { + 'use strict'; + + if (!AblePlayer || typeof AblePlayer.prototype.initSpeech !== 'function') { + return; + } + + var initSpeech = AblePlayer.prototype.initSpeech; + + AblePlayer.prototype.initSpeech = function (context) { + if (context === 'init') { + // handlePlay() reads this.synth.paused before it calls + // initSpeech('play'), so keep a reference to the synthesizer without + // speaking. speechEnabled stays null, so the later call still runs. + if (window.speechSynthesis && !this.synth) { + this.synth = window.speechSynthesis; + } + return; + } + return initSpeech.apply(this, arguments); + }; +})(window.AblePlayer);