api.player.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. class Player {
  3. /**
  4. * Current instance player width
  5. *
  6. * @var string
  7. */
  8. protected $width = '600px';
  9. /**
  10. * Current instance auto play flag
  11. *
  12. * @var bool
  13. */
  14. protected $autoPlayFlag = false;
  15. /**
  16. * Player JS lib implementation URL
  17. *
  18. * @var string
  19. */
  20. protected $playerLib = '';
  21. public function __construct($width = '', $autoPlay = false) {
  22. $this->setPlayerLib();
  23. if ($width) {
  24. $this->setWidth($width);
  25. }
  26. $this->setAutoplay($autoPlay);
  27. }
  28. /**
  29. * Sets current instance width
  30. *
  31. * @param string $width
  32. *
  33. * @return void
  34. */
  35. protected function setWidth($width) {
  36. $this->width = $width;
  37. }
  38. /**
  39. * Sets current instance autoplay
  40. *
  41. * @param bool $state
  42. *
  43. * @return void
  44. */
  45. protected function setAutoplay($state) {
  46. $this->autoPlayFlag = $state;
  47. }
  48. /**
  49. * Sets player lib property
  50. *
  51. * @param string $lib
  52. *
  53. * @return void
  54. */
  55. public function setPlayerLib($lib='w4') {
  56. $this->playerLib= 'modules/jsc/playerjs/'.$lib.'_playerjs.js';
  57. }
  58. /**
  59. * Renders web player for some playlist
  60. *
  61. * @param string $playlistPath - full playlist path
  62. * @param string $plStart - starting segment for playback
  63. * @param string $playerId - must be equal to channel name to access playlist in DOM
  64. * @param string $poster - optional channel screenshot
  65. *
  66. * @return string
  67. */
  68. public function renderPlaylistPlayer($playlistPath, $plStart = '', $playerId = '', $poster = '') {
  69. $autoPlay = ($this->autoPlayFlag) ? 'true' : 'false';
  70. $playerId = ($playerId) ? $playerId : 'plplayer' . wf_InputId();
  71. $poster = ($poster) ? ' poster:"' . $poster . '",' : '';
  72. $result = '';
  73. $result .= wf_tag('script', false, '', 'src="' . $this->playerLib . '"') . wf_tag('script', true);
  74. $result .= wf_tag('div', false, '', 'style="float:left; width:' . $this->width . '; margin:5px;"');
  75. $result .= wf_tag('div', false, 'archplayercontainer', 'id = "' . $playerId . '"') . wf_tag('div', true);
  76. $result .= wf_tag('script');
  77. $result .= 'var player = new Playerjs({id:"' . $playerId . '", ' . $poster . ' file:"' . $playlistPath . '", autoplay:' . $autoPlay . ' ' . $plStart . '});';
  78. $result .= wf_tag('script', true);
  79. $result .= wf_tag('div', true);
  80. $result .= wf_CleanDiv();
  81. return($result);
  82. }
  83. /**
  84. * Renders web player for some single file
  85. *
  86. * @param string $playlistPath - full playlist path
  87. * @param string $playerId - must be equal to channel name to access playlist in DOM
  88. * @param string $poster - optional channel screenshot
  89. * @return string
  90. */
  91. public function renderSinglePlayer($filePath, $playerId = '', $poster = '') {
  92. $autoPlay = ($this->autoPlayFlag) ? 'true' : 'false';
  93. $playerId = ($playerId) ? $playerId : 'singleplayer' . wf_InputId();
  94. $poster = ($poster) ? ' poster:"' . $poster . '",' : '';
  95. $result = '';
  96. $result .= wf_tag('script', false, '', 'src="' . $this->playerLib . '"') . wf_tag('script', true);
  97. $result .= wf_tag('div', false, '', 'style="float:left; width:' . $this->width . '; margin:5px;"');
  98. $result .= wf_tag('div', false, '', 'id = "' . $playerId . '" style="width:90%;"') . wf_tag('div', true);
  99. $result .= wf_tag('script');
  100. $result .= 'var player = new Playerjs({id:"' . $playerId . '", ' . $poster . ' file:"' . $filePath . '", autoplay:' . $autoPlay . '});';
  101. $result .= wf_tag('script', true);
  102. $result .= wf_tag('div', true);
  103. $result .= wf_CleanDiv();
  104. return($result);
  105. }
  106. /**
  107. * Renders web player for some single file
  108. *
  109. * @param string $playlistPath - full playlist path
  110. * @param string $playerId - must be equal to channel name to access playlist in DOM
  111. * @param string $poster - optional channel screenshot
  112. * @return string
  113. */
  114. public function renderLivePlayer($filePath, $playerId = '', $poster = '') {
  115. $autoPlay = ($this->autoPlayFlag) ? 'true' : 'false';
  116. $playerId = ($playerId) ? $playerId : 'singleplayer' . wf_InputId();
  117. $poster = ($poster) ? ' poster:"' . $poster . '",' : '';
  118. $result = '';
  119. $result .= wf_tag('script', false, '', 'src="' . $this->playerLib . '"') . wf_tag('script', true);
  120. $result .= wf_tag('div', false, '', 'style="float:left; width:' . $this->width . '; margin:5px;"');
  121. $result .= wf_tag('div', false, '', 'id = "' . $playerId . '" style="width:90%;"') . wf_tag('div', true);
  122. $result .= wf_tag('script');
  123. $result .= 'var player = new Playerjs({id:"' . $playerId . '", ' . $poster . ' file:"' . $filePath . '", autoplay:' . $autoPlay . '});';
  124. $result .= wf_tag('script', true);
  125. $result .= wf_tag('div', true);
  126. $result .= wf_CleanDiv();
  127. return($result);
  128. }
  129. }