A useful API function would be mediaelement_render($file, Array $options = array()).

It could be used to replace

  $settings = array(
    '#a-unique-id' => array(
      'controls' => TRUE,
      'opts' => array(),
    ),
  );

  // Requires patch from drupal.org/node/1132746
  mediaelement_add_js($settings);

  $variables = array(
    'attributes' => array(
      'src' => $file,
      'id' => 'a-unique-id',
    ),
    'settings' => array(
      'download_link' => FALSE,
    ),
  );

  $output = theme('mediaelement_audio', $variables);

with something like

$output = mediaelement_render($file);

It would also take a parameter for optional settings. I suggest the following;

  • id; Optionally set the unique ID. Otherwise MediaElement module creates a unique one using a static count variable.
  • width; Width of the player in pixels or percentage.
  • height; Height of the player in pixels or percentage.
  • controls; Disable or enable the controls.
  • opts; MediaElement.js API options.
  • attributes; Override defaults for theme_mediaelement_audio().
  • settings; Override defaults for theme_mediaelement_audio().
  • type; video or audio. Otherwise MediaElement module determines it by the file extension of $file.

The first parameter $file would become $attributes['src']. $options['id'] would become $attributes['id']. $options['width'] and $options['height'] would be handled similarly. $options['controls'], $options['opts'], $options['attributes'], $options['settings'], are obviously parameters for theme_mediaelement_audio() and drupal_add_js().

$options['type'] would determine which theme function is called; theme_mediaelement_audio() or theme_mediaelement_video(). Perhaps these could be abstracted into theme_mediaelement_element() so that theme_mediaelement_audio() and theme_mediaelement_video() simply become wrapper functions for theme_mediaelement_element(), since the two functions are identical except for the keywords "video" and "audio".

Comments

Bevan’s picture

#1132746: API function: mediaelement_add_js() is related.

autoplay would also be a good option.

mfer’s picture

sub

mfer’s picture

I'm looking at this and wondering how to do this as a renderable array. This is especially important when the media is presented in a block you want to cache. Same applies to _add_js issue.

Bevan’s picture

If I recall correctly you can make up your own Form API properties, as long as they are prefixed with a hash character. You can then also set the #theme callback that is designated to rendering the element in question.

pbuyle’s picture

Instead of providing a function to generate an HTML string, what about an hook_element_info() implementation to provide (non-input) element types.

Then using the Render Array API or the Form API, one could use

$element = array(
  '#type' => 'mediaelement_video',
  '#file' => $file,
  '#attributes' => array(
    'id' => 'a-unique-id',
  ),
  '#settings' => array(
    'download_link' => FALSE,
  ),
);
jnettik’s picture

Status: Needs review » Closed (outdated)

Old ticket. Closing.