-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayControl.js
More file actions
242 lines (183 loc) · 7.07 KB
/
PlayControl.js
File metadata and controls
242 lines (183 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
if(!javaxt) var javaxt={};
if(!javaxt.dhtml) javaxt.dhtml={};
//******************************************************************************
//** PlayControl
//******************************************************************************
/**
* Used to render a slider that can be used a part of a audio or video
* controller. This class builds upon the javaxt.dhtml.Slider class by
* providing basic functions like play, pause, and stop. However, this class
* does not provide any buttons or any other components to perform these
* operations - this is just a fancy slider :-) Users can interact with the
* slider while its running and get back elapsed time.
*
******************************************************************************/
javaxt.dhtml.PlayControl = function(parent, config) {
this.className = "javaxt.dhtml.PlayControl";
var me = this;
var slider;
var playing = false;
var timerId;
var loop = false;
var defaultConfig = {
/** Used to specify the start time, in seconds. Default is 0.
*/
startTime: 0,
/** Refresh rate, in seconds. Default is 0.2.
*/
speed: 0.2,
/** Used to specify the total run time, in seconds. Default is 60.
*/
totalTime: 60,
/** Style for individual elements within the component. Uses the style
* defined in the javaxt.dhtml.Slider class by default.
*/
style: {
groove: {},
handle: {}
}
};
//**************************************************************************
//** Constructor
//**************************************************************************
var init = function(){
//Clone the config so we don't modify the original config object
var clone = {};
merge(clone, config);
//Merge clone with default config
merge(clone, defaultConfig);
config = clone;
var fireEvents = false;
slider = new javaxt.dhtml.Slider(parent, config);
slider.onChange = function(){
if (fireEvents){
me.onChange(me.getElapsedTime());
}
};
slider.onDrag = function(){
var resume = playing;
if (playing) me.pause();
if (resume) me.play(loop);
};
slider.onRender = function(){
me.setElapsedTime(config.startTime);
fireEvents = true;
};
};
//**************************************************************************
//** setRunTime
//**************************************************************************
/** Used to update the total run time. This value is originally set in the
* config settings (see totalTime).
* @param runTime Total run time, in seconds
*/
this.setRunTime = function(runTime){
if (runTime===config.totalTime) return;
var resume = false;
if (playing){
me.pause();
resume = true;
}
config.totalTime = runTime;
if (resume) me.play();
};
//**************************************************************************
//** play
//**************************************************************************
/** Used to start the player
*/
this.play = function(_loop){
if (playing===true) return;
if (_loop===true) loop = true;
else loop = false;
playing = true;
var startDate = new Date().getTime();
var currVal = slider.getPosition();
if (currVal>0){
startDate = startDate - ((currVal/slider.getWidth()) * config.totalTime)*1000;
}
timerId = setInterval(function(){
var w = slider.getWidth();
var elapsedTime = (new Date().getTime()-startDate)/1000;
var percentComplete = elapsedTime/config.totalTime;
var nextVal = w*percentComplete; //Math.ceil
if (nextVal>=w){
if (loop===true){
me.onEnd();
slider.setValue(0);
startDate = new Date().getTime();
}
else{
slider.setValue(w);
me.pause();
me.onEnd();
}
}
else{
slider.setValue(nextVal);
}
}, config.speed*1000);
};
//**************************************************************************
//** pause
//**************************************************************************
/** Used to pause the player.
*/
this.pause = function(){
clearInterval(timerId);
playing = false;
};
//**************************************************************************
//** stop
//**************************************************************************
/** Used to stop the player and set the start time to 0 seconds.
*/
this.stop = function(){
me.pause();
slider.setValue(0);
};
//**************************************************************************
//** isPlaying
//**************************************************************************
/** Returns true if the player is playing.
*/
this.isPlaying = function(){
return playing;
};
//**************************************************************************
//** getElapsedTime
//**************************************************************************
this.getElapsedTime = function(){
return (slider.getPosition()/slider.getWidth()) * config.totalTime;
};
//**************************************************************************
//** setElapsedTime
//**************************************************************************
/** Used to update the elapsed time time.
* @param elapsedTime Time in seconds
*/
this.setElapsedTime = function(elapsedTime){
if (elapsedTime==null || elapsedTime<0) return;
var w = slider.getWidth();
var x = Math.ceil((elapsedTime/config.totalTime)*w);
if (x>w) x = 0;
slider.setValue(x);
};
//**************************************************************************
//** onChange
//**************************************************************************
/** Called whenever the player is updated.
*/
this.onChange = function(elapsedTime){};
//**************************************************************************
//** onEnd
//**************************************************************************
/** Called whenever the elapsed time equals the total run time.
*/
this.onEnd = function(){};
//**************************************************************************
//** Utils
//**************************************************************************
var merge = javaxt.dhtml.utils.merge;
init();
};