ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMediaPlayerGUI.php
Go to the documentation of this file.
1 <?php
2 
25 {
26  protected bool $force_audio_preview = false;
27  protected string $video_preview_pic_alt = "";
28  protected string $video_preview_pic = "";
29  protected string $alt_video_mime = "";
30  protected string $alt_video_file = "";
31  protected string $id = "";
33  protected ilLanguage $lng;
34  protected string $file = "";
35  protected int $displayHeight = 0;
36  protected int $displayWidth = 0;
37  protected string $mimeType = "";
38  protected static int $nr = 1;
39  protected static bool $lightbox_initialized = false;
40  protected int $current_nr = 0;
41  protected string $title = "";
42  protected string $description = "";
43  protected string $event_callback_url = "";
44  protected string $download_link = "";
45 
46  public function __construct(
47  string $a_id = "",
48  string $a_event_callback_url = ""
49  ) {
50  global $DIC;
51 
52  $this->tpl = $DIC->ui()->mainTemplate();
53  $this->lng = $DIC->language();
54  $this->id = $a_id;
55  $this->event_callback_url = $a_event_callback_url;
56  $this->current_nr = self::$nr;
57  self::$nr++;
58  }
59 
60  public function setFile(
61  string $a_file
62  ): void {
63  $this->file = $a_file;
64  }
65 
66  public function getFile(): string
67  {
68  return $this->file;
69  }
70 
71  public function setAlternativeVideoFile(
72  string $a_val
73  ): void {
74  $this->alt_video_file = $a_val;
75  }
76 
77  public function getAlternativeVideoFile(): string
78  {
79  return $this->alt_video_file;
80  }
81 
86  public function setAlternativeVideoMimeType(
87  string $a_val
88  ): void {
89  $this->alt_video_mime = $a_val;
90  }
91 
92  public function getAlternativeVideoMimeType(): string
93  {
94  return $this->alt_video_mime;
95  }
96 
97  public function setDisplayHeight(int $dHeight): void
98  {
99  $this->displayHeight = $dHeight;
100  }
101 
102  public function getDisplayHeight(): int
103  {
104  return $this->displayHeight;
105  }
106 
107  public function setDisplayWidth(int $a_val): void
108  {
109  $this->displayWidth = $a_val;
110  }
111 
112  public function getDisplayWidth(): int
113  {
114  return $this->displayWidth;
115  }
116 
117  public function setMimeType(string $value): void
118  {
119  $this->mimeType = $value;
120  }
121 
125  public function setVideoPreviewPic(
126  string $a_val,
127  string $a_alt = ""
128  ): void {
129  $this->video_preview_pic = $a_val;
130  $this->video_preview_pic_alt = $a_alt;
131  }
132 
133  public function getVideoPreviewPic(): string
134  {
136  }
137 
138  public function setTitle(string $a_val): void
139  {
140  $this->title = $a_val;
141  }
142 
143  public function getTitle(): string
144  {
145  return $this->title;
146  }
147 
148  public function setDescription(string $a_val): void
149  {
150  $this->description = $a_val;
151  }
152 
153  public function getDescription(): string
154  {
155  return $this->description;
156  }
157 
161  public function setForceAudioPreview(bool $a_val): void
162  {
163  $this->force_audio_preview = $a_val;
164  }
165 
166  public function getForceAudioPreview(): bool
167  {
169  }
170 
171  public function setDownloadLink(string $a_val): void
172  {
173  $this->download_link = $a_val;
174  }
175 
176  public function getDownloadLink(): string
177  {
178  return $this->download_link;
179  }
180 
181  public static function initJavascript(
182  ilGlobalTemplateInterface $a_tpl = null
183  ): void {
184  global $DIC;
185 
186  $tpl = $DIC["tpl"];
187 
188  if ($a_tpl == null) {
189  $a_tpl = $tpl;
190  }
191 
193 
194  $a_tpl->addJavascript("./Services/MediaObjects/js/MediaObjects.js?1");
195 
197  }
198 
202  public function getMp3PlayerHtml(
203  bool $a_preview = false
204  ): string {
205  $tpl = $this->tpl;
206  $lng = $this->lng;
207 
208  self::initJavascript($tpl);
209 
210  if (!self::$lightbox_initialized && $a_preview) {
211  $lb = new ilLightboxGUI("media_lightbox");
212  $lb->setWidth("660px");
213  $lb->addLightbox();
214  self::$lightbox_initialized = true;
215  }
216 
217  // youtube
220  $mp_tpl = new ilTemplate("tpl.flv_player.html", true, true, "Services/MediaObjects");
221  if ($a_preview) {
222  if ($this->getDownloadLink() != "") {
223  $mp_tpl->setCurrentBlock("ytdownload");
224  $mp_tpl->setVariable("TXT_DOWNLOAD", $lng->txt("download"));
225  $mp_tpl->setVariable("HREF_DOWNLOAD", $this->getDownloadLink());
226  $mp_tpl->parseCurrentBlock();
227  }
228 
229  $mp_tpl->setCurrentBlock("ytpreview");
230  if ($this->getVideoPreviewPic() != "") {
231  $mp_tpl->setVariable("IMG_SRC", $this->getVideoPreviewPic());
232  } else {
233  $mp_tpl->setVariable("IMG_SRC", ilUtil::getImagePath("mcst_preview.svg"));
234  }
235  $height = $this->getDisplayHeight();
236  $width = $this->getDisplayWidth();
237  $mp_tpl->setVariable("DISPLAY_HEIGHT", $height);
238  $mp_tpl->setVariable("DISPLAY_WIDTH", $width);
239  $mp_tpl->setVariable("IMG_ALT", $this->video_preview_pic_alt);
240  $mp_tpl->setVariable("PTITLE", $this->getTitle());
241  $mp_tpl->parseCurrentBlock();
242  }
243  $mp_tpl->setCurrentBlock("youtube");
244  if ($a_preview) {
245  $mp_tpl->setVariable("CLASS", "ilNoDisplay");
246  }
247  $mp_tpl->setVariable("SRC", "https://www.youtube.com/embed/" . $p["v"]);
248  $mp_tpl->setVariable("PLAYER_NR", $this->id . "_" . $this->current_nr);
249  $mp_tpl->setVariable("TXT_PLAY", $lng->txt("mob_play"));
250  $mp_tpl->setVariable("TITLE", $this->getTitle());
251  $mp_tpl->setVariable("DESCRIPTION", $this->getDescription());
252  if ($a_preview) {
253  $mp_tpl->setVariable("CLOSE", ilGlyphGUI::get(ilGlyphGUI::CLOSE));
254  }
255  $mp_tpl->parseCurrentBlock();
256  return $mp_tpl->get();
257  }
258 
259  // vimeo
261  $mp_tpl = new ilTemplate("tpl.flv_player.html", true, true, "Services/MediaObjects");
262  if ($a_preview) {
263  if ($this->getDownloadLink() != "") {
264  $mp_tpl->setCurrentBlock("ytdownload");
265  $mp_tpl->setVariable("TXT_DOWNLOAD", $lng->txt("download"));
266  $mp_tpl->setVariable("HREF_DOWNLOAD", $this->getDownloadLink());
267  $mp_tpl->parseCurrentBlock();
268  }
269 
270  $mp_tpl->setCurrentBlock("ytpreview");
271  if ($this->getVideoPreviewPic() != "") {
272  $mp_tpl->setVariable("IMG_SRC", $this->getVideoPreviewPic());
273  } else {
274  $mp_tpl->setVariable("IMG_SRC", ilUtil::getImagePath("mcst_preview.svg"));
275  }
276  $height = $this->getDisplayHeight();
277  $width = $this->getDisplayWidth();
278  $mp_tpl->setVariable("DISPLAY_HEIGHT", $height);
279  $mp_tpl->setVariable("DISPLAY_WIDTH", $width);
280  $mp_tpl->setVariable("IMG_ALT", $this->video_preview_pic_alt);
281  $mp_tpl->setVariable("PTITLE", $this->getTitle());
282  $mp_tpl->parseCurrentBlock();
283  }
284  $mp_tpl->setCurrentBlock("youtube");
285  if ($a_preview) {
286  $mp_tpl->setVariable("CLASS", "ilNoDisplay");
287  }
288  $mp_tpl->setVariable("SRC", $this->getFile() . "?controls=0");
289  $mp_tpl->setVariable("PLAYER_NR", $this->id . "_" . $this->current_nr);
290  $mp_tpl->setVariable("TITLE", $this->getTitle());
291  $mp_tpl->setVariable("DESCRIPTION", $this->getDescription());
292  include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
293  if ($a_preview) {
294  $mp_tpl->setVariable("CLOSE", ilGlyphGUI::get(ilGlyphGUI::CLOSE));
295  }
296  $mp_tpl->parseCurrentBlock();
297  return $mp_tpl->get();
298  }
299 
300  $mimeType = $this->mimeType == "" ? ilObjMediaObject::getMimeType(basename($this->getFile())) : $this->mimeType;
301 
302  // video tag
303  if (in_array($mimeType, array("video/mp4",
304  "video/m4v",
305  "video/rtmp",
306  "video/x-flv",
307  "video/webm",
308  "video/youtube",
309  "video/vimeo",
310  "video/ogg"
311  ))) {
312  $style = "";
313  if ($mimeType == "video/quicktime") {
314  $mimeType = "video/mov";
315  }
316 
317  $mp_tpl = new ilTemplate("tpl.flv_player.html", true, true, "Services/MediaObjects");
318 
319  // preview
320  if ($a_preview) {
321  if ($this->getDownloadLink() != "") {
322  $mp_tpl->setCurrentBlock("download");
323  $mp_tpl->setVariable("TXT_DOWNLOAD", $lng->txt("download"));
324  $mp_tpl->setVariable("HREF_DOWNLOAD", $this->getDownloadLink());
325  $mp_tpl->parseCurrentBlock();
326  }
327 
328  $mp_tpl->setCurrentBlock("preview");
329  if ($this->getVideoPreviewPic() != "") {
330  $mp_tpl->setVariable("IMG_SRC", $this->getVideoPreviewPic());
331  } else {
332  $mp_tpl->setVariable("IMG_SRC", ilUtil::getImagePath("mcst_preview.svg"));
333  }
334  $mp_tpl->setVariable("IMG_ALT", $this->video_preview_pic_alt);
335  $mp_tpl->setVariable("PTITLE", $this->getTitle());
336  $mp_tpl->parseCurrentBlock();
337  }
338  $mp_tpl->setCurrentBlock("mejs_video");
339 
340  if ($a_preview) {
341  $mp_tpl->setVariable("WRAP_CLASS", "ilNoDisplay");
342  $mp_tpl->setVariable("CLASS", "mejs__player ilNoDisplay");
343  }
344 
345  // sources
346  $mp_tpl->setVariable("FILE", $this->getFile());
347  $player_nr = $this->id . "_" . $this->current_nr;
348  $mp_tpl->setVariable("PLAYER_NR", $player_nr);
349  $mp_tpl->setVariable("TXT_PLAY", $lng->txt("mob_play"));
350 
351  $onload_code = "il.MediaObjects.setPlayerConfig('player_" . $player_nr .
352  "', {event_url: '" . $this->event_callback_url . "'});";
353 
354  $this->tpl->addOnLoadCode(
355  $onload_code
356  );
357 
358  $height = $this->getDisplayHeight();
359  $width = $this->getDisplayWidth();
360  if (is_int(strpos($mimeType, "audio/mpeg"))) {
361  //$height = "30px";
362  }
363 
364  if ($height != "") {
365  $style = "height: " . $height . "; ";
366  }
367  if ($width != "") {
368  $style .= "width: " . $width . "; ";
369  }
370  if ($style != "") {
371  $mp_tpl->setVariable("STYLE", "style='$style'");
372  }
373  $mp_tpl->setVariable("FILE", $this->getFile());
374  $mp_tpl->setVariable("PREVIEW_PIC", $this->getVideoPreviewPic());
375  $mp_tpl->setVariable("TITLE", $this->getTitle());
376  $mp_tpl->setVariable("DESCRIPTION", $this->getDescription());
377  if ($a_preview) {
378  $mp_tpl->setVariable("CLOSE", ilGlyphGUI::get(ilGlyphGUI::CLOSE));
379  }
380  $mp_tpl->parseCurrentBlock();
381  $r = $mp_tpl->get();
382 
383  if (!$a_preview) {
384  $tpl->addOnLoadCode("new MediaElementPlayer('player_" . $this->id . "_" . $this->current_nr . "');");
385  }
386 
387  //echo htmlentities($r); exit;
388  return $r;
389  }
390 
391  // audio/mpeg
392  if (is_int(strpos($mimeType, "audio/mpeg")) ||
393  in_array($mimeType, array("application/ogg", "audio/ogg"))) {
395  $mp_tpl = new ilTemplate("tpl.flv_player.html", true, true, "Services/MediaObjects");
396  $preview_output = false;
397  if ($this->getVideoPreviewPic() != "" || $this->getForceAudioPreview()) {
398  if ($this->getDownloadLink() != "") {
399  $mp_tpl->setCurrentBlock("adownload");
400  $mp_tpl->setVariable("TXT_DOWNLOAD", $lng->txt("download"));
401  $mp_tpl->setVariable("HREF_DOWNLOAD", $this->getDownloadLink());
402  $mp_tpl->parseCurrentBlock();
403  }
404 
405  $mp_tpl->setCurrentBlock("apreview");
406  if ($this->getVideoPreviewPic() != "") {
407  $mp_tpl->setVariable("IMG_SRC", $this->getVideoPreviewPic());
408  } else {
409  $mp_tpl->setVariable("IMG_SRC", ilUtil::getImagePath("mcst_preview.svg"));
410  }
411  $mp_tpl->setVariable("PTITLE", $this->getTitle());
412  $mp_tpl->parseCurrentBlock();
413  $preview_output = true;
414  }
415  $mp_tpl->setCurrentBlock("audio");
416  if ($preview_output) {
417  $mp_tpl->setVariable("ASTYLE", "margin-top:-40px");
418  }
419  $mp_tpl->setVariable("AFILE", $this->getFile());
420  $mp_tpl->setVariable("APLAYER_NR", $this->id . "_" . $this->current_nr);
421  $mp_tpl->setVariable("AEVENT_URL", $this->event_callback_url);
422  $mp_tpl->setVariable("AHEIGHT", "40");
423  $mp_tpl->setVariable("AWIDTH", "320");
424  $mp_tpl->parseCurrentBlock();
425  return $mp_tpl->get();
426  }
427 
428  // images
429  if (is_int(strpos($mimeType, "image/"))) {
430  $mp_tpl = new ilTemplate("tpl.flv_player.html", true, true, "Services/MediaObjects");
431 
432  if ($this->getDownloadLink() != "") {
433  $mp_tpl->setCurrentBlock("idownload");
434  $mp_tpl->setVariable("TXT_DOWNLOAD", $lng->txt("download"));
435  $mp_tpl->setVariable("HREF_DOWNLOAD", $this->getDownloadLink());
436  $mp_tpl->parseCurrentBlock();
437  }
438 
439  $mp_tpl->setCurrentBlock("ipreview");
440  if ($this->getVideoPreviewPic() != "") {
441  $mp_tpl->setVariable("IMG_SRC", $this->getVideoPreviewPic());
442  } else {
443  $mp_tpl->setVariable("IMG_SRC", $this->getFile());
444  }
445  $mp_tpl->setVariable("PTITLE", $this->getTitle());
446  $mp_tpl->parseCurrentBlock();
447 
448  $mp_tpl->setCurrentBlock("image");
449  $mp_tpl->setVariable("IFILE", $this->getFile());
450  $mp_tpl->setVariable("IPLAYER_NR", $this->id . "_" . $this->current_nr);
451  $mp_tpl->setVariable("ITITLE", $this->getTitle());
452  $mp_tpl->setVariable("IDESCRIPTION", $this->getDescription());
453  $mp_tpl->setVariable("ICLOSE", ilGlyphGUI::get(ilGlyphGUI::CLOSE));
454 
455  if ($this->event_callback_url) {
456  $mp_tpl->setVariable("IMG_CALLBACK_URL", $this->event_callback_url);
457  $mp_tpl->setVariable("IMG_CALLBACK_PLAYER_NR", $this->id . "_" . $this->current_nr);
458  }
459 
460  $mp_tpl->setVariable("IHEIGHT", $this->getDisplayHeight());
461  $mp_tpl->setVariable("IWIDTH", $this->getDisplayWidth());
462  $mp_tpl->parseCurrentBlock();
463 
464  return $mp_tpl->get();
465  }
466 
467  // fallback, no preview mode
468  $mimeType = $this->mimeType == "" ? ilObjMediaObject::getMimeType(basename($this->getFile())) : $this->mimeType;
469  if (strpos($mimeType, "flv") === false
470  && strpos($mimeType, "audio/mpeg") === false
471  && strpos($mimeType, "image/png") === false
472  && strpos($mimeType, "image/gif") === false) {
473  $html = '<embed src="' . $this->getFile() . '" ' .
474  'type="' . $mimeType . '" ' .
475  'ShowControls="1" ' .
476  'autoplay="false" autostart="false" ' .
477  'width="320" height="240" scale="aspect" ></embed>';
478  return $html;
479  }
480 
481  return "";
482  }
483 
484  public function getPreviewHtml(): string
485  {
486  return $this->getMp3PlayerHtml(true);
487  }
488 
492  public function getMediaPlayerHtml(): string
493  {
494  return $this->getMp3PlayerHtml(false);
495  }
496 }
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
setVideoPreviewPic(string $a_val, string $a_alt="")
Set video preview picture.
static get(string $a_glyph, string $a_text="")
setDescription(string $a_val)
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
ilGlobalTemplateInterface $tpl
static isYouTube(string $a_location)
Identify YouTube links.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static isVimeo(string $a_location)
Identify Vimeo links.
static bool $lightbox_initialized
getMediaPlayerHtml()
Get HTML (no preview) for media player integration.
setAlternativeVideoMimeType(string $a_val)
Set alternative video mime type.
static initJavascript(ilGlobalTemplateInterface $a_tpl=null)
global $DIC
Definition: feed.php:28
static getMimeType(string $a_file, bool $a_external=false)
get mime type for file
static extractYouTubeParameters(string $a_location)
Extract YouTube Parameter.
__construct(string $a_id="", string $a_event_callback_url="")
setAlternativeVideoFile(string $a_val)
setDownloadLink(string $a_val)
setForceAudioPreview(bool $a_val)
Force audio preview.
addOnLoadCode(string $a_code, int $a_batch=2)
Add on load code.
getMp3PlayerHtml(bool $a_preview=false)
Get Html for MP3 Player.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setDisplayHeight(int $dHeight)
static initConnection(?ilGlobalTemplateInterface $a_main_tpl=null)
Init YUI Connection module.
static initMediaElementJs(ilGlobalTemplateInterface $a_tpl=null)
Init mediaelement.js scripts.