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_SLAudioWrapper,
\r
16 X_Audio_SLAudio_uid = 0;
\r
18 if( X.Pulgin.SilverlightEnabled ){
\r
20 // TODO X.Node.inherits
\r
21 X_Audio_SLAudioWrapper = X.EventDispatcher.inherits(
\r
22 'X.AV.SilverlightAudioWrapper',
\r
23 X.Class.POOL_OBJECT,
\r
25 _isSilverlight : true, // for X.EventDispatcher.listen
\r
48 _lastUserAction : '',
\r
50 _interval : 0, // setInterval timer id
\r
52 Constructor : function( proxy, source, option ){
\r
54 if( !X_Audio_SLAudio_uid ){
\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
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
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
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
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
90 X_AudioWrapper_updateStates( this, option );
\r
92 this.listenOnce( X.Event.KILL_INSTANCE );
\r
95 onSLReady : function( sender ){
\r
96 if( !this._onload ) return;
\r
98 window[ this._onload ] = null;
\r
99 delete this._onload;
\r
100 X_Callback_correct( this._callback );
\r
101 delete this._callback;
\r
103 sender.children.add(
\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
111 this._rawObject = sender.findName('media'); // x:Name='media'
\r
113 this.listen( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] );
\r
116 handleEvent : function( e ){
\r
117 var lastState, currentState;
\r
121 case 'MediaFailed' :
\r
123 this.playing = false;
\r
124 this._ended = true;
\r
125 this._paused = false;
\r
126 this.proxy.dispatch( 'error' ); // open failed
\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
139 this.autoplay && X.Timer.once( 16, this, this.play );
\r
142 case 'MediaEnded' :
\r
143 this.loop && this.playing && this.play();
\r
146 case 'CurrentStateChanged' :
\r
147 lastState = this._lastState,
\r
148 currentState = this._rawObject.CurrentState;
\r
150 // ignore consecutive events or 'Closed' == 'Error'
\r
151 if( lastState === currentState
\r
152 || ( (lastState === 'Closed' || lastState === 'Error') && ( currentState === 'Closed' || currentState === 'Error') ) ){
\r
155 this._lastState = currentState; // update last state
\r
157 switch( currentState ){
\r
160 switch( this._lastUserAction ){
\r
162 this.proxy.dispatch( 'waiting' );
\r
165 this.proxy.dispatch( 'seeking' );
\r
172 // media.play(none supported file) -> 'Error'
\r
173 // media.play(file not found) -> 'Closed'
\r
174 // media.load -> 'Error'
\r
178 this.playing = false;
\r
179 this._ended = true;
\r
180 this._paused = false;
\r
181 this.proxy.dispatch( 'error' );
\r
184 // userAction.pause() -> MediaState('Paused') -> x
\r
185 // userAction.stop() -> MediaState('Paused') -> x
\r
186 // userAction.play() + file end -> MediaState('Paused') -> uueventfire('ended')
\r
188 this.playing = false;
\r
190 switch( this._lastUserAction ){
\r
191 case 'play': // play() -> file end -> event('ended')
\r
193 this._ended = true;
\r
194 this._paused = false;
\r
195 this.proxy.dispatch( 'ended' );
\r
196 this._currentTime( this.startTime );
\r
199 this._ended = false;
\r
200 this._paused = true;
\r
203 this._ended = true;
\r
204 this._paused = false;
\r
208 // media.play -> 'Playing'
\r
211 this.playing = true;
\r
212 this._ended = false;
\r
213 this._paused = false;
\r
214 this.proxy.dispatch( 'playing' );
\r
219 this.playing = false;
\r
220 this._ended = true;
\r
221 this._paused = false;
\r
222 this._currentTime( this.startTime );
\r
227 case X.Event.KILL_INSTANCE :
\r
228 if( this._onload ){
\r
229 // window への delete に ie5 は対応しないが、そもそも ie5 は Silverlight に非対応
\r
230 delete window[ this._onload ];
\r
231 delete this._onload;
\r
232 X_Callback_correct( this._callback );
\r
234 this.xnodeObject.destroy();
\r
239 close : function(){
\r
240 this.playing && this.pause();
\r
241 this.proxy.dispatch( 'ended' );
\r
245 // SilverlightAudio.play
\r
247 var begin, end, halfway;
\r
249 // もし kill 後に autoplayTimer で呼ばれても、_closed==true なので平気
\r
250 if( this.error ) return;
\r
251 if( !this.duration ){
\r
252 this.autoplay = true;
\r
256 this._lastUserAction = 0 <= this.seekTime ? 'seek' : 'play';
\r
258 end = X_AudioWrapper_getEndTime( this );
\r
259 begin = X_AudioWrapper_getStartTime( this, end, true );
\r
261 console.log( '[SLAudio] play ' + begin + ' -> ' + end );
\r
263 this._rawObject.Volume = this.volume;
\r
264 this._currentTime( begin );
\r
266 if( !this.playing ){
\r
267 this._rawObject.play();
\r
268 this.proxy.dispatch( 'play' );
\r
270 this.playing = true;
\r
273 halfway = end < this.duration;
\r
274 this._timerID && X.Timer.remove( this._timerID );
\r
277 this._timerID = X.Timer.once( end - begin, this, this._onEnded );
\r
279 delete this._timerID;
\r
282 if( !this._interval ){
\r
283 this._interval = X.Timer.add( 1000, 0, this, this._onInterval );
\r
287 _onInterval : function(){
\r
288 if( !this.playing ){
\r
289 delete this._interval;
\r
290 return X_Callback_UN_LISTEN;
\r
292 this.proxy.dispatch( 'timeupdate' );
\r
295 _onEnded : function(){
\r
297 delete this._timerID;
\r
299 if( this.playing ){
\r
301 time = this._rawObject.Position.Seconds * 1000 - X_AudioWrapper_getEndTime( this ) | 0;
\r
303 console.log( '> onEnd ' + time );
\r
304 this._timerID = X.Timer.once( -time, this, this._onEnded );
\r
309 this.looped = true;
\r
311 this.proxy.dispatch( 'looped' );
\r
314 this.proxy.dispatch( 'ended' );
\r
319 // SilverlightAudio.pause
\r
320 pause : function(){
\r
322 this._lastUserAction = 'pause';
\r
323 this.playing = false;
\r
324 this._paused = true;
\r
325 this._ended = false;
\r
326 this._rawObject.pause();
\r
327 this.proxy.dispatch( 'pause' );
\r
331 // SilverlightAudio.state
\r
332 state : function( obj ){ // @return Hash: { loop, error, paused, ended, source, duration }
\r
335 if( obj === undefined ){
\r
337 startTime : this.startTime,
\r
338 endTime : this.endTime < 0 ? this.duration : this.endTime,
\r
339 loopStartTime : this.loopStartTime < 0 ? this.startTime : this.loopStartTime,
\r
340 loopEndTime : this.loopEndTime < 0 ? ( this.endTime || this.duration ) : this.loopEndTime,
\r
342 currentTime : this._rawObject.Position.Seconds * 1000,
\r
346 looped : this.looped,
\r
347 volume : this.volume,
\r
348 error : this.error,
\r
349 playing : this.playing,
\r
350 duration : this.duration // this._rawObject.NaturalDuration.Seconds;
\r
354 result = X_AudioWrapper_updateStates( this, obj );
\r
356 if( result & 2 ){ // seek
\r
360 end = X_AudioWrapper_getEndTime( this );
\r
361 halfway = end < this.duration;
\r
362 this._timerID && X.Timer.remove( this._timerID );
\r
365 this._timerID = X.Timer.once( end - this._rawObject.Position.Seconds * 1000, this, this._onEnded );
\r
367 delete this._timerID;
\r
372 this._rawObject.Volume = this.volume;
\r
377 // SilverlightAudio.currentTime
\r
378 _currentTime : function( time ){ // @param Number: time
\r
379 var position = this._rawObject.Position; // [!] create instance
\r
381 position.Seconds = time / 1000 | 0; // set current time
\r
383 this._rawObject.Position = position; // [!] reattach instance
\r
389 function slerror(){
\r
390 alert( 'slerror' );
\r
393 X_Audio_BACKENDS.push( {
\r
394 backendName : 'Silverlight Audio',
\r
396 detect : function( proxy, source, ext ){
\r
397 var ok = ext === 'mp3' || ext === 'wma' || ext === 'wav';
\r
398 proxy.asyncDispatch( ok ? 'support' : 'nosupport' );
\r
401 klass : X_Audio_SLAudioWrapper
\r