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
47 _lastUserAction : '',
\r
49 _interval : 0, // setInterval timer id
\r
51 Constructor : function( proxy, source, option ){
\r
53 if( !X_Audio_SLAudio_uid ){
\r
55 // X_Node_systemNode.create( 'script', { type : 'text/xaml', id : 'silverlightaudio' } )
\r
56 // .text( '<Canvas xmlns="http://schemas.microsoft.com/client/2007" ' +
\r
57 // 'xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></Canvas>');
\r
60 // <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
63 * [Silverlight 2]JavaScriptコードからSilverlightのオブジェクトを利用するには?[C#、VB]
\r
64 * http://www.atmarkit.co.jp/fdotnet/dotnettips/902slobjcallfromjs/slobjcallfromjs.html
\r
65 * このページのサンプルは sl5+firefox32 環境で動いている。xaml を js から利用する形ではなく、.xap を sl4 以下で作るのがよさそう.
\r
70 this._source = source;
\r
71 this._onload = 'XAudioSilverlightOnLoad' + ( ++X_Audio_SLAudio_uid );
\r
72 this._callback = window[ this._onload ] = X_Callback_create( this, this.onSLReady, [ option.autoplay ] );
\r
73 this.xnodeObject = X_Node_body
\r
74 .create( 'object', {
\r
75 type : 'application/x-silverlight-2',
\r
76 data : 'data:application/x-silverlight-2,',
\r
81 '<param name="background" value="#00000000">' + // transparent
\r
82 '<param name="windowless" value="true">' +
\r
83 '<param name="source" value="#silverlightaudio">' + // XAML ID
\r
84 '<param name="onload" value="' + this._onload + '">' // + // bond to global
\r
85 //'<param value="2.0.31005.0" name="minRuntimeVersion">' +
\r
86 //'<param value="true" name="autoUpgrade">' +
\r
87 //'<param name="onerror" value="slerror">' // bond to global
\r
89 X_AudioWrapper_updateStates( this, option );
\r
91 this.listenOnce( X.Event.KILL_INSTANCE );
\r
94 onSLReady : function( sender, autoplay ){
\r
95 if( !this._onload ) return;
\r
97 window[ this._onload ] = null;
\r
98 delete this._onload;
\r
99 X_Callback_correct( this._callback );
\r
100 delete this._callback;
\r
102 sender.children.add(
\r
106 '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">' +
\r
107 '<MediaElement x:Name="media" Source="' + this._source + '" Volume="' + this.volume + '" AutoPlay="false" />' +
\r
110 this._rawObject = sender.findName('media'); // x:Name='media'
\r
112 this.listen( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] );
\r
114 autoplay && X.Timer.once( 100, this, this.play );
\r
117 handleEvent : function( e ){
\r
118 var lastState, currentState;
\r
122 case 'MediaFailed' :
\r
124 this.playing = false;
\r
125 this._ended = true;
\r
126 this._paused = false;
\r
127 this.proxy.dispatch( 'error' ); // open failed
\r
130 case 'MediaOpened' :
\r
131 // http://msdn.microsoft.com/ja-jp/library/bb979710(VS.95).aspx
\r
132 this.duration = this._rawObject.NaturalDuration.Seconds * 1000;
\r
133 // TODO 'canplaythrough'
\r
134 this.proxy.asyncDispatch( 'loadstart' );
\r
135 this.proxy.asyncDispatch( 'loadedmetadata' );
\r
136 this.proxy.asyncDispatch( 'loadeddata' );
\r
137 this.proxy.asyncDispatch( 'canplay' );
\r
138 this.proxy.asyncDispatch( 'canplaythrough' );
\r
141 case 'MediaEnded' :
\r
142 this.loop && this.playing && this.play();
\r
145 case 'CurrentStateChanged' :
\r
146 lastState = this._lastState,
\r
147 currentState = this._rawObject.CurrentState;
\r
149 // ignore consecutive events or 'Closed' == 'Error'
\r
150 if( lastState === currentState
\r
151 || ( (lastState === 'Closed' || lastState === 'Error') && ( currentState === 'Closed' || currentState === 'Error') ) ){
\r
154 this._lastState = currentState; // update last state
\r
156 switch( currentState ){
\r
159 switch( this._lastUserAction ){
\r
161 this.proxy.dispatch( 'waiting' );
\r
164 this.proxy.dispatch( 'seeking' );
\r
171 // media.play(none supported file) -> 'Error'
\r
172 // media.play(file not found) -> 'Closed'
\r
173 // media.load -> 'Error'
\r
177 this.playing = false;
\r
178 this._ended = true;
\r
179 this._paused = false;
\r
180 this.proxy.dispatch( 'error' );
\r
183 // userAction.pause() -> MediaState('Paused') -> x
\r
184 // userAction.stop() -> MediaState('Paused') -> x
\r
185 // userAction.play() + file end -> MediaState('Paused') -> uueventfire('ended')
\r
187 this.playing = false;
\r
189 switch( this._lastUserAction ){
\r
190 case 'play': // play() -> file end -> event('ended')
\r
192 this._ended = true;
\r
193 this._paused = false;
\r
194 this.proxy.dispatch( 'ended' );
\r
195 this._currentTime( this.startTime );
\r
198 this._ended = false;
\r
199 this._paused = true;
\r
202 this._ended = true;
\r
203 this._paused = false;
\r
207 // media.play -> 'Playing'
\r
210 this.playing = true;
\r
211 this._ended = false;
\r
212 this._paused = false;
\r
213 this.proxy.dispatch( 'playing' );
\r
218 this.playing = false;
\r
219 this._ended = true;
\r
220 this._paused = false;
\r
221 this._currentTime( this.startTime );
\r
226 case X.Event.KILL_INSTANCE :
\r
227 if( this._onload ){
\r
228 // window への delete に ie5 は対応しないが、そもそも ie5 は Silverlight に非対応
\r
229 delete window[ this._onload ];
\r
230 delete this._onload;
\r
231 X_Callback_correct( this._callback );
\r
233 this.xnodeObject.destroy();
\r
238 close : function(){
\r
239 this.playing && this.pause();
\r
240 this.proxy.dispatch( 'ended' );
\r
244 // SilverlightAudio.play
\r
246 var begin, end, halfway;
\r
248 // もし kill 後に autoplayTimer で呼ばれても、_closed==true なので平気
\r
249 if( this.error ) return;
\r
251 this._lastUserAction = 0 <= this.seekTime ? 'seek' : 'play';
\r
253 end = X_AudioWrapper_getEndTime( this );
\r
254 begin = X_AudioWrapper_getStartTime( this, end, true );
\r
256 console.log( '[SLAudio] play ' + begin + ' -> ' + end );
\r
258 this._rawObject.Volume = this.volume;
\r
259 this._currentTime( begin );
\r
261 if( !this.playing ){
\r
262 this._rawObject.play();
\r
263 this.proxy.dispatch( 'play' );
\r
265 this.playing = true;
\r
268 halfway = end < this.duration;
\r
269 this._timerID && X.Timer.remove( this._timerID );
\r
272 this._timerID = X.Timer.once( end - begin, this, this._onEnded );
\r
274 delete this._timerID;
\r
277 if( !this._interval ){
\r
278 this._interval = X.Timer.add( 1000, 0, this, this._onInterval );
\r
282 _onInterval : function(){
\r
283 if( !this.playing ){
\r
284 delete this._interval;
\r
285 return X_Callback_UN_LISTEN;
\r
287 this.proxy.dispatch( 'timeupdate' );
\r
290 _onEnded : function(){
\r
292 delete this._timerID;
\r
294 if( this.playing ){
\r
296 time = this._rawObject.Position.Seconds * 1000 - X_AudioWrapper_getEndTime( this ) | 0;
\r
298 console.log( '> onEnd ' + time );
\r
299 this._timerID = X.Timer.once( -time, this, this._onEnded );
\r
304 this.looped = true;
\r
306 this.proxy.dispatch( 'looped' );
\r
309 this.proxy.dispatch( 'ended' );
\r
314 // SilverlightAudio.pause
\r
315 pause : function(){
\r
317 this._lastUserAction = 'pause';
\r
318 this.playing = false;
\r
319 this._paused = true;
\r
320 this._ended = false;
\r
321 this._rawObject.pause();
\r
322 this.proxy.dispatch( 'pause' );
\r
326 // SilverlightAudio.state
\r
327 state : function( obj ){ // @return Hash: { loop, error, paused, ended, source, duration }
\r
330 if( obj === undefined ){
\r
332 startTime : this.startTime,
\r
333 endTime : this.endTime < 0 ? this.duration : this.endTime,
\r
334 loopStartTime : this.loopStartTime < 0 ? this.startTime : this.loopStartTime,
\r
335 loopEndTime : this.loopEndTime < 0 ? ( this.endTime || this.duration ) : this.loopEndTime,
\r
337 currentTime : this._rawObject.Position.Seconds * 1000,
\r
341 looped : this.looped,
\r
342 volume : this.volume,
\r
343 error : this.error,
\r
344 playing : this.playing,
\r
345 duration : this.duration // this._rawObject.NaturalDuration.Seconds;
\r
349 result = X_AudioWrapper_updateStates( this, obj );
\r
351 if( result & 2 ){ // seek
\r
355 end = X_AudioWrapper_getEndTime( this );
\r
356 halfway = end < this.duration;
\r
357 this._timerID && X.Timer.remove( this._timerID );
\r
360 this._timerID = X.Timer.once( end - this._rawObject.Position.Seconds * 1000, this, this._onEnded );
\r
362 delete this._timerID;
\r
367 this._rawObject.Volume = this.volume;
\r
372 // SilverlightAudio.currentTime
\r
373 _currentTime : function( time ){ // @param Number: time
\r
374 var position = this._rawObject.Position; // [!] create instance
\r
376 position.Seconds = time / 1000 | 0; // set current time
\r
378 this._rawObject.Position = position; // [!] reattach instance
\r
384 function slerror(){
\r
385 alert( 'slerror' );
\r
388 X_Audio_BACKENDS.push( {
\r
389 backendName : 'Silverlight Audio',
\r
391 detect : function( proxy, source, ext ){
\r
392 var ok = ext === 'mp3' || ext === 'wma' || ext === 'wav';
\r
393 proxy.asyncDispatch( ok ? 'support' : 'nosupport' );
\r
396 klass : X_Audio_SLAudioWrapper
\r