comparison johndyer-mediaelement-13fa20a/src/js/mep-feature-volume.js @ 0:032bc65ebafc

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