class WavesurferAudioPlayer in AudioField 8
Implements the Wavesurfer Audio Player plugin.
@AudioPlayer ( id = "wavesurfer_audio_player", title =
Plugin annotation
@Translation("Wavesurfer audio player"),
description = @Translation("A customizable audio waveform visualization, built on top of Web Audio API and HTML5 Canvas."),
fileTypes = {
"mp3", "ogg", "oga", "wav",
},
libraryName = "wavesurfer",
website = "https://github.com/katspaugh/wavesurfer.js",
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\audiofield\AudioFieldPluginBase implements ContainerFactoryPluginInterface uses StringTranslationTrait
- class \Drupal\audiofield\Plugin\AudioPlayer\WavesurferAudioPlayer
- class \Drupal\audiofield\AudioFieldPluginBase implements ContainerFactoryPluginInterface uses StringTranslationTrait
Expanded class hierarchy of WavesurferAudioPlayer
File
- src/
Plugin/ AudioPlayer/ WavesurferAudioPlayer.php, line 31
Namespace
Drupal\audiofield\Plugin\AudioPlayerView source
class WavesurferAudioPlayer extends AudioFieldPluginBase {
/**
* Module handler service.
*
* @var Drupal\Core\Extension\ModuleHandler
*/
protected $moduleHandler;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LibraryDiscovery $library_discovery, MessengerInterface $messenger, LoggerChannelFactoryInterface $logger_factory, FileSystemInterface $file_system, ModuleHandler $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $library_discovery, $messenger, $logger_factory, $file_system);
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('library.discovery'), $container
->get('messenger'), $container
->get('logger.factory'), $container
->get('file_system'), $container
->get('module_handler'));
}
/**
* {@inheritdoc}
*/
public function renderPlayer(FieldItemListInterface $items, $langcode, array $settings) {
// Check to make sure we're installed.
if (!$this
->checkInstalled()) {
// Show the error.
$this
->showInstallError();
// Simply return the default rendering so the files are still displayed.
return $this
->renderDefaultPlayer($items, $settings);
}
// Create arrays to pass to the twig template.
$template_files = [];
// Get a unique render Id.
$settings['unique_id'] = $this
->getUniqueRenderId();
// Start building settings to pass to the javascript wavesurfer builder.
$player_settings = [
// Wavesurfer expects this as a 0 - 1 range.
'volume' => $settings['audio_player_initial_volume'] / 10,
'playertype' => $settings['audio_player_wavesurfer_combine_files'] ? 'playlist' : 'default',
'files' => [],
'audioRate' => $settings['audio_player_wavesurfer_audiorate'],
'autoCenter' => $settings['audio_player_wavesurfer_autocenter'],
'barGap' => $settings['audio_player_wavesurfer_bargap'],
'barHeight' => $settings['audio_player_wavesurfer_barheight'],
'barWidth' => $settings['audio_player_wavesurfer_barwidth'],
'cursorColor' => $settings['audio_player_wavesurfer_cursorcolor'],
'cursorWidth' => $settings['audio_player_wavesurfer_cursorwidth'],
'forceDecode' => $settings['audio_player_wavesurfer_forcedecode'],
'normalize' => $settings['audio_player_wavesurfer_normalize'],
'progressColor' => $settings['audio_player_wavesurfer_progresscolor'],
'responsive' => $settings['audio_player_wavesurfer_responsive'],
'waveColor' => $settings['audio_player_wavesurfer_wavecolor'],
'autoplayNextTrack' => $settings['audio_player_wavesurfer_playnexttrack'],
'autoplay' => $settings['audio_player_autoplay'],
'unique_id' => $settings['unique_id'],
];
// Format files for output.
$template_files = $this
->getItemRenderList($items);
foreach ($template_files as &$renderInfo) {
// Generate settings for this file.
$fileSettings = [
'id' => $renderInfo->id,
'path' => $renderInfo->url
->toString(),
];
// Check for Peak files.
if ($settings['audio_player_wavesurfer_use_peakfile'] && $this
->getClassType($renderInfo->item) == 'FileItem') {
// Load the associated file.
$file = File::load($renderInfo->item
->get('target_id')
->getCastedValue());
// Get the file URL.
$deliveryUrl = $file
->getFileUri();
// Get the file information so we can determin extension.
$deliveryFileInfo = pathinfo(file_create_url($deliveryUrl));
// Generate the URL for finding the peak file.
$peakData = [
'url' => dirname($deliveryUrl) . '/' . $deliveryFileInfo['filename'] . '.json',
'arguments' => '--pixels-per-second 20 --bits 8',
];
// Allow other modules to alter path data.
$this->moduleHandler
->alter('audiofield_wavesurfer_peak', $peakData);
// Get the real path.
$peakPath = $this->fileSystem
->realpath($peakData['url']);
// If the file is missing and Audiowaveform is installed.
if (!file_exists($peakPath) && audiofield_check_audiowaveform_installed()) {
$deliveryPath = escapeshellarg($this->fileSystem
->realpath($deliveryUrl));
$peakPath = escapeshellarg($peakPath);
$peakArguments = $peakData['arguments'];
// Generate the data file.
shell_exec("audiowaveform -i {$deliveryPath} -o {$peakPath} {$peakArguments}");
// If the file didn't generate, log/report the error.
if (!file_exists($peakData['url'])) {
$message_data = [
'@status_report' => Link::createFromRoute('status report', 'system.status')
->toString(),
];
$this->loggerFactory
->get('audiofield')
->warning('Warning: Unable to generate Waveform peak file. Please check your installation of audiowaveform. More information available in the @status_report.', $message_data);
$this->messenger
->addWarning($this
->t('Warning: Unable to generate Waveform peak file. Please check your installation of audiowaveform. More information available in the @status_report.', $message_data));
}
}
// If the file exists, add it to the jQuery and template settings.
if (file_exists($peakData['url'])) {
$renderInfo->peakpath = $fileSettings['peakpath'] = file_create_url($peakData['url']);
}
}
// Add this file to the render settings.
$player_settings['files'][] = $fileSettings;
}
return [
'audioplayer' => [
'#theme' => 'audioplayer',
'#plugin_id' => 'wavesurfer',
'#plugin_theme' => $player_settings['playertype'],
'#settings' => $settings,
'#files' => $template_files,
],
'downloads' => $this
->createDownloadList($items, $settings),
'#attached' => [
'library' => [
// Attach the wavesurfer library.
'audiofield/audiofield.' . $this
->getPluginLibraryName(),
],
'drupalSettings' => [
'audiofieldwavesurfer' => [
$settings['unique_id'] => $player_settings,
],
],
],
];
}
/**
* {@inheritdoc}
*/
public function getPluginLibraryVersion() {
// Parse the JSON file for version info.
$library_path = $this->fileSystem
->realpath(DRUPAL_ROOT . $this
->getPluginLibraryPath() . '/package.json');
$library_data = Json::decode(file_get_contents($library_path));
return $library_data['version'];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AudioFieldPluginBase:: |
protected | property | File System service. | |
AudioFieldPluginBase:: |
protected | property | Library discovery service. | |
AudioFieldPluginBase:: |
protected | property | Messenger service. | |
AudioFieldPluginBase:: |
protected | property | Messenger service. | |
AudioFieldPluginBase:: |
public | function | Checks to see if this audio plugin has been properly installed. | 1 |
AudioFieldPluginBase:: |
public | function | Checks to see if this audio plugin version is up to date. | |
AudioFieldPluginBase:: |
public | function | Used to render list of downloads as an item list. | |
AudioFieldPluginBase:: |
private | function | Get a title description from an audiofield entity. | |
AudioFieldPluginBase:: |
public | function | Get required rendering information from an entity. | |
AudioFieldPluginBase:: |
private | function | Get source URL from an audiofield entity. | |
AudioFieldPluginBase:: |
protected | function | Get the class type for an entity. | |
AudioFieldPluginBase:: |
private | function | Get the filetype for an item. | |
AudioFieldPluginBase:: |
public | function | Used to format file entities for use in the twig themes. | |
AudioFieldPluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginBase:: |
|
AudioFieldPluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginBase:: |
|
AudioFieldPluginBase:: |
public | function | Gets the main library instance of this plugin. | |
AudioFieldPluginBase:: |
public | function | Gets the name of the main library of the plugin instance. | |
AudioFieldPluginBase:: |
public | function | Gets the location of this plugin's library installation. | |
AudioFieldPluginBase:: |
public | function | Gets the remote download source from the plugin's main library. | |
AudioFieldPluginBase:: |
public | function | Gets the title of the plugin instance. | |
AudioFieldPluginBase:: |
private | function | Get a unique ID for an item. | |
AudioFieldPluginBase:: |
protected | function | Get a unique audofield ID. | |
AudioFieldPluginBase:: |
private | function | Load a file from an audio file entity. | |
AudioFieldPluginBase:: |
public | function | Used to render a default player (for fallback). | |
AudioFieldPluginBase:: |
public | function | Shows library installation errors for in-use libraries. | |
AudioFieldPluginBase:: |
protected | function | Validate that this entity will work with this player. | |
AudioFieldPluginBase:: |
private | function | Validate that a file entity will work with this player. | |
AudioFieldPluginBase:: |
private | function | Validate that a link entity will work with this player. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WavesurferAudioPlayer:: |
protected | property | Module handler service. | |
WavesurferAudioPlayer:: |
public static | function |
Creates an instance of the plugin. Overrides AudioFieldPluginBase:: |
|
WavesurferAudioPlayer:: |
public | function |
Parses library to get version number of installed library. Overrides AudioFieldPluginBase:: |
|
WavesurferAudioPlayer:: |
public | function |
Renders the player. Overrides AudioFieldPluginBase:: |
|
WavesurferAudioPlayer:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides AudioFieldPluginBase:: |