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
6 * Silverlight 4 → 5における不具合の状況
\r
7 * http://www.slideshare.net/wakabayashiy/silverlight-4-5
\r
9 * IE10以降でSilverlightでF5押したらフリーズする不具合と対処
\r
10 * http://katsuyuzu.hatenablog.jp/entry/2014/01/11/003550
\r
12 * SilverlLight5 ie6&7(ietester,winxp), ie8(winxp) で動作確認。firefox32 では動作しない。(4以下の方がよい?)
\r
15 var X_Audio_SLAudio, X_Audio_SLAudioWrapper,
\r
16 X_Audio_SLAudio_uid = 0,
\r
17 X_Audio_SLAudio_LIVE_LIST = [],
\r
18 X_Audio_SLAudio_POOL_LIST = [];
\r
20 if( X.Pulgin.SilverlightEnabled ){
\r
22 function getSLAudioWrapper( proxy ){
\r
23 var i = X_Audio_SLAudio_LIVE_LIST.length;
\r
25 if( X_Audio_SLAudio_LIVE_LIST[ --i ].proxy === proxy ) return X_Audio_SLAudio_LIVE_LIST[ i ];
\r
31 backendName : 'Silverlight Audio',
\r
33 detect : function( proxy, source, ext ){
\r
34 var ok = ext === 'mp3' || ext === 'wma';
\r
35 proxy.asyncDispatch( ok ? 'support' : 'nosupport' );
\r
38 register : function( proxy, source, option ){
\r
39 X_Audio_SLAudio_LIVE_LIST.push( new X_Audio_SLAudioWrapper( proxy, source, option ) );
\r
42 close : function( proxy ){
\r
43 return getSLAudioWrapper( proxy ).close();
\r
46 play : function( proxy ){
\r
47 return getSLAudioWrapper( proxy ).play();
\r
50 pause : function( proxy ){
\r
51 return getSLAudioWrapper( proxy ).pause();
\r
54 state : function( proxy, obj ){
\r
55 return getSLAudioWrapper( proxy ).state( obj );
\r
59 X_Audio_BACKENDS.push( X_Audio_SLAudio );
\r
65 X_Audio_SLAudioWrapper = X.EventDispatcher.inherits(
\r
66 'X.AV.SilverlightAudioWrapper',
\r
67 X.Class.POOL_OBJECT,
\r
69 _isSilverlight : true, // for X.EventDispatcher.listen
\r
89 _lastUserAction : '',
\r
91 _interval : 0, // setInterval timer id
\r
93 Constructor : function( proxy, source, option ){
\r
95 if( !X_Audio_SLAudio_uid ){
\r
97 //X.X_Node_systemNode.create( 'script', { type : 'text/xaml', id : 'silverlightaudio' } )
\r
98 // .text( '<Canvas xmlns="http://schemas.microsoft.com/client/2007" ' +
\r
99 // 'xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></Canvas>');
\r
102 // <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
105 * [Silverlight 2]JavaScriptコードからSilverlightのオブジェクトを利用するには?[C#、VB]
\r
106 * http://www.atmarkit.co.jp/fdotnet/dotnettips/902slobjcallfromjs/slobjcallfromjs.html
\r
107 * このページのサンプルは sl5+firefox32 環境で動いている。xaml を js から利用する形ではなく、.xap を sl4 以下で作るのがよさそう.
\r
111 this.proxy = proxy;
\r
112 this._source = source;
\r
113 this._onload = 'XAudioSilverlightOnLoad' + ( ++X_Audio_SLAudio_uid );
\r
114 this._callback = window[ this._onload ] = X_Callback_create( this, this.onSLReady, [ option.autoplay ] );
\r
115 this.xnodeObject = X_Node_body
\r
116 .create( 'object', {
\r
117 type : 'application/x-silverlight-2',
\r
118 data : 'data:application/x-silverlight-2,',
\r
123 '<param name="background" value="#00000000">' + // transparent
\r
124 '<param name="windowless" value="true">' +
\r
125 '<param name="source" value="#silverlightaudio">' + // XAML ID
\r
126 '<param name="onload" value="' + this._onload + '">' // + // bond to global
\r
127 //'<param value="2.0.31005.0" name="minRuntimeVersion">' +
\r
128 //'<param value="true" name="autoUpgrade">' +
\r
129 //'<param name="onerror" value="slerror">' // bond to global
\r
131 X_AudioWrapper_updateStates( this, option );
\r
133 this.listenOnce( X.Event.KILL_INSTANCE );
\r
136 onSLReady : function( sender, autoplay ){
\r
137 if( !this._onload ) return;
\r
139 window[ this._onload ] = null;
\r
140 delete this._onload;
\r
141 X_Callback_correct( this._callback );
\r
142 delete this._callback;
\r
144 sender.children.add(
\r
148 '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">' +
\r
149 '<MediaElement x:Name="media" Source="' + this._source + '" Volume="' + this.volume + '" AutoPlay="false" />' +
\r
152 this._rawObject = sender.findName('media'); // x:Name='media'
\r
154 this.listen( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] );
\r
156 autoplay && X.Timer.once( 100, this, this.play );
\r
159 handleEvent : function( e ){
\r
160 var lastState, currentState;
\r
164 case 'MediaFailed' :
\r
166 this.playing = false;
\r
167 this._ended = true;
\r
168 this._paused = false;
\r
169 this.proxy.dispatch( 'error' ); // open failed
\r
172 case 'MediaOpened' :
\r
173 // http://msdn.microsoft.com/ja-jp/library/bb979710(VS.95).aspx
\r
174 this.duration = this._rawObject.NaturalDuration.Seconds * 1000;
\r
175 this.endTime = this.endTime || this.duration;
\r
176 // TODO 'canplaythrough'
\r
177 this.proxy.asyncDispatch( 'loadstart' );
\r
178 this.proxy.asyncDispatch( 'loadedmetadata' );
\r
179 this.proxy.asyncDispatch( 'loadeddata' );
\r
180 this.proxy.asyncDispatch( 'canplay' );
\r
181 this.proxy.asyncDispatch( 'canplaythrough' );
\r
184 case 'MediaEnded' :
\r
185 this.loop && this.playing && this.play();
\r
188 case 'CurrentStateChanged' :
\r
189 lastState = this._lastState,
\r
190 currentState = this._rawObject.CurrentState;
\r
192 // ignore consecutive events or 'Closed' == 'Error'
\r
193 if( lastState === currentState
\r
194 || ( (lastState === 'Closed' || lastState === 'Error') && ( currentState === 'Closed' || currentState === 'Error') ) ){
\r
197 this._lastState = currentState; // update last state
\r
199 switch( currentState ){
\r
202 switch( this._lastUserAction ){
\r
204 this.proxy.dispatch( 'waiting' );
\r
207 this.proxy.dispatch( 'seeking' );
\r
214 // media.play(none supported file) -> 'Error'
\r
215 // media.play(file not found) -> 'Closed'
\r
216 // media.load -> 'Error'
\r
220 this.playing = false;
\r
221 this._ended = true;
\r
222 this._paused = false;
\r
223 this.proxy.dispatch( 'error' );
\r
226 // userAction.pause() -> MediaState('Paused') -> x
\r
227 // userAction.stop() -> MediaState('Paused') -> x
\r
228 // userAction.play() + file end -> MediaState('Paused') -> uueventfire('ended')
\r
230 this.playing = false;
\r
232 switch( this._lastUserAction ){
\r
233 case 'play': // play() -> file end -> event('ended')
\r
235 this._ended = true;
\r
236 this._paused = false;
\r
237 this.proxy.dispatch( 'ended' );
\r
238 this._currentTime( this.startTime );
\r
241 this._ended = false;
\r
242 this._paused = true;
\r
245 this._ended = true;
\r
246 this._paused = false;
\r
250 // media.play -> 'Playing'
\r
253 this.playing = true;
\r
254 this._ended = false;
\r
255 this._paused = false;
\r
256 this.proxy.dispatch( 'playing' );
\r
261 this.playing = false;
\r
262 this._ended = true;
\r
263 this._paused = false;
\r
264 this._currentTime( this.startTime );
\r
269 case X.Event.KILL_INSTANCE :
\r
270 if( this._onload ){
\r
271 // window への delete に ie5 は対応しないが、そもそも ie5 は Silverlight に非対応
\r
272 delete window[ this._onload ];
\r
273 delete this._onload;
\r
274 X_Callback_correct( this._callback );
\r
276 this.xnodeObject.destroy();
\r
281 close : function(){
\r
282 this.playing && this.pause();
\r
283 this.proxy.dispatch( 'ended' );
\r
287 // SilverlightAudio.play
\r
288 play : function( seekTime ){
\r
289 var begin, halfway;
\r
291 // もし kill 後に autoplayTimer で呼ばれても、_closed==true なので平気
\r
292 if( this.error ) return;
\r
294 begin = ( seekTime || seekTime === 0 ) ? seekTime : this.playing ? this.loopStartTime : this.startTime;
\r
295 this._lastUserAction = this.playing ? 'seek' : 'play';
\r
297 this._currentTime( begin );
\r
298 this._rawObject.Volume = this.volume;
\r
300 if( !this.playing ){
\r
301 this._rawObject.play();
\r
302 this.proxy.dispatch( 'play' );
\r
304 this.playing = true;
\r
307 halfway = this.endTime < this.duration;
\r
308 this._timerID && X.Timer.remove( this._timerID );
\r
311 this._timerID = X.Timer.once( this.endTime - begin, this, this._onEnded );
\r
313 delete this._timerID;
\r
316 if( !this._interval ){
\r
317 this._interval = X.Timer.add( 1000, 0, this, this._onInterval );
\r
321 _onInterval : function(){
\r
322 if( !this.playing ){
\r
323 delete this._interval;
\r
324 return X_Callback_UN_LISTEN;
\r
326 this.proxy.dispatch( 'timeupdate' );
\r
329 _onEnded : function(){
\r
330 delete this._timerID;
\r
331 if( this.playing ){
\r
336 this.dispatch( 'ended' );
\r
341 // SilverlightAudio.pause
\r
342 pause : function(){
\r
344 this._lastUserAction = 'pause';
\r
345 this.playing = false;
\r
346 this._paused = true;
\r
347 this._ended = false;
\r
348 this._rawObject.pause();
\r
349 this.proxy.dispatch( 'pause' );
\r
353 // SilverlightAudio.state
\r
354 state : function( obj ){ // @return Hash: { loop, error, paused, ended, source, duration }
\r
357 if( obj === undefined ){
\r
359 startTime : this.startTime,
\r
360 endTime : this.endTime,
\r
361 loopStartTime : this.loopStartTime,
\r
362 currentTime : this._rawObject.Position.Seconds * 1000,
\r
364 volume : this.volume,
\r
365 error : this.error,
\r
366 playing : this.playing,
\r
367 duration : this.duration || 0 // this._rawObject.NaturalDuration.Seconds;
\r
371 result = X_AudioWrapper_updateStates( this, obj );
\r
373 if( result & 2 ){ // seek
\r
374 this.play( this.seekTime );
\r
375 delete this.seekTime;
\r
378 halfway = this.endTime < this.duration;
\r
379 this._timerID && X.Timer.remove( this._timerID );
\r
382 this._timerID = X.Timer.once( this.endTime - this._rawObject.Position.Seconds * 1000, this, this._onEnded );
\r
384 delete this._timerID;
\r
389 this._rawObject.Volume = this.volume;
\r
394 // SilverlightAudio.currentTime
\r
395 _currentTime : function( time ){ // @param Number: time
\r
396 var position = this._rawObject.Position; // [!] create instance
\r
398 position.Seconds = time / 1000; // set current time
\r
400 this._rawObject.Position = position; // [!] reattach instance
\r