Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Media players

From MoodleDocs
Revision as of 08:27, 30 November 2016 by Marina Glancy (talk | contribs) (Created page with "=Introduction= Media players are used to automatically embed files in the pages. It is normally video or audio files or links to media sharing sites such as YouTube or Vimeo....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Introduction

Media players are used to automatically embed files in the pages. It is normally video or audio files or links to media sharing sites such as YouTube or Vimeo. However it is also possible to use media players for embedding other contents - diagrams, formulas, etc. Media players usually look at the file extension to match with the player but they can also make a URL match (this can be used for YouTube, Vimeo and similar sites links).

In standard Moodle package media players are used:

- When [Multimedia plugin filter|https://docs.moodle.org/en/Multimedia_plugins_filter] - When using URL or File resources and also in one place in Lesson module

Using media players

Plugin developers that want to use media players in their plugins for displaying a file or URL. This is an example from mod_resource. First of all we create a moodle_url instance from the link to the file that is going to be embedded

   $mediamanager = core_media_manager::instance();
   $embedoptions = array(
       core_media_manager::OPTION_TRUSTED => true, // Only add it for trusted contents if current user has respective capability with RISK_XSS mask.
       core_media_manager::OPTION_BLOCK => true,
   );
   if ($mediamanager->can_embed_url($moodleurl, $embedoptions)) {
       $code = $mediamanager->embed_url($moodleurl, $title, 0, 0, $embedoptions);
   } else {
       // Put your own fallback here, for example a link to the file. Look at example in mod_resource, it has special treatment for images and PDF files.
   }

Advanced usage is to use methods can_embed_urls() and embed_urls() that allow to embed one of the alternatives, for example, when video is uploaded in several formats and the player will select the format that it wants.

Developing media players

Media player plugins are located in media/player/ directory and follow standard structure of Moodle plugins. Additionally they must contain the file in classes/plugin.php containing the class media_PLUGINNAME_plugin extending core_media_player.

Class core_media_player contains all methods that can be overridden in individual players. We won't cover all methods from the parent class here since the phpdocs are self-explanatory but there are several things that should be mentioned:

Matching

... to be continued...

Fallbacks

... to be continued...