OSDN Git Service

Version 0.6.90, performance fix for HTML5Audio & rename SilverLight -> Silverlight.
[pettanr/clientJs.git] / 0.6.x / js / 06_audio / 02_XSilverlightAudio.js
1 /*\r
2  * original : uupaa-js SilverlightAudio.js\r
3  * https://code.google.com/p/uupaa-js/source/browse/trunk/0.8/src/Audio/SilverlightAudio.js?r=568\r
4  */\r
5 \r
6 /*\r
7  * Silverlight 4 → 5における不具合の状況\r
8  * http://www.slideshare.net/wakabayashiy/silverlight-4-5 \r
9  *\r
10  * SilverlLight5 ie6&7(ietester,winxp), ie8(winxp) で動作確認。firefox32 では動作しない。(4以下の方がよい?)\r
11  */\r
12 var X_Audio_SLAudio, X_Audio_SLAudioWrapper,\r
13         X_Audio_SLAudio_uid = 0,\r
14         X_Audio_SLAudio_LIVE_LIST = [],\r
15         X_Audio_SLAudio_POOL_LIST = [];\r
16 \r
17 if( X.Pulgin.SilverlightEnabled ){\r
18         \r
19         function getSLAudioWrapper( proxy ){\r
20                 var i = X_Audio_SLAudio_LIVE_LIST.length;\r
21                 for( ; i; ){\r
22                         if( X_Audio_SLAudio_LIVE_LIST[ --i ].proxy === proxy ) return X_Audio_SLAudio_LIVE_LIST[ i ];\r
23                 };\r
24         };\r
25         \r
26         X_Audio_SLAudio = X.Class._override(\r
27                 new X.EventDispatcher(),\r
28                 {\r
29                         backendName : 'Silverlight Audio',\r
30 \r
31                         detect : function( source, ext ){\r
32                                 var ok = ext === 'mp3' || ext === 'wma';\r
33                                 \r
34                                 this.asyncDispatch( 0, ok ? 'support' : 'nosupport' );\r
35                                 \r
36                                 return this;\r
37                         },\r
38                         \r
39                         register : function( proxy, source, option ){\r
40                                 X_Audio_SLAudio_LIVE_LIST.push( new X_Audio_SLAudioWrapper( proxy, source, option ) );\r
41                         },\r
42                         \r
43                         close : function(){\r
44                                 return getSLAudioWrapper( this ).close();\r
45                         },\r
46                         \r
47                         play : function( position ){\r
48                                 return getSLAudioWrapper( this ).play( position );\r
49                         },\r
50                         \r
51                         pause : function(){\r
52                                 return getSLAudioWrapper( this ).pause();\r
53                         },\r
54                         \r
55                         stop : function(){\r
56                                 return getSLAudioWrapper( this ).stop();\r
57                         },\r
58                         \r
59                         loop : function( v ){\r
60                                 return getSLAudioWrapper( this ).loop( v );\r
61                         },\r
62         \r
63                         state : function(){\r
64                                 return getSLAudioWrapper( this ).state();\r
65                         },\r
66         \r
67                         volume : function( v ){\r
68                                 return getSLAudioWrapper( this ).volume( v );\r
69                         },\r
70         \r
71                         startTime : function( time ){\r
72                                 return getSLAudioWrapper( this ).startTime( time );\r
73                         },\r
74         \r
75                         currentTime : function( time ){\r
76                                 return getSLAudioWrapper( this ).currentTime( time );\r
77                         },\r
78         \r
79                         isPlaying : function(){\r
80                                 return getSLAudioWrapper( this ).isPlaying();\r
81                         }\r
82                 }\r
83         );\r
84         \r
85         X_Audio_BACKENDS.push( X_Audio_SLAudio );\r
86         \r
87         function slerror(){\r
88                 alert( 'slerror' );\r
89         };\r
90         \r
91         X_Audio_SLAudioWrapper = X.EventDispatcher.inherits(\r
92                 'X.AV.SilverlightAudioWrapper',\r
93                 X.Class.POOL_OBJECT,\r
94                 {\r
95                         _isSilverlight  : true, // for X.EventDispatcher.listen\r
96                 proxy           : null,\r
97                 _onload         : '',\r
98                 _callback       : null,                 \r
99                 xnodeObject     : null,\r
100                 \r
101                         _loop           : false,\r
102                         _source         : '',\r
103                         _error          : 0,\r
104                         _ended          : true,\r
105                         _paused         : false,\r
106                         _volume         : 0.5,\r
107                         _startTime      : 0,\r
108                         _lastUserAction : '',\r
109                         _lastState      : '',\r
110                         _duration       : 0,\r
111                         _interval       : 0, // setInterval timer id\r
112                         \r
113                         Constructor : function( proxy, source, option ){\r
114                                 \r
115                                 if( !X_Audio_SLAudio_uid ){\r
116                                         // source\r
117                                         //X.Dom.Node._systemNode.create( 'script', { type : 'text/xaml', id : 'silverlightaudio' } )\r
118                                         //      .text( '<Canvas xmlns="http://schemas.microsoft.com/client/2007" ' +\r
119                                         // 'xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></Canvas>');\r
120                                         \r
121                                         // html に以下を書いた                     \r
122                                         // <script id="silverlightaudio" type="text/xaml"><Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></Canvas></script>\r
123                                 };\r
124                                 /*\r
125                                  * [Silverlight 2]JavaScriptコードからSilverlightのオブジェクトを利用するには?[C#、VB]\r
126                                  * http://www.atmarkit.co.jp/fdotnet/dotnettips/902slobjcallfromjs/slobjcallfromjs.html\r
127                                  * このページのサンプルは sl5+firefox32 環境で動いている。xaml を js から利用する形ではなく、.xap を sl4 以下で作るのがよさそう.\r
128                                  */\r
129                                 this.proxy       = proxy;\r
130                         this._onload     = 'XAudioSilverlightOnLoad';// + ( ++X_Audio_SLAudio_uid );\r
131                                 this._callback   = window[ this._onload ] = X.Callback.create( this, this.onSLReady, [ option.autoplay ] );\r
132                         this.xnodeObject = X.Dom.Node._body\r
133                                 .create( 'object', {\r
134                                                 type   : 'application/x-silverlight-2',\r
135                                                 data   : 'data:application/x-silverlight-2,',\r
136                                                 width  : 1,\r
137                                                 height : 1\r
138                                         })\r
139                                         .html(\r
140                                             '<param name="background" value="#00000000">' +      // transparent\r
141                                             '<param name="windowless" value="true">' +\r
142                                             '<param name="source" value="#silverlightaudio">' +  // XAML ID\r
143                                             '<param name="onload" value="' + this._onload + '">'// + // bond to global\r
144                                             //'<param value="2.0.31005.0" name="minRuntimeVersion">' +\r
145                                             //'<param value="true" name="autoUpgrade">' +\r
146                                             //'<param name="onerror" value="slerror">' // bond to global\r
147                                         );\r
148                                 \r
149                             this._loop       = option.loop;\r
150                             this._source     = source;\r
151                                 if( option.volume       ) this._volume  = option.volume;\r
152                                 if( option.startTime ) this._startTime = option.startTime;\r
153         \r
154                                 this.listenOnce( X.Event.KILL_INSTANCE );\r
155                         },\r
156                         \r
157                         onSLReady : function( sender, autoplay ){\r
158                                 if( !this._onload ) return;\r
159                                 \r
160                                 window[ this._onload ] = null;\r
161                                 delete this._onload;\r
162                                 this._callback.kill();\r
163                                 delete this._callback;\r
164 \r
165                                 sender.children.add(\r
166                                         sender.GetHost().\r
167                                         content.\r
168                                         CreateFromXaml(\r
169                                         '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">' +\r
170                                                 '<MediaElement x:Name="media" Source="' + this._source + '" Volume="' + this._volume + '" AutoPlay="false" />' +\r
171                                         '</Canvas>'));\r
172                 \r
173                                 this._rawObject = sender.findName('media'); // x:Name='media'\r
174                                 \r
175                                 this.listen( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] );\r
176                                 \r
177                                 autoplay && X.Timer.once( 100, this, this.play );\r
178                         },\r
179                         \r
180                         handleEvent : function( e ){\r
181                                 var lastState, currentState;\r
182                                 \r
183                                 switch( e.type ){\r
184                                         \r
185                                         case 'MediaFailed' :\r
186                                                 this._error = 4;\r
187                                                 this._ended = true;\r
188                                                 this._paused = false;\r
189                                 \r
190                                                 this.proxy.dispatch( 'error' ); // open failed\r
191                                                 break;\r
192                                         case 'MediaOpened' :\r
193                                                 // http://msdn.microsoft.com/ja-jp/library/bb979710(VS.95).aspx\r
194                                                 this._duration = this._rawObject.NaturalDuration.Seconds;\r
195                                 \r
196                                                 this.proxy.dispatch( 'canplay' );\r
197                                                 break;\r
198                                         case 'MediaEnded' :\r
199                                                 this.currentTime(this._startTime);\r
200                                 \r
201                                                 if (this._loop) {\r
202                                                         this._rawObject.play();\r
203                                                 }\r
204                                                 break;\r
205                                         case 'CurrentStateChanged' :\r
206                                                 lastState       = this._lastState,\r
207                                                 currentState = this._rawObject.CurrentState;\r
208                                 \r
209                                                 // ignore consecutive events or 'Closed' == 'Error'\r
210                                                 if( lastState === currentState\r
211                                                         || ( (lastState === 'Closed' || lastState === 'Error') && ( currentState === 'Closed' || currentState === 'Error') ) ){\r
212                                                         return;\r
213                                                 };\r
214                                                 this._lastState = currentState; // update last state\r
215                                 \r
216                                                 switch (currentState) {\r
217                                                         case 'Buffering':\r
218                                                         case 'Opening':\r
219                                                                         break;\r
220                                         \r
221                                                         // media.play(none supported file) -> 'Error'\r
222                                                         // media.play(file not found)     -> 'Closed'\r
223                                                         // media.load -> 'Error'\r
224                                                         case 'Error':\r
225                                                         case 'Closed':\r
226                                                                         this._error = 4;\r
227                                                                         this._ended = true;\r
228                                                                         this._paused = false;\r
229                                                                         this.proxy.dispatch( 'error' );\r
230                                                                         break;\r
231                                         \r
232                                                         // userAction.pause()              -> MediaState('Paused') -> x\r
233                                                         // userAction.stop()                    -> MediaState('Paused') -> x\r
234                                                         // userAction.play() + file end -> MediaState('Paused') -> uueventfire('ended')\r
235                                                         case 'Paused':\r
236                                                                         switch (this._lastUserAction) {\r
237                                                                                 case 'play': // play() -> file end -> event('ended')\r
238                                                                                         this._ended = true;\r
239                                                                                         this._paused = false;\r
240                                                                                         this.proxy.dispatch( 'ended' );\r
241                                                                                         this.currentTime(this._startTime);\r
242                                                                                         break;\r
243                                                                                 case 'pause':\r
244                                                                                         this._ended = false;\r
245                                                                                         this._paused = true;\r
246                                                                                         break;\r
247                                                                                 case 'stop':\r
248                                                                                         this._ended = true;\r
249                                                                                         this._paused = false;\r
250                                                                         }\r
251                                                                         break;\r
252                                         \r
253                                                         // media.play -> 'Playing'\r
254                                                         case 'Playing':\r
255                                                                         this._error = 0;\r
256                                                                         this._ended = false;\r
257                                                                         this._paused = false;\r
258                                                                         this.proxy.dispatch( 'playing' );\r
259                                                                         break;\r
260                                         \r
261                                                         // stop()\r
262                                                         case 'Stopped':\r
263                                                                         this._ended = true;\r
264                                                                         this._paused = false;\r
265                                                                         this.currentTime(this._startTime);\r
266                                                 }\r
267                                                 break;\r
268                                         \r
269                                         case X.Event.KILL_INSTANCE :\r
270                                                 if( this._onload ){\r
271                                                         window[ this._onload ] = null;\r
272                                                         this._callback.kill();\r
273                                                 };\r
274                                                 this.xnodeObject.destroy();\r
275                                                 break;\r
276                                 };\r
277                         },\r
278                         \r
279                         close : function(){\r
280                                 if (this._interval) {\r
281                                         X.Timer.remove( this._interval );\r
282                                         delete this._interval;\r
283                                 };\r
284                                 this.proxy.dispatch( 'ended' );\r
285                                 this.kill();\r
286                         },\r
287                         \r
288                         // SilverlightAudio.play\r
289                         play : function(){\r
290                                 if (!this._error) {\r
291                                         this._lastUserAction = 'play';\r
292                                         if (this._ended) {\r
293                                                 this.currentTime(this._startTime);\r
294                                         }\r
295                         \r
296                                         this._rawObject.play();\r
297                                         this.proxy.dispatch( 'play' );\r
298                         \r
299                                         if (!this._interval) {\r
300                                                 this._interval = X.Timer.add( 1000, 0, this, this._onInterval );\r
301                                         }\r
302                                 }\r
303                         },\r
304                         \r
305                         _onInterval : function(){\r
306                                 this.isPlaying() && this.proxy.dispatch( 'timeupdate' );\r
307                         },\r
308                         \r
309                         // SilverlightAudio.pause\r
310                         pause : function(){\r
311                                 if (!this._error) {\r
312                                         this._lastUserAction = 'pause';\r
313                                         this._paused = true;\r
314                                         this._ended = false;\r
315                                         this._rawObject.pause();\r
316                                         this.proxy.dispatch( 'pause' );\r
317                                 }\r
318                         },\r
319                         \r
320                         // SilverlightAudio.stop\r
321                         stop : function(){\r
322                                 if (!this._error) {\r
323                                         this._lastUserAction = 'stop';\r
324                                         this._rawObject.stop();\r
325                                         this._ended = true;\r
326                                 }\r
327                         },\r
328                         \r
329                         // SilverlightAudio.loop\r
330                         loop : function(value){ // @param Boolean: true is loop\r
331                                                                    // @return Boolean/void: true is loop\r
332                                 if (value === undefined) {\r
333                                         return this._loop;\r
334                                 }\r
335                                 this._loop = value;\r
336                         },\r
337                         \r
338                         // SilverlightAudio.state\r
339                         state : function(){ // @return Hash: { loop, error, paused, ended, source, duration }\r
340                                 return {\r
341                                         loop:      this._loop,\r
342                                         error:    this._error,\r
343                                         paused:  this._paused,\r
344                                         ended:    this._ended,\r
345                                         source:  this._source,\r
346                                         duration:   this._duration\r
347                                 };\r
348                         },\r
349                         \r
350                         // SilverlightAudio.volume\r
351                         volume : function(value){ // @param Number: 0.0 ~ 1.0\r
352                                                                          // @return Number/void:\r
353                                 if (value === undefined) {\r
354                                         return this._volume;\r
355                                 }\r
356                                 this._volume = value;\r
357                                 this._rawObject.Volume = value;\r
358                         },\r
359                         \r
360                         // SilverlightAudio.startTime\r
361                         startTime : function(time){ // @param Number: time\r
362                                                                            // @return Number/void:\r
363                                 if (time === undefined) {\r
364                                         return this._startTime;\r
365                                 }\r
366                                 this._startTime = time;\r
367                         },\r
368                         \r
369                         // SilverlightAudio.currentTime\r
370                         currentTime : function(time){ // @param Number: time\r
371                                                                                  // @return Number/void:\r
372                                 var position = this._rawObject.Position; // [!] create instance\r
373                                 \r
374                                 if (time === undefined) {\r
375                                         return position.Seconds;\r
376                                 };\r
377 \r
378                                 position.Seconds = time; // set current time\r
379                         \r
380                                 this._rawObject.Position = position; // [!] reattach instance\r
381                         },\r
382                         \r
383                         // SilverlightAudio.isPlaying\r
384                         isPlaying : function(){ // @return Boolean:\r
385                                 if (!this._error && !this._paused && !this._ended) {\r
386                                         return true;\r
387                                 }\r
388                                 return false;\r
389                         }\r
390 \r
391                 }\r
392         );\r
393         \r
394 };\r