X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=0.6.x%2Fjs%2F07_audio%2F03_XSilverlightAudio.js;h=d169d888bd62dfd339356ae302ab8ef656eff2a4;hb=c50ad9ff1e8d6e9be2089ba554813564bd24a7c8;hp=c02ad7f8ca3b48aea37c1b9bcba5a4c0be39fdd6;hpb=94c39d10a21853703c90cb09b1e82bd7a2d8923e;p=pettanr%2FclientJs.git diff --git a/0.6.x/js/07_audio/03_XSilverlightAudio.js b/0.6.x/js/07_audio/03_XSilverlightAudio.js index c02ad7f..d169d88 100644 --- a/0.6.x/js/07_audio/03_XSilverlightAudio.js +++ b/0.6.x/js/07_audio/03_XSilverlightAudio.js @@ -12,56 +12,12 @@ * SilverlLight5 ie6&7(ietester,winxp), ie8(winxp) で動作確認。firefox32 では動作しない。(4以下の方がよい?) */ -var X_Audio_SLAudio, X_Audio_SLAudioWrapper, - X_Audio_SLAudio_uid = 0, - X_Audio_SLAudio_LIVE_LIST = [], - X_Audio_SLAudio_POOL_LIST = []; +var X_Audio_SLAudioWrapper, + X_Audio_SLAudio_uid = 0; if( X.Pulgin.SilverlightEnabled ){ - function getSLAudioWrapper( proxy ){ - var i = X_Audio_SLAudio_LIVE_LIST.length; - for( ; i; ){ - if( X_Audio_SLAudio_LIVE_LIST[ --i ].proxy === proxy ) return X_Audio_SLAudio_LIVE_LIST[ i ]; - }; - }; - - X_Audio_SLAudio = - { - backendName : 'Silverlight Audio', - - detect : function( proxy, source, ext ){ - var ok = ext === 'mp3' || ext === 'wma'; - proxy.asyncDispatch( ok ? 'support' : 'nosupport' ); - }, - - register : function( proxy, source, option ){ - X_Audio_SLAudio_LIVE_LIST.push( new X_Audio_SLAudioWrapper( proxy, source, option ) ); - }, - - close : function( proxy ){ - return getSLAudioWrapper( proxy ).close(); - }, - - play : function( proxy ){ - return getSLAudioWrapper( proxy ).play(); - }, - - pause : function( proxy ){ - return getSLAudioWrapper( proxy ).pause(); - }, - - state : function( proxy, obj ){ - return getSLAudioWrapper( proxy ).state( obj ); - } - }; - - X_Audio_BACKENDS.push( X_Audio_SLAudio ); - - function slerror(){ - alert( 'slerror' ); - }; - + // TODO X.Node.inherits X_Audio_SLAudioWrapper = X.EventDispatcher.inherits( 'X.AV.SilverlightAudioWrapper', X.Class.POOL_OBJECT, @@ -70,14 +26,17 @@ if( X.Pulgin.SilverlightEnabled ){ proxy : null, startTime : 0, - endTime : 0, - loopStartTime : 0, - seekTime : 0, + endTime : -1, + loopStartTime : -1, + loopEndTime : -1, + seekTime : -1, duration : 0, playing : false, error : 0, loop : false, + looped : false, + autoplay : false, volume : 0.5, _onload : '', @@ -111,7 +70,7 @@ if( X.Pulgin.SilverlightEnabled ){ this.proxy = proxy; this._source = source; this._onload = 'XAudioSilverlightOnLoad' + ( ++X_Audio_SLAudio_uid ); - this._callback = window[ this._onload ] = X_Callback_create( this, this.onSLReady, [ option.autoplay ] ); + this._callback = window[ this._onload ] = X_Callback_create( this, this.onSLReady ); this.xnodeObject = X_Node_body .create( 'object', { type : 'application/x-silverlight-2', @@ -133,7 +92,7 @@ if( X.Pulgin.SilverlightEnabled ){ this.listenOnce( X.Event.KILL_INSTANCE ); }, - onSLReady : function( sender, autoplay ){ + onSLReady : function( sender ){ if( !this._onload ) return; window[ this._onload ] = null; @@ -152,8 +111,6 @@ if( X.Pulgin.SilverlightEnabled ){ this._rawObject = sender.findName('media'); // x:Name='media' this.listen( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] ); - - autoplay && X.Timer.once( 100, this, this.play ); }, handleEvent : function( e ){ @@ -172,13 +129,14 @@ if( X.Pulgin.SilverlightEnabled ){ case 'MediaOpened' : // http://msdn.microsoft.com/ja-jp/library/bb979710(VS.95).aspx this.duration = this._rawObject.NaturalDuration.Seconds * 1000; - this.endTime = this.endTime || this.duration; // TODO 'canplaythrough' this.proxy.asyncDispatch( 'loadstart' ); this.proxy.asyncDispatch( 'loadedmetadata' ); this.proxy.asyncDispatch( 'loadeddata' ); this.proxy.asyncDispatch( 'canplay' ); this.proxy.asyncDispatch( 'canplaythrough' ); + + this.autoplay && X.Timer.once( 16, this, this.play ); break; case 'MediaEnded' : @@ -285,17 +243,25 @@ if( X.Pulgin.SilverlightEnabled ){ }, // SilverlightAudio.play - play : function( seekTime ){ - var begin, halfway; + play : function(){ + var begin, end, halfway; // もし kill 後に autoplayTimer で呼ばれても、_closed==true なので平気 if( this.error ) return; - - begin = ( seekTime || seekTime === 0 ) ? seekTime : this.playing ? this.loopStartTime : this.startTime; - this._lastUserAction = this.playing ? 'seek' : 'play'; + if( !this.duration ){ + this.autoplay = true; + return; + }; + + this._lastUserAction = 0 <= this.seekTime ? 'seek' : 'play'; + + end = X_AudioWrapper_getEndTime( this ); + begin = X_AudioWrapper_getStartTime( this, end, true ); + + console.log( '[SLAudio] play ' + begin + ' -> ' + end ); - this._currentTime( begin ); this._rawObject.Volume = this.volume; + this._currentTime( begin ); if( !this.playing ){ this._rawObject.play(); @@ -304,11 +270,11 @@ if( X.Pulgin.SilverlightEnabled ){ this.playing = true; }; - halfway = this.endTime < this.duration; + halfway = end < this.duration; this._timerID && X.Timer.remove( this._timerID ); if( halfway ){ - this._timerID = X.Timer.once( this.endTime - begin, this, this._onEnded ); + this._timerID = X.Timer.once( end - begin, this, this._onEnded ); } else { delete this._timerID; }; @@ -327,13 +293,25 @@ if( X.Pulgin.SilverlightEnabled ){ }, _onEnded : function(){ + var time; delete this._timerID; + if( this.playing ){ + + time = this._rawObject.Position.Seconds * 1000 - X_AudioWrapper_getEndTime( this ) | 0; + if( time < -16 ){ + console.log( '> onEnd ' + time ); + this._timerID = X.Timer.once( -time, this, this._onEnded ); + return; + }; + if( this.loop ){ + this.looped = true; this.play(); + this.proxy.dispatch( 'looped' ); } else { this.pause(); - this.dispatch( 'ended' ); + this.proxy.dispatch( 'ended' ); }; }; }, @@ -352,34 +330,39 @@ if( X.Pulgin.SilverlightEnabled ){ // SilverlightAudio.state state : function( obj ){ // @return Hash: { loop, error, paused, ended, source, duration } - var result; + var result, end; if( obj === undefined ){ return { startTime : this.startTime, - endTime : this.endTime, - loopStartTime : this.loopStartTime, + endTime : this.endTime < 0 ? this.duration : this.endTime, + loopStartTime : this.loopStartTime < 0 ? this.startTime : this.loopStartTime, + loopEndTime : this.loopEndTime < 0 ? ( this.endTime || this.duration ) : this.loopEndTime, + currentTime : this._rawObject.Position.Seconds * 1000, + + loop : this.loop, + looped : this.looped, volume : this.volume, error : this.error, playing : this.playing, - duration : this.duration || 0 // this._rawObject.NaturalDuration.Seconds; + duration : this.duration // this._rawObject.NaturalDuration.Seconds; }; }; result = X_AudioWrapper_updateStates( this, obj ); if( result & 2 ){ // seek - this.play( this.seekTime ); - delete this.seekTime; + this.play(); } else { if( result & 1 ){ - halfway = this.endTime < this.duration; + end = X_AudioWrapper_getEndTime( this ); + halfway = end < this.duration; this._timerID && X.Timer.remove( this._timerID ); if( halfway ){ - this._timerID = X.Timer.once( this.endTime - this._rawObject.Position.Seconds * 1000, this, this._onEnded ); + this._timerID = X.Timer.once( end - this._rawObject.Position.Seconds * 1000, this, this._onEnded ); } else { delete this._timerID; }; @@ -395,15 +378,28 @@ if( X.Pulgin.SilverlightEnabled ){ _currentTime : function( time ){ // @param Number: time var position = this._rawObject.Position; // [!] create instance - position.Seconds = time / 1000; // set current time + position.Seconds = time / 1000 | 0; // set current time this._rawObject.Position = position; // [!] reattach instance } } ); - -}; + function slerror(){ + alert( 'slerror' ); + }; + + X_Audio_BACKENDS.push( { + backendName : 'Silverlight Audio', + detect : function( proxy, source, ext ){ + var ok = ext === 'mp3' || ext === 'wma' || ext === 'wav'; + proxy.asyncDispatch( ok ? 'support' : 'nosupport' ); + }, + + klass : X_Audio_SLAudioWrapper + + } ); +}; \ No newline at end of file