Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
16 changes: 16 additions & 0 deletions ableplayer_extend.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*
Expand Down
42 changes: 42 additions & 0 deletions js/ableplayer_speech_init.js
Original file line number Diff line number Diff line change
@@ -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);