Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
bsuttis CreditAttribution: bsuttis commentedI'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:
No luck so far.
Comment #2
bsuttis CreditAttribution: bsuttis commentedFuthermore, 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.
Comment #3
md2 CreditAttribution: md2 commentedbsuttis,
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();
I wasn't able to attach or use the success option. So close, yet so far!
Comment #4
scotwith1twhat 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!
Comment #5
Nick Lewis CreditAttribution: Nick Lewis commentedThis works perfectly for me:
Comment #6
scotwith1tIn 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.
Comment #7
pal95 CreditAttribution: pal95 commentedso i should be able to paste this straight into template.php?
Comment #8
scotwith1tHook 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
Comment #9
pal95 CreditAttribution: pal95 commentedwow, that worked, never thought i would have to make my own module.
thanks.
Comment #10
martysteer CreditAttribution: martysteer commentedThere 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.
Comment #11
Maedi CreditAttribution: Maedi commentedFor 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.
Comment #12
jnettik