Skip to content

Instantly share code, notes, and snippets.

@benjaminrau
Last active December 19, 2015 08:39
Show Gist options
  • Save benjaminrau/5927125 to your computer and use it in GitHub Desktop.
Save benjaminrau/5927125 to your computer and use it in GitHub Desktop.
Vhs HTML5 Audioplayer Viewhelper
<?php
/***************************************************************
* Copyright notice
*
* (c) 2013 Björn Fromme <fromme@dreipunktnull.com>, dreipunktnull
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */
/**
* Renders HTML code to embed a HTML5 audio player. NOTICE: This is
* all HTML5 and won't work on browsers like IE8 and below. Include
* some helper library like http://kolber.github.io/audiojs/ if you need to support those.
*
* @author Björn Fromme <fromme@dreipunktnull.com>, dreipunktnull
* @package Vhs
* @subpackage ViewHelpers\Media
*/
class Tx_Vhs_ViewHelpers_Media_AudioViewHelper extends Tx_Vhs_ViewHelpers_Media_AbstractMediaTagViewHelper {
/**
* @var string
*/
protected $tagName = 'audio';
/**
* @var array
*/
protected $validTypes = array('mp3', 'wav', 'ogg');
/**
* Initialize arguments.
*
* @return void
* @api
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerArgument('autoplay', 'boolean', 'Specifies that the audio will start playing as soon as it is ready.', FALSE, FALSE);
$this->registerArgument('controls', 'boolean', 'Specifies that audio controls should be displayed (such as a play/pause button etc).', FALSE, FALSE);
$this->registerArgument('loop', 'boolean', 'Specifies that the playback will start over again, every time it is finished.', FALSE, FALSE);
$this->registerArgument('autobuffer', 'boolean', 'Specifies if and how the author thinks the audio should be loaded when the page loads.', FALSE, FALSE);
}
/**
* Render method
*
* @throws Tx_Fluid_Core_ViewHelper_Exception
* @return string
*/
public function render() {
$sources = $this->getSourcesFromArgument();
if (0 == count($sources)) {
throw new Tx_Fluid_Core_ViewHelper_Exception('No audio sources provided.', 1359382189);
}
foreach ($sources as $source) {
if (FALSE === isset($source['src'])) {
throw new Tx_Fluid_Core_ViewHelper_Exception('Missing value for "src" in sources array.', 1359381250);
}
$src = $source['src'];
if (FALSE === isset($source['type'])) {
throw new Tx_Fluid_Core_ViewHelper_Exception('Missing value for "type" in sources array.', 1359381255);
}
if (FALSE === in_array(strtolower($source['type']), $this->validTypes)) {
throw new Tx_Fluid_Core_ViewHelper_Exception('Invalid audio type "' . $source['type'] . '".', 1359381260);
}
$type = 'audio/' . strtolower($source['type']);
$src = $this->preprocessSourceUri($src);
$this->renderChildTag('source', array('src' => $src, 'type' => $type), 'append');
}
if (TRUE === (boolean) $this->arguments['autoplay']) {
$tagAttributes['autoplay'] = 'autoplay';
}
if (TRUE === (boolean) $this->arguments['controls']) {
$tagAttributes['controls'] = 'controls';
}
if (TRUE === (boolean) $this->arguments['loop']) {
$tagAttributes['loop'] = 'loop';
}
if (TRUE === (boolean) $this->arguments['autobuffer']) {
$tagAttributes['autobuffer'] = 'autobuffer';
}
$this->tag->addAttributes($tagAttributes);
return $this->tag->render();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment