~ubuntu-branches/debian/sid/wordpress/sid

« back to all changes in this revision

Viewing changes to debian/missing-sources/mediaelement/src/htmlelements/AudioElement.as

  • Committer: Package Import Robot
  • Author(s): Craig Small
  • Date: 2014-04-16 22:48:26 UTC
  • mfrom: (1.2.34)
  • Revision ID: package-import@ubuntu.com-20140416224826-087tu71aw8bjhvmd
Tags: 3.8.3+dfsg-1
New upstream release - fixes Quick Draft tool that broke in 3.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1

 
2
package htmlelements 
 
3
{
 
4
        import flash.events.Event;
 
5
        import flash.events.IOErrorEvent;
 
6
        import flash.events.ProgressEvent;
 
7
        import flash.events.TimerEvent;
 
8
        import flash.media.ID3Info;
 
9
        import flash.media.Sound;
 
10
        import flash.media.SoundChannel;
 
11
        import flash.media.SoundLoaderContext;
 
12
        import flash.media.SoundTransform;
 
13
        import flash.net.URLRequest;
 
14
        import flash.utils.Timer;
 
15
 
 
16
 
 
17
 
 
18
        /**
 
19
        * ...
 
20
        * @author DefaultUser (Tools -> Custom Arguments...)
 
21
        */
 
22
        public class AudioElement implements IMediaElement
 
23
        {
 
24
 
 
25
                private var _sound:Sound;
 
26
                private var _soundTransform:SoundTransform;
 
27
                private var _soundChannel:SoundChannel;
 
28
                private var _soundLoaderContext:SoundLoaderContext;
 
29
 
 
30
                private var _volume:Number = 1;
 
31
                private var _preMuteVolume:Number = 0;
 
32
                private var _isMuted:Boolean = false;
 
33
                private var _isPaused:Boolean = true;
 
34
                private var _isEnded:Boolean = false;
 
35
                private var _isLoaded:Boolean = false;
 
36
                private var _currentTime:Number = 0;
 
37
                private var _duration:Number = 0;
 
38
                private var _bytesLoaded:Number = 0;
 
39
                private var _bytesTotal:Number = 0;
 
40
                private var _bufferedTime:Number = 0;
 
41
                private var _bufferingChanged:Boolean = false;
 
42
 
 
43
                private var _currentUrl:String = "";
 
44
                private var _autoplay:Boolean = true;
 
45
                private var _preload:String = "";
 
46
 
 
47
                private var _element:FlashMediaElement;
 
48
                private var _timer:Timer;
 
49
                private var _firedCanPlay:Boolean = false;
 
50
 
 
51
                public function setSize(width:Number, height:Number):void {
 
52
                        // do nothing!
 
53
                }
 
54
 
 
55
                public function duration():Number {
 
56
                        return _duration;
 
57
                }
 
58
 
 
59
                public function currentTime():Number {
 
60
                        return _currentTime;
 
61
                }
 
62
                
 
63
                public function currentProgress():Number {
 
64
                                return Math.round(_bytesLoaded/_bytesTotal*100);
 
65
                }
 
66
 
 
67
                public function AudioElement(element:FlashMediaElement, autoplay:Boolean, preload:String, timerRate:Number, startVolume:Number) 
 
68
                {
 
69
                        _element = element;
 
70
                        _autoplay = autoplay;
 
71
                        _volume = startVolume;
 
72
                        _preload = preload;
 
73
 
 
74
                        _timer = new Timer(timerRate);
 
75
                        _timer.addEventListener(TimerEvent.TIMER, timerEventHandler);
 
76
 
 
77
                        _soundTransform = new SoundTransform(_volume);
 
78
                        _soundLoaderContext = new SoundLoaderContext();
 
79
                }
 
80
 
 
81
                // events
 
82
                function progressHandler(e:ProgressEvent):void {
 
83
 
 
84
                        _bytesLoaded = e.bytesLoaded;
 
85
                        _bytesTotal = e.bytesTotal;
 
86
 
 
87
                        // this happens too much to send every time
 
88
                        //sendEvent(HtmlMediaEvent.PROGRESS);
 
89
                        
 
90
                        // so now we just trigger a flag and send with the timer
 
91
                        _bufferingChanged = true;
 
92
                }
 
93
 
 
94
                function id3Handler(e:Event):void {
 
95
                        sendEvent(HtmlMediaEvent.LOADEDMETADATA);                       
 
96
                        
 
97
                        try {
 
98
                                var id3:ID3Info = _sound.id3;
 
99
                                var obj = {
 
100
                                        type:'id3',
 
101
                                        album:id3.album,
 
102
                                        artist:id3.artist,
 
103
                                        comment:id3.comment,
 
104
                                        genre:id3.genre,
 
105
                                        songName:id3.songName,
 
106
                                        track:id3.track,
 
107
                                        year:id3.year
 
108
                                }
 
109
                        } catch (err:Error) {}
 
110
                        
 
111
                        
 
112
                }
 
113
 
 
114
                function timerEventHandler(e:TimerEvent) {
 
115
                        _currentTime = _soundChannel.position/1000;
 
116
 
 
117
                        // calculate duration
 
118
                        var duration = Math.round(_sound.length * _sound.bytesTotal/_sound.bytesLoaded/100) / 10;
 
119
 
 
120
                        // check to see if the estimated duration changed
 
121
                        if (_duration != duration && !isNaN(duration)) {
 
122
 
 
123
                                _duration = duration;
 
124
                                sendEvent(HtmlMediaEvent.LOADEDMETADATA);
 
125
                        }
 
126
                        
 
127
                        // check for progress
 
128
                        if (_bufferingChanged) {
 
129
                                
 
130
                                sendEvent(HtmlMediaEvent.PROGRESS);
 
131
                        
 
132
                                _bufferingChanged = false;
 
133
                        }
 
134
 
 
135
                        // send timeupdate
 
136
                        sendEvent(HtmlMediaEvent.TIMEUPDATE);
 
137
 
 
138
                        // sometimes the ended event doesn't fire, here's a fake one
 
139
                        if (_duration > 0 && _currentTime >= _duration-0.2) {
 
140
                                handleEnded();
 
141
                        }
 
142
                }
 
143
 
 
144
                function soundCompleteHandler(e:Event) {
 
145
                        handleEnded();
 
146
                }
 
147
 
 
148
                function handleEnded():void {
 
149
                        _timer.stop();
 
150
                        _currentTime = 0;
 
151
                        _isEnded = true;
 
152
 
 
153
                        sendEvent(HtmlMediaEvent.ENDED);
 
154
                }
 
155
 
 
156
                //events
 
157
 
 
158
 
 
159
                // METHODS
 
160
                public function setSrc(url:String):void {
 
161
                        _currentUrl = url;
 
162
                        _isLoaded = false;
 
163
                }
 
164
 
 
165
 
 
166
                public function load():void {
 
167
 
 
168
                        if (_currentUrl == "")
 
169
                                return;
 
170
 
 
171
                        _sound = new Sound();
 
172
                        //sound.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
 
173
                        _sound.addEventListener(ProgressEvent.PROGRESS,progressHandler);
 
174
                        _sound.addEventListener(Event.ID3,id3Handler);
 
175
                        _sound.load(new URLRequest(_currentUrl));
 
176
                        _currentTime = 0;
 
177
                        
 
178
                        sendEvent(HtmlMediaEvent.LOADSTART);
 
179
 
 
180
                        _isLoaded = true;
 
181
                        
 
182
                        sendEvent(HtmlMediaEvent.LOADEDDATA);
 
183
                        sendEvent(HtmlMediaEvent.CANPLAY);                              
 
184
                        _firedCanPlay = true;
 
185
                        
 
186
                        if (_playAfterLoading) {
 
187
                                _playAfterLoading = false;
 
188
                                play();
 
189
                        }                                               
 
190
                }
 
191
 
 
192
                private var _playAfterLoading:Boolean= false;
 
193
 
 
194
                public function play():void {
 
195
 
 
196
                        if (!_isLoaded) {
 
197
                                _playAfterLoading = true;
 
198
                                load();
 
199
                                return;
 
200
                        }
 
201
 
 
202
                        _timer.stop();
 
203
 
 
204
                        _soundChannel = _sound.play(_currentTime*1000, 0, _soundTransform);
 
205
                        _soundChannel.removeEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
 
206
                        _soundChannel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
 
207
 
 
208
                        _timer.start();
 
209
                        
 
210
                        didStartPlaying();
 
211
                }
 
212
 
 
213
                public function pause():void {
 
214
 
 
215
                        _timer.stop();
 
216
                        if (_soundChannel != null) {
 
217
                                _currentTime = _soundChannel.position/1000;
 
218
                                _soundChannel.stop();                   
 
219
                        }
 
220
 
 
221
                        _isPaused = true;
 
222
                        sendEvent(HtmlMediaEvent.PAUSE);
 
223
                }
 
224
 
 
225
 
 
226
                public function stop():void {
 
227
                        if (_timer != null) {
 
228
                                _timer.stop();
 
229
                        }
 
230
                        if (_soundChannel != null) {
 
231
                                _soundChannel.stop();
 
232
                                _sound.close();
 
233
                        }
 
234
                        unload();
 
235
                        sendEvent(HtmlMediaEvent.STOP);
 
236
                }
 
237
 
 
238
                public function setCurrentTime(pos:Number):void {
 
239
                        sendEvent(HtmlMediaEvent.SEEKING);
 
240
                        _timer.stop();
 
241
                        _currentTime = pos;
 
242
                        _soundChannel.stop();
 
243
                        _sound.length
 
244
                        _soundChannel = _sound.play(_currentTime * 1000, 0, _soundTransform);
 
245
                        sendEvent(HtmlMediaEvent.SEEKED);
 
246
 
 
247
                        _timer.start();
 
248
                        
 
249
                        didStartPlaying();
 
250
                }
 
251
                
 
252
                private function didStartPlaying():void {
 
253
                        _isPaused = false;
 
254
                        sendEvent(HtmlMediaEvent.PLAY);
 
255
                        sendEvent(HtmlMediaEvent.PLAYING);
 
256
                        if (!_firedCanPlay) {
 
257
                                sendEvent(HtmlMediaEvent.LOADEDDATA);
 
258
                                sendEvent(HtmlMediaEvent.CANPLAY);                              
 
259
                                _firedCanPlay = true;
 
260
                        }
 
261
                }
 
262
 
 
263
 
 
264
                public function setVolume(volume:Number):void {
 
265
 
 
266
                        _volume = volume;
 
267
                        _soundTransform.volume = volume;
 
268
 
 
269
                        if (_soundChannel != null) {
 
270
                                _soundChannel.soundTransform = _soundTransform;
 
271
                        }
 
272
 
 
273
                        _isMuted = (_volume == 0);
 
274
 
 
275
                        sendEvent(HtmlMediaEvent.VOLUMECHANGE);
 
276
                }
 
277
                
 
278
                public function getVolume():Number {
 
279
                        if(_isMuted) {
 
280
                                return 0;
 
281
                        } else {
 
282
                                return _volume;
 
283
                        }
 
284
                }
 
285
 
 
286
 
 
287
                public function setMuted(muted:Boolean):void {
 
288
 
 
289
                        // ignore if already set
 
290
                        if ( (muted && _isMuted) || (!muted && !_isMuted))
 
291
                                return;
 
292
 
 
293
                        if (muted) {
 
294
                                _preMuteVolume = _soundTransform.volume;
 
295
                                setVolume(0);
 
296
                        } else {
 
297
                                setVolume(_preMuteVolume);
 
298
                        }
 
299
 
 
300
                        _isMuted = muted;
 
301
                }
 
302
 
 
303
                public function unload():void {
 
304
                        _sound = null;
 
305
                        _isLoaded = false;
 
306
                }
 
307
 
 
308
                private function sendEvent(eventName:String) {
 
309
 
 
310
                        // calculate this to mimic HTML5
 
311
                        _bufferedTime = _bytesLoaded / _bytesTotal * _duration;
 
312
 
 
313
                        // build JSON
 
314
                        var values:String = "duration:" + _duration + 
 
315
                                                        ",currentTime:" + _currentTime + 
 
316
                                                        ",muted:" + _isMuted + 
 
317
                                                        ",paused:" + _isPaused + 
 
318
                                                        ",ended:" + _isEnded + 
 
319
                                                        ",volume:" + _volume +
 
320
                                                        ",src:\"" + _currentUrl + "\"" +
 
321
                                                        ",bytesTotal:" + _bytesTotal +
 
322
                                                        ",bufferedBytes:" + _bytesLoaded +
 
323
                                                        ",bufferedTime:" + _bufferedTime +
 
324
                                                        "";
 
325
 
 
326
                        _element.sendEvent(eventName, values);
 
327
                }
 
328
 
 
329
        }
 
330
 
 
331
}
 
332