Added source for flvplayer to svn
[Packages/TYPO3.CMS.git] / typo3 / contrib / flashmedia / src / flvplayer.as
1 //--------------------------------------------------------------------------
2 // initial variables that might be useful to change
3 //--------------------------------------------------------------------------
4
5 // Stage variables
6
7 Stage.scaleMode = "noScale";
8 Stage.align = "TL";
9
10 // Add link to rightclick menu
11
12 var newMenu:ContextMenu = new ContextMenu();
13 newMenu.hideBuiltInItems();
14 newMenu.customItems.push(new ContextMenuItem("TYPO3 Media Player...",goTo));
15 this.menu = newMenu;
16 function goTo() { getURL("http://typo3.org"); };
17
18 //_root.allowFullScreen = "true";
19 var screenMode:String = 'normal';
20
21 function fullScreen()
22 {
23         if(screenMode == 'normal')
24         {
25       Stage["displayState"] = "fullScreen";
26           screenMode = 'full';
27         }
28         else
29         {
30                 Stage["displayState"] = "normal";
31                 screenMode = 'normal';
32         }
33 }
34
35 var stageSize:Object = new Object();
36 stageSize.onResize = function() 
37 {
38     w = Stage.width;
39  h = Stage.height;
40  setDims(w,h);                                
41 }
42 Stage.addListener(stageSize);
43
44
45 if(_root.previewSeek == 'true')
46 {
47  makePre = true;
48 }
49 else
50 {
51  makePre = false;
52 }
53
54
55 // toggle for which file to play if none was set in html
56 // you can change the 'test.flv' in your filename
57 if(!_root.file) {
58         file = "video.flv";
59 } else {
60         file = _root.file;
61 }
62
63 // toggle for autostarting the video
64 // you can change the 'true' in 'false'
65 if(_root.autoStart == "true") {
66         autoStart = true;
67 } else {
68         autoStart = false;
69 }
70
71 if(!_root.smoothing) {
72         smoothing = true;
73 } else {
74         smoothing = _root.smoothing;
75 }
76 if(!_root.deblocking) {
77         deblocking = 5;
78 } else {
79         deblocking = _root.deblocking;
80 }
81 if(!_root.volume) {
82         volume = 80;
83 } else {
84         volume = _root.volume;
85 }
86 if(!_root.prebuffer) {
87         prebuffer = 5;
88 } else {
89         prebuffer = _root.prebuffer;
90 }
91 if(!_root.preview || _root.preview == "true") {
92         preview = true;
93 }
94 if(!_root.previewSeek) {
95         previewSeek = 0.1
96 } else {
97         previewSeek = _root.previewSeek
98 }
99
100 if (!_root.clickAlpha) {
101         clickAlpha = 65;
102 } else {
103         clickAlpha = _root.clickAlpha;
104 }
105
106 if (!_root.clickText) {
107         clickText = "";
108 } else {
109         clickText = _root.clickText;
110 }
111
112 // toggle for the width and height of the video
113 // you can change them to the width and height you want
114 w = Stage.width;
115 h = Stage.height;
116
117 //--------------------------------------------------------------------------
118 // stream setup and functions
119 //--------------------------------------------------------------------------
120
121 // create and set netstream
122 nc = new NetConnection();
123 nc.connect(null);
124 ns = new NetStream(nc);
125 ns.setBufferTime(2);
126
127 // create and set sound object
128 this.createEmptyMovieClip("snd", 0);
129 snd.attachAudio(ns);
130 audio = new Sound(snd);
131
132 //attach videodisplay
133 videoDisplay.attachVideo(ns);
134
135 // Retrieve duration meta data from netstream
136 ns.onMetaData = function(obj) {
137         this.totalTime = obj.duration;
138         // these three lines were used for automatically sizing
139         // it is now done by sizing the video to stage dimensions
140         // if(obj.height > 0 && obj.height < Stage.height-20) {
141         // setDims(obj.width, obj.height);
142         // }
143 };
144
145 // retrieve status messages from netstream
146 ns.onStatus = function(object) { 
147         if(object.code == "NetStream.Play.Stop") {
148                 // rewind and pause on when movie is finished
149                 ns.seek(0);
150                 if(_root.repeat == "true") {
151                         return;
152                 }
153                 if(preview) {
154                         ns.seek(previewSeek);
155                 }
156                 ns.pause();
157                 playBut._visible = true;
158                 pauseBut._visible = false;
159                 if (!preview) {
160                         videoDisplay._visible = false;
161                 }
162                 showClick(true);
163         }
164         if (info.code == "NetStream.Buffer.Full") {
165         if(makePre) {
166                         ns.seek(previewSeek);
167                         makePre = false;
168     }
169    }
170 };
171
172
173 //--------------------------------------------------------------------------
174 // controlbar functionality
175 //--------------------------------------------------------------------------
176
177 function showClick(show) {
178         if (show) {
179                 if (_root.click) {
180                         if (!alignedClick) {
181                                 clickImage.loadMovie(_root.click);
182                                 clickImage._alpha = clickAlpha;
183                         }
184                         clickImage._visible = true;
185                 } else {
186                         playText.text = clickText;
187                 }
188         } else {
189                 if (playText.text.length) {
190                         playText.text = "";
191                 }
192                 if (clickImage._visible) {
193                         clickImage._visible = false;
194                 }
195         }
196 }
197
198 // play the movie and hide playbutton
199 function playMovie() {
200         if(!isStarted) {
201                 audio.setVolume(volume);
202                 ns.setBufferTime(prebuffer);
203                 ns.play(file);
204                 isStarted = true;
205         } else {
206                 showClick(false);
207                 ns.pause();
208         }
209         pauseBut._visible = true;
210         playBut._visible = false;
211         videoDisplay._visible = true;
212 }
213
214 // pause the movie and hide pausebutton
215 function pauseMovie() {
216         ns.pause();
217         playBut._visible = true;
218         pauseBut._visible = false;
219 };
220
221 // video click action
222 videoBg.onPress = function() {
223         if(pauseBut._visible == false) {
224                 playMovie();
225         } else {
226                 pauseMovie();
227         }
228 };
229
230 // pause button action
231 pauseBut.onPress = function() {
232         pauseMovie();
233 };
234
235 // play button action
236 playBut.onPress = function() {
237         playMovie();
238 };
239
240 // file load progress
241 progressBar.onEnterFrame = function() {
242         if (isStarted) {
243                 loaded = this._parent.ns.bytesLoaded;
244                 total = this._parent.ns.bytesTotal;
245                 if (loaded == total && loaded > 1000) {
246                         this.loa._xscale = 100;
247                         delete this.onEnterFrame;
248                 } else {
249                         this.loa._xscale = int(loaded/total*100);
250                 }
251         }
252 };
253
254 // play progress function
255 progressBar.tme.onEnterFrame = function() {
256         if (isStarted) {
257                 this._xscale = ns.time/ns.totalTime*100;
258
259                 if (bufferPercent != -1) {
260                         if (!bufferPercent) {
261                                 showClick(false);
262                         }
263                         bufferPercent = int(ns.bufferLength/ns.bufferTime*100);
264                         if (bufferPercent >= 100) {
265                                 playText.text = "";
266                                 bufferPercent = -1;
267                         } else {
268                                 playText.text = "buffering .. " + bufferPercent + "%";
269                         }
270                 }
271         } else if (inPreview && ns.time > 0) {
272                 ns.close();
273                 inPreview = false;
274         }
275
276         if (clickImage._width && !alignedClick) {
277                 clickImage._x = (videoDisplay._width - clickImage._width) / 2;
278                 clickImage._y = (videoDisplay._height - clickImage._height) / 2;
279                 alignedClick = true;
280         }
281 };
282
283 // start playhead scrubbing
284 progressBar.loa.onPress = function() {
285         this.onEnterFrame = function() {
286                 scl = (this._xmouse/this._width)*(this._xscale/100)*(this._xscale/100);
287                 if(scl < 0.02) { scl = 0; }
288                 ns.seek(scl*ns.totalTime);
289                 if (isStarted) {
290                         showClick(false);
291                 }
292         };
293 };
294
295 // stop playhead scrubbing
296 progressBar.loa.onRelease = progressBar.loa.onReleaseOutside = function () { 
297         delete this.onEnterFrame;
298         pauseBut._visible == false ? videoDisplay.pause() : null;
299 };
300
301
302 // fullscreen
303 if(_root.allowFullScreen == "true") {
304         FSBut.onPress = function() {
305                 fullScreen();
306         };
307 } else if (_root.fs == "true") {
308         FSBut.onPress = function() {
309                 getURL("javascript: history.go(-1)");
310         };
311 }
312
313
314
315 // volume scrubbing
316 volumeBar.back.onPress = function() {
317         this.onEnterFrame = function() {
318                 var xm = this._xmouse;
319                 if(xm>=0 && xm <= 20) {
320                         this._parent.mask._width = this._xmouse;
321                         this._parent._parent.audio.setVolume(this._xmouse*5);
322                 }
323         };
324 }
325 volumeBar.back.onRelease = volumeBar.back.onReleaseOutside = function() {
326         delete this.onEnterFrame;
327 }
328
329 volumeBar.icn.onPress = function() {
330         if (volumeBar.mute._visible) {
331                 setVolume(volume);
332         } else {
333                 volume = audio.getVolume();
334                 setVolume(0);
335         }
336 }
337
338 function setVolume(vol) {
339         audio.setVolume(vol);
340         volumeBar.mask._width = vol/5;
341         if (vol > 0 && volumeBar.mute._visible) {
342                 volumeBar.mute._visible = false;
343         } else if (!vol && !volumeBar.mute._visible) {
344                 volumeBar.mute._visible = true;
345         }
346 }
347
348 setVolume(volume);
349
350
351 //--------------------------------------------------------------------------
352 // resize and position all items
353 //--------------------------------------------------------------------------
354 function setDims(w,h) {
355         // set videodisplay dimensions
356         videoDisplay._width = videoBg._width = w;
357         videoDisplay._height = videoBg._height = h-20;
358         playText1._x = w/2-120;
359         playText1._y = h/2-20;
360         playText2._x = playText1._x + 1;
361         playText2._y = playText1._y + 1;
362                 
363         // resize the controlbar items .. 
364         if(_root.fs == "true") {
365                 colorBar._y = playBut._y = pauseBut._y = progressBar._y = FSBut._y = volumeBar._y = h-30; 
366                 playBut._x = pauseBut._x = colorBar._x = w/2-150;
367                 colorBar._width = 300;
368                 colorBar._alpha = 25;
369                 progressBar._x = w/2-133;
370                 progressBar._width = 228;
371                 FSBut._x = w/2+95;
372                 volumeBar._x = w/2+112;
373                 videoDisplay._height = h;
374         } else {
375                 colorBar._y = playBut._y = pauseBut._y = progressBar._y = FSBut._y = volumeBar._y = h-18;
376                 progressBar._width = w-56;
377                 colorBar._width = w;
378                 volumeBar._x = w-38;
379                 if(_root.allowFullScreen == "true") { 
380                         FSBut._visible = true;
381                         progressBar._width -=17;
382                         FSBut._x = w-55;
383                 } else {
384                         FSBut._visible = false;
385                 }
386         }
387 }
388
389 // here you can ovverride the dimensions of the video
390 setDims(w,h);
391
392
393 //--------------------------------------------------------------------------
394 // all done ? start the movie !
395 //--------------------------------------------------------------------------
396
397 // start playing the movie
398 // if no autoStart it searches for a placeholder jpg
399 // and hides the pauseBut
400
401 pauseBut._visible = false;
402 videoDisplay.smoothing = smoothing;
403 videoDisplay.deblocking = deblocking;
404 if (autoStart == true) {
405         playMovie();
406 } else {
407         showClick(true);
408         if (preview) {
409                 inPreview = true;
410                 audio.setVolume(0);
411                 ns.play(file);
412                 ns.seek(previewSeek);
413         } else {
414                 if(_root.image) { 
415                         imageStr = _root.image;
416                 } else {
417                         imageStr = file.substring(0,file.length-3)+"jpg";
418                         if (imageStr.substring(0,2) == "..") {
419                                 imageStr = imageStr.substring(3);
420                         }
421                 }
422                 imageClip.loadMovie(imageStr);
423         }
424 }