annotate johndyer-mediaelement-13fa20a/src/js/mep-feature-volume.js @ 25:4a4bd554b4c1 tip

Closing this sub branch.
author Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk>
date Mon, 25 Mar 2013 14:02:54 +0000
parents 032bc65ebafc
children
rev   line source
gyorgy@0 1 (function($) {
gyorgy@0 2 MediaElementPlayer.prototype.buildvolume = function(player, controls, layers, media) {
gyorgy@0 3 var mute =
gyorgy@0 4 $('<div class="mejs-button mejs-volume-button mejs-mute">'+
gyorgy@0 5 '<button type="button"></button>'+
gyorgy@0 6 '<div class="mejs-volume-slider">'+ // outer background
gyorgy@0 7 '<div class="mejs-volume-total"></div>'+ // line background
gyorgy@0 8 '<div class="mejs-volume-current"></div>'+ // current volume
gyorgy@0 9 '<div class="mejs-volume-handle"></div>'+ // handle
gyorgy@0 10 '</div>'+
gyorgy@0 11 '</div>')
gyorgy@0 12 .appendTo(controls),
gyorgy@0 13 volumeSlider = mute.find('.mejs-volume-slider'),
gyorgy@0 14 volumeTotal = mute.find('.mejs-volume-total'),
gyorgy@0 15 volumeCurrent = mute.find('.mejs-volume-current'),
gyorgy@0 16 volumeHandle = mute.find('.mejs-volume-handle'),
gyorgy@0 17
gyorgy@0 18 positionVolumeHandle = function(volume) {
gyorgy@0 19
gyorgy@0 20 var
gyorgy@0 21 top = volumeTotal.height() - (volumeTotal.height() * volume);
gyorgy@0 22
gyorgy@0 23 // handle
gyorgy@0 24 volumeHandle.css('top', top - (volumeHandle.height() / 2));
gyorgy@0 25
gyorgy@0 26 // show the current visibility
gyorgy@0 27 volumeCurrent.height(volumeTotal.height() - top + parseInt(volumeTotal.css('top').replace(/px/,''),10));
gyorgy@0 28 volumeCurrent.css('top', top);
gyorgy@0 29 },
gyorgy@0 30 handleVolumeMove = function(e) {
gyorgy@0 31 var
gyorgy@0 32 railHeight = volumeTotal.height(),
gyorgy@0 33 totalOffset = volumeTotal.offset(),
gyorgy@0 34 totalTop = parseInt(volumeTotal.css('top').replace(/px/,''),10),
gyorgy@0 35 newY = e.pageY - totalOffset.top,
gyorgy@0 36 volume = (railHeight - newY) / railHeight
gyorgy@0 37
gyorgy@0 38 // TODO: handle vertical and horizontal CSS
gyorgy@0 39 // only allow it to move within the rail
gyorgy@0 40 if (newY < 0)
gyorgy@0 41 newY = 0;
gyorgy@0 42 else if (newY > railHeight)
gyorgy@0 43 newY = railHeight;
gyorgy@0 44
gyorgy@0 45 // move the handle to match the mouse
gyorgy@0 46 volumeHandle.css('top', newY - (volumeHandle.height() / 2) + totalTop );
gyorgy@0 47
gyorgy@0 48 // show the current visibility
gyorgy@0 49 volumeCurrent.height(railHeight-newY);
gyorgy@0 50 volumeCurrent.css('top',newY+totalTop);
gyorgy@0 51
gyorgy@0 52 // set mute status
gyorgy@0 53 if (volume == 0) {
gyorgy@0 54 media.setMuted(true);
gyorgy@0 55 mute.removeClass('mejs-mute').addClass('mejs-unmute');
gyorgy@0 56 } else {
gyorgy@0 57 media.setMuted(false);
gyorgy@0 58 mute.removeClass('mejs-unmute').addClass('mejs-mute');
gyorgy@0 59 }
gyorgy@0 60
gyorgy@0 61 volume = Math.max(0,volume);
gyorgy@0 62 volume = Math.min(volume,1);
gyorgy@0 63
gyorgy@0 64 // set the volume
gyorgy@0 65 media.setVolume(volume);
gyorgy@0 66 },
gyorgy@0 67 mouseIsDown = false;
gyorgy@0 68
gyorgy@0 69 // SLIDER
gyorgy@0 70 mute
gyorgy@0 71 .hover(function() {
gyorgy@0 72 volumeSlider.show();
gyorgy@0 73 }, function() {
gyorgy@0 74 volumeSlider.hide();
gyorgy@0 75 })
gyorgy@0 76
gyorgy@0 77 volumeSlider
gyorgy@0 78 .bind('mousedown', function (e) {
gyorgy@0 79 handleVolumeMove(e);
gyorgy@0 80 mouseIsDown = true;
gyorgy@0 81 return false;
gyorgy@0 82 });
gyorgy@0 83 $(document)
gyorgy@0 84 .bind('mouseup', function (e) {
gyorgy@0 85 mouseIsDown = false;
gyorgy@0 86 })
gyorgy@0 87 .bind('mousemove', function (e) {
gyorgy@0 88 if (mouseIsDown) {
gyorgy@0 89 handleVolumeMove(e);
gyorgy@0 90 }
gyorgy@0 91 });
gyorgy@0 92
gyorgy@0 93
gyorgy@0 94 // MUTE button
gyorgy@0 95 mute.find('button').click(function() {
gyorgy@0 96 if (media.muted) {
gyorgy@0 97 media.setMuted(false);
gyorgy@0 98 mute.removeClass('mejs-unmute').addClass('mejs-mute');
gyorgy@0 99 positionVolumeHandle(1);
gyorgy@0 100 } else {
gyorgy@0 101 media.setMuted(true);
gyorgy@0 102 mute.removeClass('mejs-mute').addClass('mejs-unmute');
gyorgy@0 103 positionVolumeHandle(0);
gyorgy@0 104 }
gyorgy@0 105 });
gyorgy@0 106
gyorgy@0 107 // listen for volume change events from other sources
gyorgy@0 108 media.addEventListener('volumechange', function(e) {
gyorgy@0 109 if (!mouseIsDown) {
gyorgy@0 110 positionVolumeHandle(e.target.volume);
gyorgy@0 111 }
gyorgy@0 112 }, true);
gyorgy@0 113
gyorgy@0 114 // set initial volume
gyorgy@0 115 positionVolumeHandle(player.options.startVolume);
gyorgy@0 116
gyorgy@0 117 // shim gets the startvolume as a parameter, but we have to set it on the native <video> and <audio> elements
gyorgy@0 118 if (media.pluginType === 'native') {
gyorgy@0 119 media.setVolume(player.options.startVolume);
gyorgy@0 120 }
gyorgy@0 121 }
gyorgy@0 122
gyorgy@0 123 })(mejs.$);