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

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 can use media players in their plugins for displaying a file or URL.

Via multimedia plugins filters

Multimedia plugins filter is automatically enabled and admins hardly ever disable it. The simplest way to engage media players is to call format_text() on the link to the media file, for example:

echo format_text(html_writer::link($urltofile, 'Text for fallback'), FORMAT_HTML, ['context'=>$context]);

Via media manager

If you want to use advanced options such as specifying alternative URLs/files, setting size or passing other options you can use media manager directly. Below is an example from mod_resource.

Create instance of moodle_url objects to link to the media file.

   $moodleurl = new moodle_url(....); // Create instance of moodle_url objects to link to the media file. You can use moodle_url::make_pluginfile_url() to form a link to a plugin file.
   $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...