BxPlyrPlayer.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Plyr Plyr player integration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Plyr player integration.
  13. * @see BxDolPlayer
  14. */
  15. class BxPlyrPlayer extends BxDolPlayer
  16. {
  17. /**
  18. * Standard view initialization params
  19. */
  20. protected static $CONF_STANDARD = '
  21. <div {attrs_wrapper}>
  22. <video {attrs}>
  23. {webm}
  24. {mp4}
  25. {captions}
  26. </video>
  27. </div>
  28. ';
  29. /**
  30. * Standard view initialization params for audio
  31. */
  32. protected static $CONF_STANDARD_AUDIO = '
  33. <div {attrs_wrapper}>
  34. <audio {attrs}>
  35. {mp3}
  36. </audio>
  37. </div>
  38. ';
  39. /**
  40. * Minimal view initialization params
  41. */
  42. protected static $CONF_MINI = "";
  43. /**
  44. * Embed view initialization params
  45. */
  46. protected static $CONF_EMBED = "";
  47. protected $_oTemplate;
  48. protected $_bJsCssAdded = false;
  49. public function __construct ($aObject, $oTemplate)
  50. {
  51. parent::__construct ($aObject);
  52. if ($oTemplate)
  53. $this->_oTemplate = $oTemplate;
  54. else
  55. $this->_oTemplate = BxDolTemplate::getInstance();
  56. $this->_aSkins = array(
  57. );
  58. }
  59. public function getCodeAudio ($iViewMode, $aParams, $bDynamicMode = false)
  60. {
  61. $aControls = explode(',', 'play-large,play,progress,current-time,mute,volume,pip,airplay');
  62. $aSettings = array();
  63. return $this->_getCodePlyr (self::$CONF_STANDARD_AUDIO, 'bx-player-plyr', $aControls, $aSettings, $iViewMode, $aParams, $bDynamicMode);
  64. }
  65. public function getCodeVideo ($iViewMode, $aParams, $bDynamicMode = false)
  66. {
  67. $aControls = explode(',', getParam('bx_plyr_option_controls'));
  68. $aSettings = explode(',', getParam('bx_plyr_option_settings'));
  69. return $this->_getCodePlyr (self::$CONF_STANDARD, 'bx-player-plyr', $aControls, $aSettings, $iViewMode, $aParams, $bDynamicMode);
  70. }
  71. protected function _getCodePlyr ($sInit, $sClass, $aControls, $aSettings, $iViewMode, $aParams, $bDynamicMode = false)
  72. {
  73. // set visual mode
  74. switch ($iViewMode) {
  75. case BX_PLAYER_MINI:
  76. $sClass .= ' bx-player-plyr-mini';
  77. break;
  78. case BX_PLAYER_EMBED:
  79. $sClass .= ' bx-player-plyr-embed';
  80. break;
  81. case BX_PLAYER_STANDARD:
  82. default:
  83. $sClass .= ' bx-player-plyr-standard';
  84. break;
  85. }
  86. // attrs
  87. $sId = isset($aParams['attrs']) && isset($aParams['attrs']['id']) ? $aParams['attrs']['id'] : 'BxPlyr' . mt_rand();
  88. $aAttrsDefault = array(
  89. 'id' => $sId,
  90. 'controls' => '',
  91. 'controlsList' => 'nodownload',
  92. 'preload' => 'none',
  93. 'autobuffer' => '',
  94. 'class' => $sClass,
  95. );
  96. $aAttrs = isset($aParams['attrs']) && is_array($aParams['attrs']) ? $aParams['attrs'] : array();
  97. $aAttrs = array_merge($aAttrsDefault, $aAttrs);
  98. if (isset($aParams['poster']) && is_string($aParams['poster']))
  99. $aAttrs['poster'] = $aParams['poster'];
  100. $sAttrs = bx_convert_array2attrs($aAttrs);
  101. $aAttrsWrapper = isset($aParams['attrs_wrapper']) && is_array($aParams['attrs_wrapper']) ? $aParams['attrs_wrapper'] : array();
  102. $sAttrsWrapper = bx_convert_array2attrs($aAttrsWrapper, 'bx-player-plyr-wrapper', isset($aParams['styles']) && is_string($aParams['styles']) ? $aParams['styles'] : false);
  103. // generate files list for HTML5 player
  104. $aTypes = array(
  105. 'webm' => '<source type="video/webm" src="{url}" size="{size}" />',
  106. 'mp4' => '<source type="video/mp4" src="{url}" size="{size}" />',
  107. 'mp3' => '<source type="audio/mpeg" src="{url}" />',
  108. );
  109. $mp4 = '';
  110. $webm = '';
  111. $mp3 = '';
  112. foreach ($aTypes as $s => $ss) {
  113. if (!isset($aParams[$s]))
  114. continue;
  115. if (is_array($aParams[$s])) {
  116. foreach ($aParams[$s] as $sType => $sUrl) {
  117. if (empty($sUrl) || !isset($this->_aSizes[$sType]))
  118. continue;
  119. $$s .= str_replace(
  120. array('{url}', '{size}'),
  121. array($sUrl, $this->_aSizes[$sType]),
  122. $ss
  123. );
  124. }
  125. }
  126. elseif (is_string($aParams[$s]) && !empty($aParams[$s]))
  127. $$s = str_replace('{url}', $aParams[$s], $ss);
  128. else
  129. $$s = '';
  130. }
  131. // player code
  132. $sCode = $this->_replaceMarkers($sInit, array(
  133. 'attrs' => $sAttrs,
  134. 'attrs_wrapper' => $sAttrsWrapper,
  135. 'webm' => $webm,
  136. 'mp4' => $mp4,
  137. 'mp3' => $mp3,
  138. 'captions' => isset($aParams['captions']) ? $aParams['captions'] : '',
  139. ));
  140. // plyr initialization
  141. if (!($sFormat = getParam('sys_player_default_format')))
  142. $sFormat = 'sd';
  143. $aOptions = array_merge(array(
  144. // 'debug' => true,
  145. 'quality' => array('default' => $this->_aSizes[$sFormat]),
  146. 'displayDuration' => false,
  147. 'controls' => $aControls,
  148. 'settings' => $aSettings,
  149. 'ratio' => '16:9',
  150. ), $this->_aConfCustom);
  151. $sInitEditor = "
  152. glBxPlyr" . mt_rand() . " = new Plyr('#$sId', " . json_encode($aOptions) . ");
  153. ";
  154. // load necessary JS and CSS
  155. if ($bDynamicMode) {
  156. list($aJs, $aCss) = $this->_getJsCss(true);
  157. $sCss = $this->_oTemplate->addCss($aCss, true);
  158. $sScript = $sCss . "<script>
  159. if ('undefined' == typeof(window.Plyr)) {
  160. bx_get_scripts(" . json_encode($aJs) . ", function () {
  161. $sInitEditor
  162. });
  163. } else {
  164. setTimeout(function () {
  165. $sInitEditor
  166. }, 10); // wait while html is rendered in case of dynamic adding html
  167. }
  168. </script>";
  169. } else {
  170. $sScript = "
  171. <script>
  172. $(document).ready(function () {
  173. if (bx_is_selector_in_stylesheet('.plyr')) {
  174. $sInitEditor
  175. }
  176. else {
  177. setTimeout(function () {
  178. $sInitEditor
  179. }, 250);
  180. }
  181. });
  182. </script>";
  183. }
  184. return $this->_addJsCss($bDynamicMode) . $sScript . $sCode;
  185. }
  186. /**
  187. * Add css/js files which are needed for editor display and functionality.
  188. */
  189. protected function _addJsCss($bDynamicMode = false, $sInitEditor = '')
  190. {
  191. if ($bDynamicMode)
  192. return '';
  193. if ($this->_bJsCssAdded)
  194. return '';
  195. list($aJs, $aCss) = $this->_getJsCss();
  196. $this->_oTemplate->addJs($aJs);
  197. $this->_oTemplate->addCss($aCss);
  198. $this->_bJsCssAdded = true;
  199. return '';
  200. }
  201. protected function _getJsCss($bUseUrlsForJs = false)
  202. {
  203. $sJsPrefix = $bUseUrlsForJs ? BX_DOL_URL_MODULES : BX_DIRECTORY_PATH_MODULES;
  204. $sJsSuffix = $bUseUrlsForJs ? '' : '|';
  205. $aJs = array(
  206. $sJsPrefix . 'boonex/plyr/plugins/plyr/' . $sJsSuffix . 'plyr.min.js',
  207. );
  208. $aCss = array(
  209. BX_DIRECTORY_PATH_MODULES . 'boonex/plyr/plugins/plyr/|plyr.css',
  210. BX_DIRECTORY_PATH_MODULES . 'boonex/plyr/template/css/|main.css',
  211. );
  212. return array($aJs, $aCss);
  213. }
  214. }
  215. /** @} */