最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to add custom button inside html player using mediaelement,js script - Stack Overflow

programmeradmin1浏览0评论

I wanted to add a extra button "HD" near caption inside html5 player. Added this piece of code inside mediaelementplayer.js file.

//HD button display starts
(function ($) {
$.extend(MediaElementPlayer.prototype, {
    buildcontextmenu: function (player, controls, layers, media) {

        // create HD button
        $('<div class="mejs-button mejs-hd-button"><span>HD<span/></div>')
       .appendTo(controls);

    }
});
})(mejs.$);
//HD button display stops

can anyone help to solve this issue? As of now mediaelementplayer.js by johndyer doesnot support HD on/off button. Reference / by johndyer

I wanted to add a extra button "HD" near caption inside html5 player. Added this piece of code inside mediaelementplayer.js file.

//HD button display starts
(function ($) {
$.extend(MediaElementPlayer.prototype, {
    buildcontextmenu: function (player, controls, layers, media) {

        // create HD button
        $('<div class="mejs-button mejs-hd-button"><span>HD<span/></div>')
       .appendTo(controls);

    }
});
})(mejs.$);
//HD button display stops

can anyone help to solve this issue? As of now mediaelementplayer.js by johndyer doesnot support HD on/off button. Reference http://mediaelementjs./ by johndyer

Share Improve this question asked Feb 12, 2015 at 6:22 Asif IqbalAsif Iqbal 5431 gold badge10 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You need to do it as follows (this is an example for a Loop button):

MediaElementPlayer.prototype.buildloop = function(player, controls, layers, media) {
    var
        // create the loop button
        loop =
        $('<div class="mejs-button mejs-loop-button ' + ((player.options.loop) ? 'mejs-loop-on' : 'mejs-loop-off') + '">' +
            '<span></span>' +
        '</div>')
        // append it to the toolbar
        .appendTo(controls)
        // add a click toggle event
        .click(function() {
            player.options.loop = !player.options.loop;
            if (player.options.loop) {
                loop.removeClass('mejs-loop-off').addClass('mejs-loop-on');
            } else {
                loop.removeClass('mejs-loop-on').addClass('mejs-loop-off');
            }
        });
}

Then, when creating your video player you can just add your variable to the features list for example:

$('video,audio').mediaelementplayer({
    features: ['loop','playpause','current','progress','duration','fullscreen'],
    alwaysShowControls: true,
});

Thank you @Sam, i used your code and wrote a vanilla version of your solution. This one adds two buttons to adjust the volume, a plus and a minus button, to make 10 steps for adjustment. (mediaelementjs 4.2.8)

Javascript:

var audio_player = document.getElementById('audio-player').children[0];

MediaElementPlayer.prototype.buildvolume_plus = function(player, controls) {
  var
    volume = document.createElement('div'),
    volume_button = document.createElement('button');
  volume.className = 'mejs__button mejs__volumeplus-button';
  volume_button.type = 'button';
  volume.appendChild(volume_button);

  controls.appendChild(volume);

  volume_button.addEventListener('click', function() {
    player.setVolume( player.volume < 1 ? Math.round( (player.volume + .1 )  * 10) / 10 : 1 );

  })
};
MediaElementPlayer.prototype.buildvolume_minus = function(player, controls) {
  var
    volume = document.createElement('div'),
    volume_button = document.createElement('button');
  volume.className = 'mejs__button mejs__volumeminus-button';
  volume_button.type = 'button';
  volume.appendChild(volume_button);

  controls.appendChild(volume);

  volume_button.addEventListener('click', function() {
    player.setVolume( player.volume > 0 ? Math.round( (player.volume - .1 )  * 10) / 10 : 0 );
  })
};

new MediaElementPlayer(audio_player);

HTML:

<div id="audio-player">
  <audio src="http://example." width="220" height="60" controls data-mejsoptions=\'{"features": ["playpause", "volume_plus", "volume_minus"]}\'></audio>
</div>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论