root/trunk/sl1/wmvplayer.js @ 42

Revision 42, 23.3 kB (checked in by jeroen, 16 months ago)

tagged the 1.1 wmvplayer and started the air player

Line 
1/****************************************************************************
2* JW WMV Player version 1.1, created with M$ Silverlight 1.0
3*
4* This file contains all logic for the JW WMV Player. For a functional setup,
5* the following two files are also needed:
6* - silverlight.js (for instantiating the silverlight plugin)
7* - wmvplayer.xaml (or another XAML skin describing the player graphics)
8*
9* More info: http://www.jeroenwijering.com/?item=JW_WMV_Player
10****************************************************************************/
11if(typeof jeroenwijering == "undefined") {
12        var jeroenwijering = new Object();
13        jeroenwijering.utils = new Object();
14}
15
16
17
18
19
20
21
22
23
24
25/****************************************************************************
26* The player wrapper; loads config variables and starts MVC cycle.
27****************************************************************************/
28jeroenwijering.Player = function(cnt,src,cfg) {
29        this.controller;
30        this.model;
31        this.view;
32        this.configuration = {
33                backgroundcolor:'FFFFFF',
34                windowless:'false',
35                file:'',
36                height:'260',
37                image:'',
38                backcolor:'FFFFFF',
39                frontcolor:'000000',
40                lightcolor:'000000',
41                screencolor:'000000',
42                width:'320',
43                logo:'',
44                overstretch:'false',
45                shownavigation:'true',
46                showstop:'false',
47                showdigits:'true',
48                usefullscreen:'true',
49                usemute:'false',
50                autostart:'false',
51                bufferlength:'3',
52                duration:'0',
53                repeat:'false',
54                sender:'',
55                start:'0',
56                volume:'90',
57                link:'',
58                linkfromdisplay:'false',
59                linktarget:'_self'
60        };
61        for(itm in this.configuration) {
62                if(cfg[itm] != undefined) {
63                        if (itm.indexOf('color') > 0) {
64                                this.configuration[itm] = cfg[itm].substr(cfg[itm].length-6);
65                        } else {
66                                this.configuration[itm] = cfg[itm];
67                        }
68                }
69        }
70        Silverlight.createObjectEx({
71                source:src,
72                parentElement:cnt,
73                properties:{
74                        width:this.configuration['width'],
75                        height:this.configuration['height'],
76                        version:'1.0',
77                        inplaceInstallPrompt:true,
78                        isWindowless:this.configuration['windowless'],
79                        background:'#'+this.configuration['backgroundcolor']
80                },
81                events:{
82                        onLoad:this.onLoadHandler,
83                        onError:null
84                },
85                context:this
86        });
87}
88
89jeroenwijering.Player.prototype = {
90        addListener: function(typ,fcn) {
91                this.view.listeners.push({type:typ,func:fcn});
92        },
93
94        getConfig: function() {
95                return this.configuration;
96        },
97
98        onLoadHandler: function(pid,tgt,sdr) {
99                tgt.configuration['sender'] = sdr;
100                tgt.controller = new jeroenwijering.Controller(tgt.configuration);
101                tgt.view = new jeroenwijering.View(tgt.configuration,tgt.controller);
102                tgt.model = new jeroenwijering.Model(tgt.configuration,tgt.controller,tgt.view);
103                tgt.controller.startMVC(tgt.view,tgt.model);
104        },
105
106        sendEvent: function(typ,prm) {
107                switch(typ.toUpperCase()) {
108                        case 'LINK':
109                                this.controller.setLink();
110                                break;
111                        case 'LOAD':
112                                this.controller.setLoad(prm);
113                                break;
114                        case 'MUTE':
115                                this.controller.setMute();
116                                break;
117                        case 'PLAY':
118                                this.controller.setPlay();
119                                break;
120                        case 'SCRUB':
121                                this.controller.setScrub(prm);
122                                break;
123                        case 'STOP':
124                                this.controller.setStop();
125                                break;
126                        case 'VOLUME':
127                                this.controller.setVolume(prm);
128                                break;
129                        case 'FULLSCREEN':
130                                this.controller.setFullscreen();
131                                break;
132                }
133        }
134}
135
136
137
138
139
140
141
142
143
144
145/****************************************************************************
146* The controller of the player MVC triad, which processes all user input.
147****************************************************************************/
148jeroenwijering.Controller = function(cfg) {
149        this.configuration = cfg;
150}
151
152jeroenwijering.Controller.prototype = {
153        startMVC: function(vie,mdl) {
154                this.view = vie;
155                this.model = mdl;
156                if(this.configuration['usemute'] == 'true') {
157                        this.view.onVolume(0);
158                        this.view.onMute(true);
159                        this.model.goVolume(0);
160                } else {
161                        this.view.onVolume(this.configuration['volume']);
162                        this.model.goVolume(this.configuration['volume']);
163                }
164                if(this.configuration['autostart'] == 'true') {
165                        this.model.goStart();
166                } else {
167                        this.model.goPause();
168                }
169        },
170
171        setState: function(old,stt) {
172                this.state = stt;
173                var pos = this.configuration['start'];
174                if(old == 'Closed' && pos > 0) {
175                        setTimeout(jeroenwijering.utils.delegate(this,this.setScrub),200,pos);
176                }
177        },
178
179        setLink: function() {
180                if (this.configuration['linktarget'].indexOf('javascript:') == 0) {
181                        return Function(this.configuration['linktarget']).apply();
182                } else if (this.configuration['linktarget'] == '_blank') {
183                        window.open(this.configuration['link']);
184                } else if (this.configuration['linktarget'] != '') {
185                        window.location = this.configuration['link'];
186                }
187        },
188
189        setLoad: function(fil) {
190                if(this.model.state != "Closed") {
191                        this.model.goStop();
192                }
193                this.configuration['file'] = fil;
194                if(this.configuration['autostart'] == 'true') {
195                        setTimeout(jeroenwijering.utils.delegate(this.model,this.model.goStart),100);
196                }
197        },
198
199        setMute: function() {
200                if(this.configuration['usemute'] == 'true') {
201                        this.configuration['usemute'] = 'false';
202                        this.model.goVolume(this.configuration['volume']);
203                        this.view.onMute(false);
204                } else {
205                        this.configuration['usemute'] = 'true';
206                        this.model.goVolume(0);
207                        this.view.onMute(true);
208                }
209        },
210
211        setPlay: function() {
212                if(this.state == 'Buffering' || this.state == 'Playing') {
213                        if(this.configuration['duration'] == 0) {
214                                this.model.goStop();
215                        } else {
216                                this.model.goPause();
217                        }
218                } else {
219                        this.model.goStart();
220                }
221        },
222
223        setScrub: function(sec) {
224                if(sec < 2) {
225                        sec = 0;
226                } else if (sec > this.configuration['duration']-4) {
227                        sec = this.configuration['duration']-4;
228                }
229                if(this.state == 'Buffering' || this.state == 'Playing') {
230                        this.model.goStart(sec);
231                } else {
232                        this.model.goPause(sec);
233                }
234        },
235
236        setStop: function() {
237                this.model.goStop();
238        },
239
240        setVolume: function(pct) {
241                if(pct < 0) { pct = 0; } else if(pct > 100) { pct = 100; }
242                this.configuration['volume'] = Math.round(pct);
243                this.model.goVolume(pct);
244                this.view.onVolume(pct);
245                if(this.configuration['usemute'] == 'true') {
246                        this.configuration['usemute'] = 'false';
247                        this.view.onMute(false);
248                }
249        },
250
251        setFullscreen: function() {
252                var fss = !this.configuration['sender'].getHost().content.FullScreen;
253                this.configuration['sender'].getHost().content.FullScreen = fss;
254                jeroenwijering.utils.delegate(this.view,this.view.onFullscreen);
255        }
256}
257
258
259
260
261
262
263
264
265
266
267/****************************************************************************
268* The view of the player MVC triad, which manages the graphics.
269****************************************************************************/
270jeroenwijering.View = function(cfg,ctr) {
271        this.configuration = cfg;
272        this.listeners = Array();
273        this.controller = ctr;
274        this.fstimeout;
275        this.fslistener;
276        this.display = this.configuration['sender'].findName("PlayerDisplay");
277        this.controlbar = this.configuration['sender'].findName("PlayerControls");
278        this.configuration['sender'].getHost().content.onResize =
279                jeroenwijering.utils.delegate(this,this.resizePlayer);
280        this.configuration['sender'].getHost().content.onFullScreenChange =
281                jeroenwijering.utils.delegate(this,this.onFullscreen);
282        this.assignColorsClicks();
283        this.resizePlayer();
284}
285
286jeroenwijering.View.prototype = {
287        onBuffer: function(pct) {
288                var snd = this.configuration['sender'];
289                if(pct == 0) {
290                        snd.findName("BufferText").Text = null;
291                } else {
292                        pct < 10 ? pct = "0"+pct: pct = ""+pct;
293                        snd.findName("BufferText").Text = pct;
294                }
295                this.delegate('BUFFER',Array(pct));
296        },
297
298        onFullscreen: function(fss) {
299                var snd = this.configuration['sender'];
300                var fst = snd.getHost().content.FullScreen;
301                if(fst) {
302                        this.fstimeout = setTimeout(jeroenwijering.utils.delegate(this,
303                                this.hideFSControls),2000);
304                        this.fslistener = this.display.addEventListener('MouseMove',
305                                jeroenwijering.utils.delegate(this,this.showFSControls));
306                        snd.findName("FullscreenSymbol").Visibility = "Collapsed";
307                        snd.findName("FullscreenOffSymbol").Visibility = "Visible";
308                } else {
309                        clearTimeout(this.fstimeout);
310                        this.display.removeEventListener("MouseMove",this.fslistener);
311                        this.controlbar.Visibility = "Visible";
312                        this.display.Cursor = "Hand";
313                        snd.findName("FullscreenSymbol").Visibility = "Visible";
314                        snd.findName("FullscreenOffSymbol").Visibility = "Collapsed";
315                }
316                this.resizePlayer();
317                this.delegate('FULLSCREEN');
318        },
319
320        showFSControls: function(sdr,arg) {
321                var vbt = sdr.findName('PlayerControls');
322                var yps = arg.GetPosition(vbt).Y;
323                clearTimeout(this.fstimeout);
324                this.controlbar.Visibility = "Visible";
325                this.display.Cursor = "Hand";
326                if(yps < 0) {
327                        this.fstimeout = setTimeout(jeroenwijering.utils.delegate(this,
328                                this.hideFSControls),2000);
329                }
330        },
331
332        hideFSControls: function() {
333                this.controlbar.Visibility = "Collapsed";
334                this.display.Cursor = "None";
335        },
336
337        onLoad: function(pct) {
338                var snd = this.configuration['sender'];
339                var max = snd.findName("TimeSlider").Width;
340                snd.findName("DownloadProgress").Width = Math.round(max*pct/100);
341                this.delegate('LOAD',Array(pct));
342        },
343
344        onMute: function(mut) {
345                var snd = this.configuration['sender'];
346                this.configuration['usemute'] = ''+mut;
347                if(mut) {
348                        snd.findName("VolumeHighlight").Visibility = "Collapsed";
349                        snd.findName("MuteSymbol").Visibility = "Visible";
350                        snd.findName("MuteOffSymbol").Visibility = "Collapsed";
351                        if(this.state == 'Playing') {
352                                snd.findName("MuteIcon").Visibility = "Visible";
353                        }
354                } else {
355                        snd.findName("VolumeHighlight").Visibility = "Visible";
356                        snd.findName("MuteSymbol").Visibility = "Collapsed";
357                        snd.findName("MuteOffSymbol").Visibility = "Visible";
358                        snd.findName("MuteIcon").Visibility = "Collapsed";
359                }
360                this.delegate('MUTE');
361        },
362
363        onState: function(old,stt) {
364                var snd = this.configuration['sender'];
365                this.state = stt;
366                if(stt == 'Buffering' || stt == 'Playing' || stt == 'Opening') {
367                        snd.findName("PlayIcon").Visibility = "Collapsed";
368                        snd.findName("PlaySymbol").Visibility = "Collapsed";
369                        snd.findName("PlayOffSymbol").Visibility = "Visible";
370                        if (stt=='Playing') {
371                                snd.findName("BufferIcon").Visibility = "Collapsed";
372                                snd.findName("BufferText").Visibility = "Collapsed";
373                                if(this.configuration['usemute'] == 'true') {
374                                        snd.findName("MuteIcon").Visibility = "Visible";
375                                }
376                        } else{
377                                snd.findName("BufferIcon").Visibility = "Visible";
378                                snd.findName("BufferText").Visibility = "Visible";
379                        }
380                } else {
381                        snd.findName("MuteIcon").Visibility = "Collapsed";
382                        snd.findName("BufferIcon").Visibility = "Collapsed";
383                        snd.findName("BufferText").Visibility = "Collapsed";
384                        snd.findName("PlaySymbol").Visibility = "Visible";
385                        snd.findName("PlayOffSymbol").Visibility = "Collapsed";
386                        snd.findName("PlayIcon").Visibility = "Visible";
387                }
388                try {
389                        if(!(old == 'Completed' && stt == 'Buffering') &&
390                                !(old == 'Buffering' && stt == 'Paused')) {
391                                playerStatusChange(old.toUpperCase(),stt.toUpperCase());
392                        }
393                } catch (err) {}
394                this.delegate('STATE',Array(old,stt));
395        },
396
397        onTime: function(elp,dur) {
398                var snd = this.configuration['sender'];
399                var snd = this.configuration['sender'];
400                var max = snd.findName("TimeSlider").Width;
401                if(dur > 0) {
402                        var pos = Math.round(max*elp/dur);
403                        this.configuration['duration'] = dur;
404                        snd.findName("ElapsedText").Text = jeroenwijering.utils.timestring(elp);
405                        snd.findName("RemainingText").Text = jeroenwijering.utils.timestring(dur-elp);
406                        snd.findName("TimeSymbol").Visibility = "Visible";
407                        snd.findName("TimeSymbol")['Canvas.Left'] = pos+4;
408                        snd.findName("TimeHighlight").Width = pos-2;
409                } else  {
410                        snd.findName("TimeSymbol").Visibility = "Collapsed";
411                }
412                this.delegate('TIME',Array(elp,dur));
413        },
414
415        onVolume: function(pct) {
416                var snd = this.configuration['sender'];
417                snd.findName("VolumeHighlight").Width = Math.round(pct/5);
418                this.delegate('VOLUME',Array(pct));
419        },
420
421        assignColorsClicks: function() {
422                this.display.Cursor = "Hand";
423                this.display.Background = "#FF"+this.configuration['screencolor'];
424                if(this.configuration['linkfromdisplay'] == 'false') {
425                        this.display.addEventListener('MouseLeftButtonUp',
426                                jeroenwijering.utils.delegate(this.controller,
427                                this.controller.setPlay));
428                } else {
429                        this.display.addEventListener('MouseLeftButtonUp',
430                                jeroenwijering.utils.delegate(this.controller,
431                                this.controller.setLink));
432                }
433                if(this.configuration['logo'] != '') {
434                        this.display.findName('OverlayCanvas').Visibility = "Visible";
435                        this.display.findName('OverlayLogo').ImageSource =
436                                this.configuration['logo'];
437                }
438                this.controlbar.findName("ControlbarBack").Fill =
439                        "#FF"+this.configuration['backcolor'];
440                this.assignButton('Play',this.controller.setPlay);
441                this.assignButton('Stop',this.controller.setStop);
442                this.configuration['sender'].findName('ElapsedText').Foreground =
443                        "#FF"+this.configuration['frontcolor'];
444                this.assignSlider('Time',this.changeTime);
445                this.configuration['sender'].findName('DownloadProgress').Fill =
446                        "#FF"+this.configuration['frontcolor'];
447                this.configuration['sender'].findName('RemainingText').Foreground =
448                        "#FF"+this.configuration['frontcolor'];
449                this.assignButton('Link',this.controller.setLink);
450                this.assignButton('Fullscreen',this.controller.setFullscreen);
451                this.assignButton('Mute',this.controller.setMute);
452                this.assignSlider('Volume',this.changeVolume);
453        },
454
455        assignButton: function(btn,act) {
456                var el1 = this.configuration['sender'].findName(btn+'Button');
457                el1.Cursor = "Hand";
458                el1.addEventListener('MouseLeftButtonUp',
459                        jeroenwijering.utils.delegate(this.controller,act));
460                el1.addEventListener('MouseEnter',
461                        jeroenwijering.utils.delegate(this,this.rollOver));
462                el1.addEventListener('MouseLeave',
463                        jeroenwijering.utils.delegate(this,this.rollOut));
464                this.configuration['sender'].findName(btn+'Symbol').Fill =
465                        "#FF"+this.configuration['frontcolor'];
466                try {
467                        this.configuration['sender'].findName(btn+'OffSymbol').Fill =
468                                "#FF"+this.configuration['frontcolor'];
469                } catch(e) {}
470        },
471
472        assignSlider: function(sld,act) {
473                var el1 = this.configuration['sender'].findName(sld+'Button');
474                el1.Cursor = "Hand";
475                el1.addEventListener('MouseLeftButtonUp',
476                        jeroenwijering.utils.delegate(this,act));
477                el1.addEventListener('MouseEnter',
478                        jeroenwijering.utils.delegate(this,this.rollOver));
479                el1.addEventListener('MouseLeave',
480                        jeroenwijering.utils.delegate(this,this.rollOut));
481                this.configuration['sender'].findName(sld+'Slider').Fill =
482                        "#FF"+this.configuration['frontcolor'];
483                this.configuration['sender'].findName(sld+'Highlight').Fill =
484                        "#FF"+this.configuration['frontcolor'];
485                this.configuration['sender'].findName(sld+'Symbol').Fill =
486                        "#FF"+this.configuration['frontcolor'];
487        },
488
489        delegate: function(typ,arg) {
490                for(var i=0; i<this.listeners.length; i++) {
491                        if(this.listeners[i]['type'].toUpperCase() == typ) {
492                                this.listeners[i]['func'].apply(null,arg);
493                        }
494                }
495        },
496
497        rollOver: function(sdr) {
498                var str = sdr.Name.substr(0,sdr.Name.length-6);
499                this.configuration['sender'].findName(str+'Symbol').Fill =
500                        "#FF"+this.configuration['lightcolor'];
501                try {
502                        this.configuration['sender'].findName(str+'OffSymbol').Fill =
503                                "#FF"+this.configuration['lightcolor'];
504                } catch(e) {}
505        },
506
507        rollOut: function(sdr) {
508                var str = sdr.Name.substr(0,sdr.Name.length-6);
509                this.configuration['sender'].findName(str+'Symbol').Fill =
510                        "#FF"+this.configuration['frontcolor'];
511                try {
512                        this.configuration['sender'].findName(str+'OffSymbol').Fill =
513                                "#FF"+this.configuration['frontcolor'];
514                } catch(e) {}
515        },
516
517        changeTime: function(sdr,arg) {
518                var tbt = sdr.findName('TimeSlider');
519                var xps = arg.GetPosition(tbt).X;
520                var sec = Math.floor(xps/tbt.Width*this.configuration['duration']);
521                this.controller.setScrub(sec);
522        },
523
524        changeVolume: function(sdr,arg) {
525                var vbt = sdr.findName('VolumeButton');
526                var xps = arg.GetPosition(vbt).X;
527                this.controller.setVolume(xps*5);
528        },
529
530        resizePlayer: function() {
531                var wid = this.configuration['sender'].getHost().content.actualWidth;
532                var hei = this.configuration['sender'].getHost().content.actualHeight;
533                var fss = this.configuration['sender'].getHost().content.FullScreen;
534                if(this.configuration['shownavigation'] == 'true') {
535                        if(fss == true) {
536                                this.resizeDisplay(wid,hei);
537                                this.controlbar['Canvas.Left'] = Math.round(wid/2-250);
538                                this.resizeControlbar(500,hei-this.controlbar.Height-16);
539                                this.controlbar.findName('ControlbarBack')['Opacity'] = 0.5;
540                        } else {
541                                this.resizeDisplay(wid,hei-20);
542                                this.controlbar['Canvas.Left'] = 0;
543                                this.resizeControlbar(wid,hei-this.controlbar.Height);
544                                this.controlbar.findName('ControlbarBack')['Opacity'] = 1;
545                        }
546                } else {
547                        this.resizeDisplay(wid,hei);
548                }
549        },
550
551        resizeDisplay: function(wid,hei) {
552                this.stretchElement('PlayerDisplay',wid,hei);
553                this.stretchElement('VideoWindow',wid,hei);
554                this.stretchElement('PlaceholderImage',wid,hei);
555                this.centerElement('PlayIcon',wid,hei);
556                this.centerElement('MuteIcon',wid,hei);
557                this.centerElement('BufferIcon',wid,hei);
558                this.centerElement('BufferText',wid,hei);
559                this.display.findName('OverlayCanvas')['Canvas.Left'] = wid-110;
560                this.display.Visibility = "Visible";
561        },
562
563        resizeControlbar: function(wid,yps,alp) {
564                this.controlbar['Canvas.Top'] = yps;
565                this.stretchElement('PlayerControls',wid);
566                this.stretchElement('ControlbarBack',wid);
567                this.placeElement('PlayButton',0);
568                var lft = 17;
569                this.placeElement('VolumeButton',wid-24);
570                this.placeElement('MuteButton',wid-37);
571                var rgt = 37;
572                if(this.configuration['showstop'] == 'true') {
573                        this.placeElement('StopButton',lft);
574                        lft += 17;
575                } else {
576                        this.controlbar.findName('StopButton').Visibility="Collapsed";
577                }
578                if(this.configuration['usefullscreen'] == 'true') {
579                        rgt += 18;
580                        this.placeElement('FullscreenButton',wid-rgt);
581                } else {
582                        this.controlbar.findName('FullscreenButton').Visibility =
583                                "Collapsed";
584                }
585                if(this.configuration['link'] != '') {
586                        rgt += 18;
587                        this.placeElement('LinkButton',wid-rgt);
588                } else {
589                        this.controlbar.findName('LinkButton').Visibility="Collapsed";
590                }
591                if(this.configuration['showdigits'] == 'true' && wid-rgt-lft> 160) {
592                        rgt += 35;
593                        this.controlbar.findName('RemainingButton').Visibility="Visible";
594                        this.controlbar.findName('ElapsedButton').Visibility="Visible";
595                        this.placeElement('RemainingButton',wid-rgt);
596                        this.placeElement('ElapsedButton',lft);
597                        lft +=35;
598                } else {
599                        this.controlbar.findName('RemainingButton').Visibility =
600                                "Collapsed";
601                        this.controlbar.findName('ElapsedButton').Visibility="Collapsed";
602                }
603                this.placeElement('TimeButton',lft);
604                this.stretchElement('TimeButton',wid-lft-rgt);
605                this.stretchElement('TimeShadow',wid-lft-rgt);
606                this.stretchElement('TimeStroke',wid-lft-rgt);
607                this.stretchElement('TimeFill',wid-lft-rgt);
608                this.stretchElement('TimeSlider',wid-lft-rgt-10);
609                this.stretchElement('DownloadProgress',wid-lft-rgt-10);
610                var tsb = this.configuration['sender'].findName('TimeSymbol');
611                this.stretchElement('TimeHighlight',tsb['Canvas.Left']-5);
612                this.controlbar.Visibility = "Visible";
613        },
614
615        centerElement: function(nam,wid,hei) {
616                var elm = this.configuration['sender'].findName(nam);
617                elm['Canvas.Left'] = Math.round(wid/2 - elm.Width/2);
618                elm['Canvas.Top'] = Math.round(hei/2 - elm.Height/2);
619        },
620
621        stretchElement: function(nam,wid,hei) {
622                var elm = this.configuration['sender'].findName(nam);
623                elm.Width = wid;
624                if (hei != undefined) { elm.Height = hei; }
625        },
626
627        placeElement: function(nam,xps,yps) {
628                var elm = this.configuration['sender'].findName(nam);
629                elm['Canvas.Left'] = xps;
630                if(yps) { elm['Canvas.Top'] = yps; }
631        }
632}
633
634
635
636
637
638
639
640
641
642
643/****************************************************************************
644* The model of the player MVC triad, which stores all playback logic.
645****************************************************************************/
646jeroenwijering.Model = function(cfg,ctr,vie) {
647        this.configuration = cfg;
648        this.controller = ctr;
649        this.view = vie;
650        this.video = this.configuration['sender'].findName("VideoWindow");
651        this.preview = this.configuration['sender'].findName("PlaceholderImage");
652        var str = {
653                'true':'UniformToFill',
654                'false':'Uniform',
655                'fit':'Fill',
656                'none':'None'
657        }
658        this.state = this.video.CurrentState;
659        this.timeint;
660        this.video.Stretch = str[this.configuration['overstretch']];
661        this.preview.Stretch = str[this.configuration['overstretch']];
662        this.video.BufferingTime =
663                jeroenwijering.utils.spanstring(this.configuration['bufferlength']);
664        this.video.AutoPlay = true;
665        this.video.AddEventListener("CurrentStateChanged",
666                jeroenwijering.utils.delegate(this,this.stateChanged));
667        this.video.AddEventListener("MediaEnded",
668                jeroenwijering.utils.delegate(this,this.mediaEnded));
669        this.video.AddEventListener("BufferingProgressChanged",
670                jeroenwijering.utils.delegate(this,this.bufferChanged));
671        this.video.AddEventListener("DownloadProgressChanged",
672                jeroenwijering.utils.delegate(this,this.downloadChanged));
673        if(this.configuration['image'] != '') {
674                this.preview.Source = this.configuration['image'];
675        }
676}
677
678jeroenwijering.Model.prototype = {
679        goPause: function(sec) {
680                this.video.pause();
681                if(!isNaN(sec)) {
682                        this.video.Position = jeroenwijering.utils.spanstring(sec);
683                }
684                this.timeChanged();
685        },
686
687        goStart: function(sec) {
688                this.video.Visibility = 'Visible';
689                this.preview.Visibility = 'Collapsed';
690                if(this.state == "Closed") {
691                        this.video.Source = this.configuration['file'];
692                } else {
693                        this.video.play();
694                }
695                if(!isNaN(sec)) {
696                        this.video.Position = jeroenwijering.utils.spanstring(sec);
697                }
698        },
699
700        goStop: function() {
701                this.video.Visibility = 'Collapsed';
702                this.preview.Visibility = 'Visible';
703                this.goPause(0);
704                this.video.Source = 'null';
705                this.view.onBuffer(0);
706        },
707
708        goVolume: function(pct) {
709                this.video.Volume = pct/100;
710        },
711
712        stateChanged: function() {
713                var stt = this.video.CurrentState;
714                if(stt != this.state) {
715                        this.controller.setState(this.state,stt);
716                        this.view.onState(this.state,stt);
717                        this.state = stt;
718                        this.configuration['duration'] =
719                                Math.round(this.video.NaturalDuration.Seconds*10)/10;
720                        if(stt != "Playing" && stt != "Buffering" && stt != "Opening") {
721                                clearInterval(this.timeint);
722                        } else {
723                                this.timeint = setInterval(jeroenwijering.utils.delegate(
724                                        this,this.timeChanged),100);
725                        }
726                }
727        },
728
729        mediaEnded: function() {
730                if(this.configuration['repeat'] == 'true') {
731                        this.goStart(0);
732                } else {
733                        this.state = 'Completed';
734                        this.view.onState(this.state,'Completed');
735                        this.video.Visibility = 'Collapsed';
736                        this.preview.Visibility = 'Visible';
737                        this.goPause(0);
738                }
739        },
740
741        bufferChanged: function() {
742                var bfr = Math.round(this.video.BufferingProgress*100);
743                this.view.onBuffer(bfr);
744        },
745
746        downloadChanged: function() {
747                var dld = Math.round(this.video.DownloadProgress*100);
748                this.view.onLoad(dld);
749        },
750
751        timeChanged: function() {
752                var pos = Math.round(this.video.Position.Seconds*10)/10;
753                this.view.onTime(pos,this.configuration['duration']);
754        }
755}
756
757
758
759
760
761
762
763
764
765
766/****************************************************************************
767* Some utility functions.
768****************************************************************************/
769jeroenwijering.utils.delegate = function(obj,fcn) {
770        return function() {
771                return fcn.apply(obj,arguments);
772        }
773}
774jeroenwijering.utils.timestring = function(stp) {
775        var hrs = Math.floor(stp/3600);
776        var min = Math.floor(stp%3600/60);
777        var sec = Math.round(stp%60);
778        var str = "";
779        sec > 9 ? str += sec: str +='0'+sec;
780        min > 9 ? str = min+":"+str: str='0'+min+":"+str;
781        hrs > 0 ? str = hrs+":"+str: null;
782        return str;
783}
784jeroenwijering.utils.spanstring = function(stp) {
785        var hrs = Math.floor(stp/3600);
786        var min = Math.floor(stp%3600/60);
787        var sec = Math.round(stp%60*10)/10;
788        var str = hrs+':'+min+':'+sec;
789        return str;
790}
Note: See TracBrowser for help on using the browser.