View source
<?php
namespace Drupal\audiofield\Plugin\AudioPlayer;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\audiofield\AudioFieldPluginBase;
use Drupal\Component\Serialization\Json;
use Drupal\file\Entity\File;
use Drupal\Core\Link;
use Drupal\Core\Asset\LibraryDiscovery;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Extension\ModuleHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WavesurferAudioPlayer extends AudioFieldPluginBase {
protected $moduleHandler;
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;
}
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'));
}
public function renderPlayer(FieldItemListInterface $items, $langcode, array $settings) {
if (!$this
->checkInstalled()) {
$this
->showInstallError();
return $this
->renderDefaultPlayer($items, $settings);
}
$template_files = [];
$settings['unique_id'] = $this
->getUniqueRenderId();
$player_settings = [
'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'],
];
$template_files = $this
->getItemRenderList($items);
foreach ($template_files as &$renderInfo) {
$fileSettings = [
'id' => $renderInfo->id,
'path' => $renderInfo->url
->toString(),
];
if ($settings['audio_player_wavesurfer_use_peakfile'] && $this
->getClassType($renderInfo->item) == 'FileItem') {
$file = File::load($renderInfo->item
->get('target_id')
->getCastedValue());
$deliveryUrl = $file
->getFileUri();
$deliveryFileInfo = pathinfo(file_create_url($deliveryUrl));
$peakData = [
'url' => dirname($deliveryUrl) . '/' . $deliveryFileInfo['filename'] . '.json',
'arguments' => '--pixels-per-second 20 --bits 8',
];
$this->moduleHandler
->alter('audiofield_wavesurfer_peak', $peakData);
$peakPath = $this->fileSystem
->realpath($peakData['url']);
if (!file_exists($peakPath) && audiofield_check_audiowaveform_installed()) {
$deliveryPath = escapeshellarg($this->fileSystem
->realpath($deliveryUrl));
$peakPath = escapeshellarg($peakPath);
$peakArguments = $peakData['arguments'];
shell_exec("audiowaveform -i {$deliveryPath} -o {$peakPath} {$peakArguments}");
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 (file_exists($peakData['url'])) {
$renderInfo->peakpath = $fileSettings['peakpath'] = file_create_url($peakData['url']);
}
}
$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' => [
'audiofield/audiofield.' . $this
->getPluginLibraryName(),
],
'drupalSettings' => [
'audiofieldwavesurfer' => [
$settings['unique_id'] => $player_settings,
],
],
],
];
}
public function getPluginLibraryVersion() {
$library_path = $this->fileSystem
->realpath(DRUPAL_ROOT . $this
->getPluginLibraryPath() . '/package.json');
$library_data = Json::decode(file_get_contents($library_path));
return $library_data['version'];
}
}