source: Dev/branches/rest-dojo-ui/client/dojox/av/_Media.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 9.0 KB
Line 
1define(['dojo'],function(dojo){
2
3dojo.experimental("dojox.av.FLVideo");
4
5        return dojo.declare("dojox.av._Media", null, {
6                // summary:
7                //              Used as a mixin for dojox and AIR media
8                //      description:
9                //              Calculates the current status of the playing media and fires
10                //              the appropriate events.
11                //
12                mediaUrl:"",
13                //
14                // initialVolume: Float?
15                //              The initial volume setting of the player. Acccepts between 0 and 1.
16                initialVolume:1,
17                //
18                //  autoPlay:Boolean?
19                //              Whether the video automatically plays on load or not.
20                autoPlay: false,
21                //
22                //      bufferTime: Number?
23                //              Time in milliseconds that the video should be loaded before it will
24                //              play. May pause and resume to build up buffer. Prevents stuttering.
25                //      Note:
26                //              Older FLVs, without a duration, cannot be buffered.
27                bufferTime: 2000,
28                //
29                //      minBufferTime: Number
30                //              Time in milliseconds bwteen the playhead time and loaded time that
31                //              will trigger the buffer. When buffer is triggered, video will pause
32                //              until the bufferTime amount is buffered.
33                //              Note: Should be a small number, greater than zero.
34                minBufferTime:300,
35                //
36                // updateTime: Number
37                //              How often, in milliseconds to get an update of the video position.
38                updateTime: 100,
39                //
40                //  id: String?
41                //              The id of this widget and the id of the SWF movie.
42                id:"",
43                //
44                // isDebug: Boolean?
45                //              Setting to true tells the SWF to output log messages to Firebug.
46                isDebug: false,
47                //
48                // percentDownloaded: read-only-Number
49                //              The percentage the media has downloaded; from 0-100
50                percentDownloaded:0,
51                //
52                // _flashObject: read-only-Object
53                //      The dojox.embed object
54                _flashObject:null,
55                //
56                // flashMedia: read-only-SWF
57                //              The SWF object. Methods are passed to this.
58                flashMedia:null,
59                //
60                // allowScriptAccess: String
61                //              Whether the SWF can access the container JS
62                allowScriptAccess:"always",
63                //
64                // allowNetworking: String
65                //              Whether SWF is restricted to a domain
66                allowNetworking: "all",
67                //
68                //      wmode: String
69                //              The render type of the SWF
70                wmode: "transparent",
71                //
72                //      allowFullScreen: Boolean
73                //              Whether to allow the SWF to go to fullscreen
74                allowFullScreen:true,
75       
76                _initStatus: function(){
77                        // summary:
78                        //              Connect mediaStatus to the media.
79                        //
80                        this.status = "ready";
81                        this._positionHandle = dojo.connect(this, "onPosition", this, "_figureStatus");
82       
83                },
84       
85                //  ==============  //
86                //  Player Getters  //
87                //  ==============  //
88       
89                getTime: function(){
90                        // summary:
91                        //              Returns the current time of the video
92                        //      Note:
93                        //              Consider the onPosition event, which returns
94                        //              the time at a set interval. Too many trips to
95                        //              the SWF could impact performance.
96                        return this.flashMedia.getTime(); // Float
97                },
98       
99                //  =============  //
100                //  Player Events  //
101                //  =============  //
102       
103                onLoad: function(/* SWF */ mov){
104                        // summary:
105                        //              Fired when the SWF player has loaded
106                        //              NOT when the video has loaded
107                        //
108                },
109       
110                onDownloaded: function(/* Number */percent){
111                        // summary:
112                        //              Fires the amount of that the media has been
113                        //              downloaded. Number, 0-100
114                },
115       
116                onClick: function(/* Object */ evt){
117                        // summary:
118                        //              TODO: Return x/y of click
119                        //              Fires when the player is clicked
120                        //              Could be used to toggle play/pause, or
121                        //              do an external activity, like opening a new
122                        //              window.
123                },
124       
125                onSwfSized: function(/* Object */ data){
126                        // summary:
127                        //              Fired on SWF resize, or when its
128                        //              toggled between fullscreen.
129                },
130       
131                onMetaData: function(/* Object */ data, /* Object */ evt){
132                        // summary:
133                        //              The video properties. Width, height, duration, etc.
134                        //              NOTE:   if data is empty, this is an older FLV with no meta data.
135                        //                              Duration cannot be determined. In original FLVs, duration
136                        //                              could only be obtained with Flash Media Server.
137                        //              NOTE:   Older FLVs can still return width and height
138                        //                              and will do so on a second event call
139                        console.warn("onMeta", data)
140                        this.duration = data.duration;
141                },
142       
143                onPosition: function(/* Float */ time){
144                        // summary:
145                        //              The position of the playhead in seconds
146                },
147       
148                onStart: function(/* Object */ data){
149                        // summary:
150                        //              Fires when video starts
151                        //              Good for setting the play button to pause
152                        //              during an autoPlay for example
153                },
154       
155                onPlay: function(/* Object */ data){
156                        // summary:
157                        //              Fires when video starts and resumes
158                },
159       
160                onPause: function(/* Object */ data){
161                        // summary:
162                        //              Fires when the pause button is clicked
163                },
164       
165                onEnd: function(/* Object */ data){
166                        // summary:
167                        //              Fires when video ends
168                        //              Could be used to change pause button to play
169                        //              or show a post video graphic, like YouTube
170                },
171       
172                onStop: function(){
173                        // summary:
174                        // Fire when the Stop button is clicked
175                        // TODO:        This is not hooked up yet and shouldn't
176                        //                      fire.
177                },
178       
179                onBuffer: function(/* Boolean */ isBuffering){
180                        // summary:
181                        //              Fires a boolean to tell if media
182                        //              is paused for buffering or if buffering
183                        //              has finished
184                        this.isBuffering = isBuffering;
185                },
186       
187                onError: function(/* Object */ data, /* String */ url){
188                        // summary:
189                        //              Fired when the player encounters an error
190                        // example:
191                        //              | console.warn("ERROR-"+data.type.toUpperCase()+":",
192                        //              |               data.info.code, " - URL:", url);
193                        console.warn("ERROR-"+data.type.toUpperCase()+":", data.info.code, " - URL:", url);
194                },
195       
196                onStatus: function(/* Object */data){
197                        // summary:
198                        //              Simple status
199                },
200       
201                onPlayerStatus: function(/* Object */data){
202                        // summary:
203                        //              The status of the video from the SWF
204                        //              playing, stopped, bufering, etc.
205                },
206       
207                onResize: function(){
208       
209                },
210       
211                _figureStatus: function(){
212                        // summary:
213                        //              Calculate media status, based on playhead movement, and
214                        //              onStop and onStart events
215                        // TODO:
216                        //              Figure in real status from the media for more accurate results.
217                        //
218                        var pos = this.getTime();
219                        //console.log(pos, this.duration,  (pos>this.duration-.5), (this.duration && pos>this.duration-.5))
220       
221                        if(this.status=="stopping"){
222                                // stop was fired, need to fake pos==0
223                                this.status = "stopped";
224                                this.onStop(this._eventFactory());
225       
226                        }else if(this.status=="ending" && pos==this._prevPos){
227                                this.status = "ended";
228                                this.onEnd(this._eventFactory());
229       
230                        }else if(this.duration && pos>this.duration-.5){
231                                this.status="ending"
232       
233                        }else if(pos===0 ){//|| this.status == "stopped"
234                                if(this.status == "ready"){
235                                        //never played
236                                }else{
237                                        //stopped
238                                        this.status = "stopped";
239                                        if(this._prevStatus != "stopped"){
240                                                this.onStop(this._eventFactory());
241                                        }
242                                }
243       
244                        }else{
245                                // pos > 0
246                                if(this.status == "ready"){
247                                        //started
248                                        this.status = "started";
249                                        this.onStart(this._eventFactory());
250                                        this.onPlay(this._eventFactory());
251       
252                                }else if(this.isBuffering){
253                                        this.status = "buffering";
254       
255                                }else if(this.status == "started" || (this.status == "playing" &&  pos != this._prevPos)){
256                                        this.status = "playing";
257                                        //this.onPosition(this._eventFactory());
258       
259                                }else if(!this.isStopped && this.status == "playing" && pos == this._prevPos){
260                                        this.status = "paused";
261                                        console.warn("pause", pos, this._prevPos)
262                                        if(this.status != this._prevStatus){
263                                                this.onPause(this._eventFactory());
264                                        }
265       
266                                }else if((this.status == "paused" ||this.status == "stopped") && pos != this._prevPos){
267                                        this.status = "started";
268                                        this.onPlay(this._eventFactory());
269                                }
270                        }
271       
272                        this._prevPos = pos;
273                        this._prevStatus = this.status;
274                        this.onStatus(this.status);
275       
276       
277                },
278       
279                _eventFactory: function(){
280                        // summary:
281                        //              Creates a generic event object.
282                        //
283                        var evt = {
284                                //position:this._channel.position,
285                                //seconds:this.toSeconds(this._channel.position*.001),
286                                //percentPlayed:this._getPercent(),
287                                status:this.status
288                        }
289                        return evt; // Object
290                },
291       
292       
293       
294                _sub: function(topic, method){
295                        // summary:
296                        // helper for subscribing to topics
297                        dojo.subscribe(this.id+"/"+topic, this, method);
298                },
299       
300                _normalizeVolume: function(vol){
301                        // summary:
302                        //              Ensures volume is less than one
303                        //
304                        if(vol>1){
305                                while(vol>1){
306                                        vol*=.1
307                                }
308                        }
309                        return vol;
310                },
311       
312                _normalizeUrl: function(_url){
313                        // summary:
314                        //              Checks that path is relative to HTML file or
315                        //              convertes it to an absolute path.
316                        //
317       
318                        console.log("  url:", _url);
319       
320                        if(_url && (_url.toLowerCase().indexOf("http")<0 || _url.indexOf("/") == 0)){
321                                //
322                                // Appears to be a relative path. Attempt to  convert it to absolute,
323                                // so it will better target the SWF.
324                                var loc = window.location.href.split("/");
325                                loc.pop();
326       
327                                loc = loc.join("/")+"/";
328                                console.log("  loc:", loc);
329                                _url = loc+_url;
330                        }
331                        return _url;
332                },
333       
334                destroy: function(){
335                        // summary:
336                        //              destroys flash
337                        if(!this.flashMedia){
338                                this._cons.push(dojo.connect(this, "onLoad", this, "destroy"));
339                                return;
340                        }
341                        dojo.forEach(this._subs, function(s){
342                                dojo.unsubscribe(s);
343                        });
344                        dojo.forEach(this._cons, function(c){
345                                dojo.disconnect(c);
346                        });
347                        this._flashObject.destroy();
348                        //dojo._destroyElement(this.flashDiv);
349       
350                }
351        });
352});
Note: See TracBrowser for help on using the repository browser.