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 // X.Node.inherits はできない。_rawObject は <object> でなく silverlight
\r
21 X_Audio_SLAudioWrapper = X_Audio_AbstractAudioBackend[ 'inherits' ](
\r
22 'X.AV.SilverlightAudioWrapper',
\r
23 X_Class.POOL_OBJECT,
\r
25 '_rawType' : X_EventDispatcher_EVENT_TARGET_SILVER_LIGHT,
\r
33 _lastUserAction : '',
\r
35 _interval : 0, // setInterval timer id
\r
37 Constructor : function( target, source, option ){
\r
40 if( !X_Audio_SLAudio_uid ){
\r
42 //xnodeScript = X_Node_head[ 'create' ]( 'script', { type : 'text/xaml', id : 'silverlightaudio' } );
\r
43 //xnodeScript[ '_rawObject' ].innerHTML = '<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></Canvas>';
\r
46 // <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
49 * [Silverlight 2]JavaScriptコードからSilverlightのオブジェクトを利用するには?[C#、VB]
\r
50 * http://www.atmarkit.co.jp/fdotnet/dotnettips/902slobjcallfromjs/slobjcallfromjs.html
\r
51 * このページのサンプルは sl5+firefox32 環境で動いている。xaml を js から利用する形ではなく、.xap を sl4 以下で作るのがよさそう.
\r
55 this.target = target || this;
\r
56 this._source = source;
\r
57 // X.Audio._slOnload_ は不可
\r
58 this._onload = 'XAudioSilverlightOnLoad' + ( ++X_Audio_SLAudio_uid );
\r
59 this._callback = window[ this._onload ] = X_Callback_create( this, this.onSLReady );
\r
60 this.xnodeObject = X_Node_body
\r
61 [ 'create' ]( 'object', {
\r
62 type : 'application/x-silverlight-2',
\r
63 data : 'data:application/x-silverlight-2,',
\r
68 '<param name="background" value="#00000000">' + // transparent
\r
69 '<param name="windowless" value="true">' +
\r
70 '<param name="source" value="#silverlightaudio">' + // XAML ID
\r
71 '<param name="onload" value="' + this._onload + '">' // + // bond to global
\r
72 //'<param value="2.0.31005.0" name="minRuntimeVersion">' +
\r
73 //'<param value="true" name="autoUpgrade">' +
\r
74 //'<param name="onerror" value="slerror">' // bond to global
\r
76 this.setState( option );
\r
78 this[ 'listenOnce' ]( X_EVENT_KILL_INSTANCE );
\r
81 onSLReady : function( sender ){
\r
82 if( !this._onload ) return;
\r
84 window[ this._onload ] = null;
\r
85 delete this._onload;
\r
86 X_Callback_correct( this._callback );
\r
87 delete this._callback;
\r
89 sender[ 'children' ][ 'add' ](
\r
90 sender[ 'GetHost' ]()[ 'content' ][ 'CreateFromXaml' ](
\r
91 '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">' +
\r
92 '<MediaElement x:Name="media" Source="' + this._source + '" Volume="' + this.gain + '" AutoPlay="false" />' +
\r
95 this[ '_rawObject' ] = sender[ 'findName' ]( 'media' ); // x:Name='media'
\r
97 this[ 'listen' ]( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] );
\r
100 handleEvent : function( e ){
\r
101 var lastState, currentState;
\r
103 console.log( e.type );
\r
106 case 'MediaFailed' :
\r
108 this.playing = false;
\r
109 this._ended = true;
\r
110 this._paused = false;
\r
111 if( this.playing ){
\r
112 //X_Timer_once( 16, this, this.actualPlay );
\r
114 this.target[ 'dispatch' ]( X_EVENT_ERROR ); // open failed
\r
119 case 'MediaOpened' :
\r
120 // http://msdn.microsoft.com/ja-jp/library/bb979710(VS.95).aspx
\r
121 this.duration = this[ '_rawObject' ][ 'NaturalDuration' ][ 'Seconds' ] * 1000;
\r
122 this.target[ 'asyncDispatch' ]( X_EVENT_READY );
\r
124 this.autoplay && X_Timer_once( 16, this, this.actualPlay );
\r
127 case 'MediaEnded' :
\r
128 //console.log( ' > ' + this.autoLoop + ' error:' + this.error );
\r
129 //this.autoLoop && /* this.playing && */ this.actualPlay();
\r
130 this._ended = true;
\r
133 case 'CurrentStateChanged' :
\r
134 lastState = this._lastState,
\r
135 currentState = this[ '_rawObject' ][ 'CurrentState' ];
\r
137 // ignore consecutive events or 'Closed' == 'Error'
\r
138 if( lastState === currentState
\r
139 || ( (lastState === 'Closed' || lastState === 'Error') && ( currentState === 'Closed' || currentState === 'Error') ) ){
\r
142 this._lastState = currentState; // update last state
\r
144 console.log( ' > ' + currentState + ' - ' + this._lastUserAction );
\r
146 switch( currentState ){
\r
149 switch( this._lastUserAction ){
\r
151 this.target[ 'dispatch' ]( X_EVENT_MEDIA_WAITING );
\r
154 this.target[ 'dispatch' ]( X_EVENT_MEDIA_SEEKING );
\r
161 // media.play(none supported file) -> 'Error'
\r
162 // media.play(file not found) -> 'Closed'
\r
163 // media.load -> 'Error'
\r
167 this.error = this.error || 2;
\r
168 this.playing = false;
\r
169 this._ended = true;
\r
170 this._paused = false;
\r
171 this.target[ 'dispatch' ]( X_EVENT_ERROR );
\r
175 // userAction.pause() -> MediaState('Paused') -> x
\r
176 // userAction.stop() -> MediaState('Paused') -> x
\r
177 // userAction.play() + file end -> MediaState('Paused') -> uueventfire('ended')
\r
180 this.playing && X_Timer_once( 16, this, this.actualPlay );
\r
181 //this.playing = false;
\r
183 switch( this._lastUserAction ){
\r
184 case 'play': // play() -> file end -> event('ended')
\r
186 //this.seekTime = 0;
\r
187 this._ended = true;
\r
188 this._paused = false;
\r
189 //this.target[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );
\r
190 //this.setCurrentTime( this.startTime );
\r
193 this._ended = false;
\r
194 this._paused = true;
\r
197 this._ended = true;
\r
198 this._paused = false;
\r
202 // media.play -> 'Playing'
\r
205 //this.playing = true;
\r
206 this._ended = false;
\r
207 this._paused = false;
\r
208 this.target[ 'dispatch' ]( X_EVENT_MEDIA_PLAYING );
\r
213 this.playing && X_Timer_once( 16, this, this.actualPlay );
\r
216 //this.playing = false;
\r
217 this._ended = true;
\r
218 this._paused = false;
\r
219 //this.setCurrentTime( this.startTime );
\r
224 case X_EVENT_KILL_INSTANCE :
\r
225 this.playing && this.target[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );
\r
226 this.playing && this.actualPause();
\r
228 if( this._onload ){
\r
229 // window への delete に ie5 は対応しないが、そもそも ie5 は Silverlight に非対応
\r
230 window[ this._onload ] = null;
\r
231 delete this._onload;
\r
232 X_Callback_correct( this._callback );
\r
234 this.xnodeObject[ 'kill' ]();
\r
239 // SilverlightAudio.play
\r
240 actualPlay : function(){
\r
241 var begin, offset, end;
\r
243 // もし kill 後に autoplayTimer で呼ばれても、_closed==true なので平気
\r
244 if( this.error ) return;
\r
245 if( !this.duration ){
\r
246 this.autoplay = true;
\r
250 this._lastUserAction = 0 <= this.seekTime ? 'seek' : 'play';
\r
252 end = X_AudioWrapper_getEndTime( this );
\r
253 begin = X_AudioWrapper_getStartTime( this, end, true ) | 0;
\r
255 // 1 秒以下は指定できないため四捨五入
\r
256 begin = ( begin / 1000 | 0 ) * 1000 + ( 500 < begin % 1000 ? 1000 : 0 );
\r
258 this[ '_rawObject' ][ 'Volume' ] = this.gain;
\r
260 this.setCurrentTime( this._beginTime = begin );
\r
262 console.log( '[play] ' + begin + ' -> ' + end );
\r
265 if( offset = begin - this.getActualCurrentTime() ){
\r
266 this.setCurrentTime( begin + offset );
\r
267 console.log( ' [差補正] ' + offset + ' ct:' + this.getActualCurrentTime() + ' begin:' + begin );
\r
268 this._beginTime = begin = this.getActualCurrentTime();
\r
271 if( !this.playing || this._ended ){
\r
272 console.log( '[play] play()' + begin + ' -> ' + end );
\r
273 this[ '_rawObject' ].play();
\r
274 this.playing = true;
\r
275 this._ended = false;
\r
278 this._timerID && X_Timer_remove( this._timerID );
\r
280 this._timerID = X_Timer_once( end - begin, this, this._onEnded );
\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.target[ 'dispatch' ]( X_EVENT_MEDIA_PLAYING );
\r
295 _onEnded : function(){
\r
297 delete this._timerID;
\r
299 if( this.playing ){
\r
300 //console.log( '> end ' + X_AudioWrapper_getEndTime( this ) + ' current:' + ( this.getActualCurrentTime() ) );
\r
301 time = this.getActualCurrentTime();
\r
303 if( time < this._beginTime ){
\r
304 console.log( '== waiting ' + time + ' < begin:' + this._beginTime );
\r
305 this.setCurrentTime( this._beginTime );
\r
306 time = this.getActualCurrentTime();
\r
307 console.log( ' > ' + time );
\r
308 this._ended && this[ '_rawObject' ].play();
\r
309 this._ended = false;
\r
310 this.target[ 'dispatch' ]( X_EVENT_MEDIA_WAITING );
\r
311 this._timerID = X_Timer_once( X_AudioWrapper_getEndTime( this ) - time, this, this._onEnded );
\r
315 time -= X_AudioWrapper_getEndTime( this );
\r
317 console.log( ' > まだ終わらない ' + time );
\r
318 this._ended && this[ '_rawObject' ].play();
\r
319 this._ended = false;
\r
320 this._timerID = X_Timer_once( -time, this, this._onEnded );
\r
324 if( this.autoLoop ){
\r
325 console.log( '========= loop?' );
\r
326 if( !( this.target[ 'dispatch' ]( X_EVENT_MEDIA_BEFORE_LOOP ) & X_Callback_PREVENT_DEFAULT ) ){
\r
327 console.log( '========== loopした' );
\r
328 this.looped = true;
\r
329 this.target[ 'dispatch' ]( X_EVENT_MEDIA_LOOPED );
\r
333 console.log( '========= pause' );
\r
334 this.actualPause();
\r
335 this.target[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );
\r
340 // SilverlightAudio.pause
\r
341 actualPause : function(){
\r
342 if( this.error || !this.playing ) return;
\r
344 this._lastUserAction = 'pause';
\r
345 this.seekTime = this.getActualCurrentTime();
\r
346 this.playing = false;
\r
347 this._paused = true;
\r
348 this._ended = false;
\r
350 this[ '_rawObject' ].pause();
\r
351 //this.target[ 'dispatch' ]( 'pause' );
\r
354 getActualCurrentTime : function(){
\r
355 return this[ '_rawObject' ][ 'Position' ][ 'Seconds' ] * 1000 | 0;
\r
358 afterUpdateState : function( result ){
\r
359 if( result & 3 ){ // seek
\r
363 end = X_AudioWrapper_getEndTime( this );
\r
364 halfway = end < this.duration;
\r
365 this._timerID && X_Timer_remove( this._timerID );
\r
368 this._timerID = X_Timer_once( end - this.getActualCurrentTime(), this, this._onEnded );
\r
370 delete this._timerID;
\r
374 this[ '_rawObject' ][ 'Volume' ] = this.gain;
\r
378 // SilverlightAudio.currentTime
\r
379 setCurrentTime : function( time ){ // @param Number: time
\r
380 var position = this[ '_rawObject' ][ 'Position' ]; // [!] create instance
\r
382 position[ 'Seconds' ] = time / 1000 | 0; // set current time
\r
384 this[ '_rawObject' ][ 'Position' ] = position; // [!] reattach instance
\r
390 function slerror(){
\r
391 alert( 'slerror' );
\r
394 X_Audio_BACKENDS.push( {
\r
395 backendName : 'Silverlight Audio',
\r
397 detect : function( proxy, source, ext ){
\r
398 var ok = ext === 'mp3' || ext === 'wma' || ext === 'wav';
\r
399 proxy[ 'asyncDispatch' ]( { type : X_EVENT_COMPLETE, canPlay : ok } );
\r
402 klass : X_Audio_SLAudioWrapper
\r