source: Dev/trunk/src/client/dojox/av/_Media.js @ 532

Last change on this file since 532 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 8.9 KB
Line 
1define(['dojo'],function(dojo){
2
3        dojo.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                //
26                //              Note: Older FLVs, without a duration, cannot be buffered.
27                bufferTime: 2000,
28
29                // minBufferTime: Number
30                //              Time in milliseconds between 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
93                        //              Note:
94                        //              Consider the onPosition event, which returns
95                        //              the time at a set interval. Too many trips to
96                        //              the SWF could impact performance.
97                        return this.flashMedia.getTime(); // Float
98                },
99       
100                //  =============  //
101                //  Player Events  //
102                //  =============  //
103       
104                onLoad: function(/* SWF */ mov){
105                        // summary:
106                        //              Fired when the SWF player has loaded
107                        //              NOT when the video has loaded
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                        //              Fires when the player is clicked
119                        //              Could be used to toggle play/pause, or
120                        //              do an external activity, like opening a new
121                        //              window.
122
123                        // TODO: Return x/y of click
124                },
125       
126                onSwfSized: function(/* Object */ data){
127                        // summary:
128                        //              Fired on SWF resize, or when its
129                        //              toggled between fullscreen.
130                },
131       
132                onMetaData: function(/* Object */ data, /* Object */ evt){
133                        // summary:
134                        //              The video properties. Width, height, duration, etc.
135
136                        // NOTE:        if data is empty, this is an older FLV with no meta data.
137                        //                      Duration cannot be determined. In original FLVs, duration
138                        //                      could only be obtained with Flash Media Server.
139                        // NOTE:        Older FLVs can still return width and height
140                        //                      and will do so on a second event call
141
142                        console.warn("onMeta", data)
143                        this.duration = data.duration;
144                },
145       
146                onPosition: function(/* Float */ time){
147                        // summary:
148                        //              The position of the playhead in seconds
149                },
150       
151                onStart: function(/* Object */ data){
152                        // summary:
153                        //              Fires when video starts
154                        //              Good for setting the play button to pause
155                        //              during an autoPlay for example
156                },
157       
158                onPlay: function(/* Object */ data){
159                        // summary:
160                        //              Fires when video starts and resumes
161                },
162       
163                onPause: function(/* Object */ data){
164                        // summary:
165                        //              Fires when the pause button is clicked
166                },
167       
168                onEnd: function(/* Object */ data){
169                        // summary:
170                        //              Fires when video ends
171                        //              Could be used to change pause button to play
172                        //              or show a post video graphic, like YouTube
173                },
174       
175                onStop: function(){
176                        // summary:
177                        //              Fire when the Stop button is clicked
178                       
179                        // TODO:        This is not hooked up yet and shouldn't
180                        //                      fire.
181                },
182       
183                onBuffer: function(/* Boolean */ isBuffering){
184                        // summary:
185                        //              Fires a boolean to tell if media
186                        //              is paused for buffering or if buffering
187                        //              has finished
188                        this.isBuffering = isBuffering;
189                },
190       
191                onError: function(/* Object */ data, /* String */ url){
192                        // summary:
193                        //              Fired when the player encounters an error
194                        // example:
195                        //              | console.warn("ERROR-"+data.type.toUpperCase()+":",
196                        //              |               data.info.code, " - URL:", url);
197                        console.warn("ERROR-"+data.type.toUpperCase()+":", data.info.code, " - URL:", url);
198                },
199       
200                onStatus: function(/* Object */data){
201                        // summary:
202                        //              Simple status
203                },
204       
205                onPlayerStatus: function(/* Object */data){
206                        // summary:
207                        //              The status of the video from the SWF
208                        //              playing, stopped, bufering, etc.
209                },
210       
211                onResize: function(){
212       
213                },
214       
215                _figureStatus: function(){
216                        // summary:
217                        //              Calculate media status, based on playhead movement, and
218                        //              onStop and onStart events
219
220                        // TODO:
221                        //              Figure in real status from the media for more accurate results.
222
223                        var pos = this.getTime();
224                        //console.log(pos, this.duration,  (pos>this.duration-.5), (this.duration && pos>this.duration-.5))
225       
226                        if(this.status=="stopping"){
227                                // stop was fired, need to fake pos==0
228                                this.status = "stopped";
229                                this.onStop(this._eventFactory());
230       
231                        }else if(this.status=="ending" && pos==this._prevPos){
232                                this.status = "ended";
233                                this.onEnd(this._eventFactory());
234       
235                        }else if(this.duration && pos>this.duration-.5){
236                                this.status="ending"
237       
238                        }else if(pos===0 ){//|| this.status == "stopped"
239                                if(this.status == "ready"){
240                                        //never played
241                                }else{
242                                        //stopped
243                                        this.status = "stopped";
244                                        if(this._prevStatus != "stopped"){
245                                                this.onStop(this._eventFactory());
246                                        }
247                                }
248       
249                        }else{
250                                // pos > 0
251                                if(this.status == "ready"){
252                                        //started
253                                        this.status = "started";
254                                        this.onStart(this._eventFactory());
255                                        this.onPlay(this._eventFactory());
256       
257                                }else if(this.isBuffering){
258                                        this.status = "buffering";
259       
260                                }else if(this.status == "started" || (this.status == "playing" &&  pos != this._prevPos)){
261                                        this.status = "playing";
262                                        //this.onPosition(this._eventFactory());
263       
264                                }else if(!this.isStopped && this.status == "playing" && pos == this._prevPos){
265                                        this.status = "paused";
266                                        console.warn("pause", pos, this._prevPos)
267                                        if(this.status != this._prevStatus){
268                                                this.onPause(this._eventFactory());
269                                        }
270       
271                                }else if((this.status == "paused" ||this.status == "stopped") && pos != this._prevPos){
272                                        this.status = "started";
273                                        this.onPlay(this._eventFactory());
274                                }
275                        }
276       
277                        this._prevPos = pos;
278                        this._prevStatus = this.status;
279                        this.onStatus(this.status);
280       
281       
282                },
283       
284                _eventFactory: function(){
285                        // summary:
286                        //              Creates a generic event object.
287                        //
288                        var evt = {
289                                //position:this._channel.position,
290                                //seconds:this.toSeconds(this._channel.position*.001),
291                                //percentPlayed:this._getPercent(),
292                                status:this.status
293                        };
294                        return evt; // Object
295                },
296       
297       
298       
299                _sub: function(topic, method){
300                        // summary:
301                        //              helper for subscribing to topics
302                        dojo.subscribe(this.id+"/"+topic, this, method);
303                },
304       
305                _normalizeVolume: function(vol){
306                        // summary:
307                        //              Ensures volume is less than one
308                        //
309                        if(vol>1){
310                                while(vol>1){
311                                        vol*=.1
312                                }
313                        }
314                        return vol;
315                },
316       
317                _normalizeUrl: function(_url){
318                        // summary:
319                        //              Checks that path is relative to HTML file or
320                        //              converts it to an absolute path.
321
322                        console.log("  url:", _url);
323       
324                        if(_url && (_url.toLowerCase().indexOf("http")<0 || _url.indexOf("/") == 0)){
325                                //
326                                // Appears to be a relative path. Attempt to  convert it to absolute,
327                                // so it will better target the SWF.
328                                var loc = window.location.href.split("/");
329                                loc.pop();
330       
331                                loc = loc.join("/")+"/";
332                                console.log("  loc:", loc);
333                                _url = loc+_url;
334                        }
335                        return _url;
336                },
337       
338                destroy: function(){
339                        // summary:
340                        //              destroys flash
341                        if(!this.flashMedia){
342                                this._cons.push(dojo.connect(this, "onLoad", this, "destroy"));
343                                return;
344                        }
345                        dojo.forEach(this._subs, function(s){
346                                dojo.unsubscribe(s);
347                        });
348                        dojo.forEach(this._cons, function(c){
349                                dojo.disconnect(c);
350                        });
351                        this._flashObject.destroy();
352                        //dojo._destroyElement(this.flashDiv);
353       
354                }
355        });
356});
Note: See TracBrowser for help on using the repository browser.