I was just looking for some advice as to where/how to inject my own mediaelement.js options when using the drupal module.

Options such as:

// shows debug errors on screen
    enablePluginDebug: false,
    // remove or reorder to change plugin priority
    plugins: ['flash','silverlight'],
    // specify to force MediaElement to use a particular video or audio type
    type: '',
    // path to Flash and Silverlight plugins
    pluginPath: '/myjsfiles/',
    // name of flash file
    flashName: 'flashmediaelement.swf',
    // name of silverlight file
    silverlightName: 'silverlightmediaelement.xap',
    // default if the <video width> is not specified
    defaultVideoWidth: 480,
    // default if the <video height> is not specified    
    defaultVideoHeight: 270,
    // overrides <video width>
    pluginWidth: -1,
    // overrides <video height>      
    pluginHeight: -1,
    // rate in milliseconds for Flash and Silverlight to fire the timeupdate event
    // larger number is less accurate, but less strain on plugin->JavaScript bridge
    timerRate: 250,
    // method that fires when the Flash or Silverlight object is ready
    success: function (mediaElement, domObject) {
         
        // add event listener
        mediaElement.addEventListener('timeupdate', function(e) {
             
            document.getElementById('current-time').innerHTML = mediaElement.currentTime;
             
        }, false);
         
        // call the play method
        mediaElement.play();
         
    },
    // fires when a problem is detected
    error: function () {
     
    }

I'm particularly interested in addEventListener such as 'ended' to do something at the end of the video. Any help would be greatly appreciated.

Kind Regards,
Mark

Comments

bsuttis’s picture

I've trying to do the above as well.

More specifically, I want to be able to add eventListeners using the success option.

I'm having trouble even hacking the module's js to get it working -- I've added this to mediaelement.module:

     $js_settings['opts']['success'] = "success:function(player, node){console.log(player);}";

No luck so far.

bsuttis’s picture

Futhermore, if i add video markup to the site by hand and in my theme's js do the following, it works, so I'm just not sure how to get it going in conjunction with the module.

$('#player1').mediaelementplayer({
  success: function (player, node) {
    console.log(player);
  }
});
md2’s picture

bsuttis,

I was able to get the options inserted using the following code. It can be included anytime before the page gets rendered I believe. I had mine in a custom module in a hook_node_view();

/*mediaelements js insertion*/
$settings = array('mediaelement' => array(
  'audio,video' => array(
  'controls' => TRUE,
  // This is the mediaelement scripts options.
  'opts' => array(
    'enablePluginDebug' => TRUE,
    'startVolume' => 0.5,
    ),
  )
));
drupal_add_js($settings, 'setting');

I wasn't able to attach or use the success option. So close, yet so far!

scotwith1t’s picture

what about if you're using mediaelement to render an audio field in a view? any idea how to go about that?

EDIT: looks like using similar code (using the options i needed) works with hook_views_pre_render(), so that was easier than i thought!

 function mymodule_views_pre_render(&$view) {
/*mediaelements js insertion*/
  $settings = array(
    'mediaelement' => array(
      'audio' => array(
        'controls' => TRUE,
        'opts' => array (
          'features' => array('playpause'),
          'startVolume' => 0.5,
          'playpause' => TRUE,
          /* make it just the size of my button */
          'audioWidth' => 24,
          'audioHeight' => 24,
          ),
       ),
    ),
);
drupal_add_js($settings, 'setting');
}
Nick Lewis’s picture

Status: Active » Needs review

This works perfectly for me:

/* 
Your module's implementation of hook_js_alter
*/
function hook_js_alter(&$javascript) {
  foreach($javascript['settings']['data'] as $k => $v) {
    if(isset($v['mediaelement'])) {
      foreach($v['mediaelement'] as $class => $options) {
       $javascript['settings']['data'][$k]['mediaelement'][$class]['opts']['audioWidth'] = 700; 
      }
    }
  }
}

scotwith1t’s picture

In my case, the hook_views_pre_render works better since I can limit it per views display with a switch/case statement...for instance, i want it to display the full player on the user's profile but only a little button on a views list with this field as part of the results. If you want to make the changes everywhere, the above works. If you want to get more specific my approach in #4 with an if statement or switch/case would be better...unless the hook_js_alter has a way to specify certain pages and i'm not seeing it.

pal95’s picture

so i should be able to paste this straight into template.php?

function mediaelement_js_alter(&$javascript) {
  foreach($javascript['settings']['data'] as $k => $v) {
    if(isset($v['mediaelement'])) {
      foreach($v['mediaelement'] as $class => $options) {
       $javascript['settings']['data'][$k]['mediaelement'][$class]['opts']['alwaysShowControls'] = true; 
      }
    }
  }
}
scotwith1t’s picture

Hook overrides go in a custom module. I almost always end up creating one of these for random snippets like this, but all you need is a mymodule folder in your sites/all/modules folder with a mymodule.info and a mymodule.module file.
the .info file just needs this to get started and you can start putting any hook overrides in the .module file

name = My module
description = Provide random snippets for overriding other functions on my site
core = 6.x
package = Other
pal95’s picture

wow, that worked, never thought i would have to make my own module.
thanks.

martysteer’s picture

There are two approaches here, javascript altering the setting array and javascript inserting into settings array. Both work okay for changing setting values, however neither of these techniques allow you to add success function callbacks to the mediaelement objects.

I think that using the drupal js settings to pass settings into the mediaelement on init is what's inhibiting 'success' callback functionality:
http://stackoverflow.com/questions/3855436/drupal-add-a-js-function-call...

Could the module maintain it's own JSON settings object somehow such that the field formatters inherit and override this object when initalising and any manual media items use the globals?
Or possibly there is a method in the mediaelement.js which allows you to attach functions to events after init success:? If so, events could just be attached with a custom theme js and the module can continue to use drupal.settings for just mediaelement settings values.

Maedi’s picture

For me the simplest way is to override the modules JS with a copy that has my own options attached:
Go to js2coffee.org if you'd like to use JavaScript.

# When set to enable mediaelement for all audio/video files add it to the page.
Drupal.behaviors.mediaelement = attach: (context, settings) ->
  if settings.mediaelement isnt undefined
    
    $.each settings.mediaelement, (selector, options) ->
      opts = undefined
      
      $(selector, context).once "mediaelement", ->
        options.opts.success = (player, node) ->

          player.addEventListener "playing", ((e) ->
            console.log 'playing'
          ), false  
		  
          player.addEventListener "ended", ((e) ->
            console.log 'ended'
          ), false            
      
        if options.controls
          $(this).mediaelementplayer options.opts
        else
          $(this).mediaelement()
  
  # The global option is seperate from the other selectors as it should be run after any other selectors.
  if settings.mediaelementAll isnt undefined
    $("video,audio", context).once "mediaelement", ->
      $(this).mediaelementplayer()
jnettik’s picture

Version: 7.x-1.2 » 8.x-1.x-dev
Status: Needs review » Active