OSDN Git Service

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