-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.as
406 lines (306 loc) · 8.55 KB
/
Player.as
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package
{
import flash.display.MovieClip;
import slider.Slider;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import button.BiButton;
import label.BiHint;
import label.Label;
import soundManager.SoundManager;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.TimerEvent;
import flash.events.IOErrorEvent;
import flash.display.Bitmap;
import utils.*;
import flash.display.LoaderInfo;
import exchanger.Exchanger;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import frame.Frame;
public class Player extends MovieClip
{
static private var _isDebugMode = true;
private var _positionSlider: Slider;
private var _volumeSlider: Slider;
private var _soundManager: SoundManager;
private var _sliderLineHeight = 5;
private var _titleLabel: Label;
private var _playProgressLabel: Label;
private var _volumeLabel: Label;
private var _URL: String;
private var _exchanger: Exchanger;
private static var STR_TRUE: String = "true";
private static var STR_FALSE: String = "false";
private var _frame: Frame;
static private var _exchangerParamsNames =
{
URL: "url",
title: "title",
disabled: "disabled"
}
private var _coords =
{
changeAllY: function( dY: int, context: Object = null )
{
var context_ = this;
if( context != null )
{
context_ = context;
}
if( typeof context_ == "object" )
{
if( context_[ 'y' ] != null )
{
context_[ 'y' ] += dY;
}
else
for( var iterObj in context_ )
{
this.changeAllY( dY, context_[ iterObj ] );
}
}
},
updateByWidth: function( width: int )
{
this.sliders.position.width = width - 88;
this.sliders.volume.x = width - 113;
this.labels.volume.x = width - 186;
},
sliders:
{
position:
{
x: 75,
y: 48
},
volume:
{
width: 100,
y: 70
}
},
playPauseHint:
{
x: 75,
y: 60
},
playPauseButton:
{
x: 10,
y: 23
},
labels:
{
title:
{
x: 75,
y: 20
},
playProgress:
{
x: 75,
y: 65
},
volume:
{
y: 65
},
disable:
{
x: 20,
y: 40
}
}
};
static private var _colors =
{
frame:
{
color: 0x3A90D3,
bkColor: 0xFFFFFF
}
};
static private var _sizes =
{
frame:
{
ellipseWidthHeight: 8
}
};
static private var _fontSizes =
{
playProgress: 12,
volume: 10,
title: 12,
disable: 18
}
private static var _texts =
{
play: "Play",
pause: "Pause",
getLoading: function( percents: Number )
{
return "Loaded: " + String( percents ) + "%";
},
volume: "Volume",
disable: "This record is available in only payed mode."
}
private var _playPauseButton: BiButton;
private static var _defaultVolume = 0.5;
private var _scaleMode = StageScaleMode.NO_SCALE;
private var _align = StageAlign.TOP_LEFT;
public function Player() {
// here is a main constructor of flash movie
// class of scroll
stage.scaleMode = _scaleMode;
stage.align = _align;
_coords.updateByWidth( stage.stageWidth );
_exchanger = new Exchanger( this.root.loaderInfo );
var titleLabelText: String;
var URL: String;
var disabled: String;
if( _isDebugMode )
{
titleLabelText = "Debug mode";
URL = "http://mp3.classic-music.ru/music/chopin/ballada2.mp3";
disabled = "false";
}
else
{
titleLabelText = _exchanger.getParameterByName( _exchangerParamsNames.title );
URL = _exchanger.getParameterByName( _exchangerParamsNames.URL );
disabled = _exchanger.getParameterByName( _exchangerParamsNames.disabled );
}
addChild( new Frame( stage.stageWidth, stage.stageHeight, _sizes.frame.ellipseWidthHeight, _colors.frame.color, _colors.frame.bkColor ) );
_coords.changeAllY( -7 );
//addChild( new Bitmap( new FrameBitmap( 0, 0 ) ) );
if( disabled == STR_TRUE )
{
_disable();
return;
}
_positionSlider = new Slider( _coords.sliders.position.x,
_coords.sliders.position.y,
_coords.sliders.position.width,
_sliderLineHeight,
_positionSliderUpdate,
false,
false,
false );
addChild( _positionSlider );
_volumeSlider = new Slider( _coords.sliders.volume.x,
_coords.sliders.volume.y,
_coords.sliders.volume.width,
_sliderLineHeight,
_volumeSliderUpdate,
true,
true,
true );
_volumeSlider.updateSliderPosition( _defaultVolume );
addChild( _volumeSlider );
stage.addEventListener( MouseEvent.MOUSE_MOVE, _mouseMove );
stage.addEventListener( MouseEvent.MOUSE_UP, _mouseUp );
var biHint: BiHint = new BiHint( _texts.play, _texts.pause );
biHint.x = _coords.playPauseHint.x;
biHint.y = _coords.playPauseHint.y;
_playPauseButton = new BiButton( new PlayInactiveBitmap( 0, 0 ),
new PlayActiveBitmap( 0, 0 ),
new PauseInactiveBitmap( 0, 0 ),
new PauseActiveBitmap( 0, 0 ),
onPlay,
onPause,
biHint );
_playPauseButton.x = _coords.playPauseButton.x;
_playPauseButton.y = _coords.playPauseButton.y;
addChild( _playPauseButton );
_titleLabel = new Label( _coords.labels.title.x, _coords.labels.title.y, _fontSizes.title );
addChild( _titleLabel );
_playProgressLabel = new Label( _coords.labels.playProgress.x, _coords.labels.playProgress.y, _fontSizes.playProgress );
addChild( _playProgressLabel );
_volumeLabel = new Label( _coords.labels.volume.x, _coords.labels.volume.y, _fontSizes.volume, _texts.volume );
addChild( _volumeLabel );
useHandCursor = true;
_soundManager = new SoundManager( _onLoadProgress, _onPlayProgress, _onComplete, _onIOError, _defaultVolume );
addChild( biHint );
_titleLabel.setText( titleLabelText );
_setURL( URL );
}
private function _disable()
{
var disableLabel = new Label( _coords.labels.disable.x, _coords.labels.disable.y, _fontSizes.disable, _texts.disable );
addChild( disableLabel );
}
private function _setTitle( text: String )
{
_titleLabel.setText( text );
}
private function _setURL( URL: String )
{
this._URL = URL;
}
private function _onLoadProgress( position: Number )
{
_positionSlider.updateLinePosition( position );
if( _soundManager.isWaitingProgress() )
_playProgressLabel.setText( _texts.getLoading( int( position * 100 ) ) );
}
private function _updateSliderLabelsPosition( position: Number, time: Number, totalTime: Number )
{
_positionSlider.updateSliderPosition( position );
var curSecs = Math.floor( time / 1000 );
var totalSecs = Math.floor( totalTime / 1000 );
if( !_soundManager.isWaitingProgress() )
_playProgressLabel.setText( TimeLabel.get( curSecs, totalSecs ) );
}
private function _onPlayProgress( position: Number )
{
// trace( "_onPlayProgress" );
_updateSliderLabelsPosition( position, _soundManager.getTime(), _soundManager.getFullTime() );
}
private function _onComplete( e: Event )
{
trace( "onComplete" );
_soundManager.setPosition( 0 );
_soundManager.stop();
_updateSliderLabelsPosition( 0, _soundManager.getTime(), _soundManager.getFullTime() );
_playPauseButton.setState( 0 );
}
private function _onIOError( e: Event )
{
// trace( "onIOError" );
}
private function _mouseMove( e: MouseEvent )
{
_positionSlider.mouseMove( e );
_volumeSlider.mouseMove( e );
}
private function _mouseUp( e: MouseEvent )
{
_positionSlider.mouseUp ( e );
_volumeSlider.mouseUp( e );
}
private function _positionSliderUpdate( position: Number )
{
_soundManager.setPosition( position );
_updateSliderLabelsPosition( position, _soundManager.getTime(), _soundManager.getFullTime() );
}
private function _volumeSliderUpdate( position: Number )
{
_soundManager.setVolume( position );
}
public function onPlay()
{
if( !_soundManager.isLoadingStarted() )
{
_soundManager.load( this._URL );
}
_soundManager.play();
}
public function onPause()
{
_soundManager.pause();
}
}
}