OSDN Git Service

Version 0.6.137, fix X.EventDispatcher.unlisten & remove X.Node.destroy.
[pettanr/clientJs.git] / 0.6.x / js / 07_audio / 03_XSilverlightAudio.js
index 5ef3c25..f4b84ab 100644 (file)
 var X_Audio_SLAudioWrapper,\r
        X_Audio_SLAudio_uid = 0;\r
 \r
-if( X.Pulgin.SilverlightEnabled ){\r
+if( X[ 'Pulgin' ][ 'SilverlightEnabled' ] ){\r
        \r
        // X.Node.inherits はできない。_rawObject は <object> でなく silverlight\r
-       X_Audio_SLAudioWrapper = X.EventDispatcher.inherits(\r
+       X_Audio_SLAudioWrapper = X_EventDispatcher[ 'inherits' ](\r
                'X.AV.SilverlightAudioWrapper',\r
-               X.Class.POOL_OBJECT,\r
+               X_Class.POOL_OBJECT,\r
                {\r
-                       _isSilverlight  : true, // for X.EventDispatcher.listen\r
+                       '_rawType'      : X_EventDispatcher_EVENT_TARGET_TYPE.SILVER_LIGHT,\r
                proxy           : null,\r
                \r
                        startTime       : 0,\r
@@ -53,8 +53,8 @@ if( X.Pulgin.SilverlightEnabled ){
                                \r
                                if( !X_Audio_SLAudio_uid ){\r
                                        // source\r
-                                       // X_Node_systemNode.create( 'script', { type : 'text/xaml', id : 'silverlightaudio' } )\r
-                                       //      .text( '<Canvas xmlns="http://schemas.microsoft.com/client/2007" ' +\r
+                                       // X_Node_systemNode[ 'create' ]( 'script', { type : 'text/xaml', id : 'silverlightaudio' } )\r
+                                       //      [ 'text' ]( '<Canvas xmlns="http://schemas.microsoft.com/client/2007" ' +\r
                                        // 'xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></Canvas>');\r
                                        \r
                                        // html に以下を書いた                     \r
@@ -69,16 +69,17 @@ if( X.Pulgin.SilverlightEnabled ){
                                // TODO embed\r
                                this.proxy       = proxy;\r
                                this._source     = source;\r
+                               // X.Audio._slOnload_ は不可\r
                        this._onload     = 'XAudioSilverlightOnLoad' + ( ++X_Audio_SLAudio_uid );\r
                                this._callback   = window[ this._onload ] = X_Callback_create( this, this.onSLReady );\r
                        this.xnodeObject = X_Node_body\r
-                               .create( 'object', {\r
+                               [ 'create' ]( 'object', {\r
                                                type   : 'application/x-silverlight-2',\r
                                                data   : 'data:application/x-silverlight-2,',\r
                                                width  : 1,\r
                                                height : 1\r
                                        })\r
-                                       .html(\r
+                                       [ 'html' ](\r
                                            '<param name="background" value="#00000000">' +      // transparent\r
                                            '<param name="windowless" value="true">' +\r
                                            '<param name="source" value="#silverlightaudio">' +  // XAML ID\r
@@ -89,7 +90,7 @@ if( X.Pulgin.SilverlightEnabled ){
                                        );\r
                                X_AudioWrapper_updateStates( this, option );\r
        \r
-                               this.listenOnce( X.Event.KILL_INSTANCE );\r
+                               this[ 'listenOnce' ]( X_EVENT_KILL_INSTANCE );\r
                        },\r
                        \r
                        onSLReady : function( sender ){\r
@@ -108,14 +109,15 @@ if( X.Pulgin.SilverlightEnabled ){
                                                '<MediaElement x:Name="media" Source="' + this._source + '" Volume="' + this.volume + '" AutoPlay="false" />' +\r
                                        '</Canvas>'));\r
                \r
-                               this._rawObject = sender.findName('media'); // x:Name='media'\r
-                               \r
-                               this.listen( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] );\r
+                               this[ '_rawObject' ] = sender.findName('media'); // x:Name='media'\r
+\r
+                               this[ 'listen' ]( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] );\r
                        },\r
                        \r
                        handleEvent : function( e ){\r
                                var lastState, currentState;\r
                                \r
+                               console.log( e.type );\r
                                switch( e.type ){\r
                                        \r
                                        case 'MediaFailed' :\r
@@ -123,20 +125,21 @@ if( X.Pulgin.SilverlightEnabled ){
                                                this.playing = false;\r
                                                this._ended  = true;\r
                                                this._paused = false;\r
-                                               this.proxy.dispatch( 'error' ); // open failed\r
+                                               this.proxy[ 'dispatch' ]( X_EVENT_ERROR ); // open failed\r
                                                break;\r
 \r
                                        case 'MediaOpened' :\r
                                                // http://msdn.microsoft.com/ja-jp/library/bb979710(VS.95).aspx\r
-                                               this.duration = this._rawObject.NaturalDuration.Seconds * 1000;\r
+                                               this.duration = this[ '_rawObject' ].NaturalDuration.Seconds * 1000;\r
                                                // TODO 'canplaythrough'\r
-                                               this.proxy.asyncDispatch( 'loadstart' );\r
-                               this.proxy.asyncDispatch( 'loadedmetadata' );\r
-                               this.proxy.asyncDispatch( 'loadeddata' );\r
-                               this.proxy.asyncDispatch( 'canplay' );\r
-                               this.proxy.asyncDispatch( 'canplaythrough' );\r
+                                               //this.proxy[ 'asyncDispatch' ]( 'loadstart' );\r
+                               //this.proxy[ 'asyncDispatch' ]( 'loadedmetadata' );\r
+                               //this.proxy[ 'asyncDispatch' ]( 'loadeddata' );\r
+                               //this.proxy[ 'asyncDispatch' ]( 'canplay' );\r
+                               //this.proxy[ 'asyncDispatch' ]( 'canplaythrough' );\r
+                               this.proxy[ 'asyncDispatch' ]( X_EVENT_READY );\r
                                \r
-                               this.autoplay && X.Timer.once( 16, this, this.play );\r
+                               this.autoplay && X_Timer_once( 16, this, this.play );\r
                                                break;\r
 \r
                                        case 'MediaEnded' :                             \r
@@ -145,7 +148,7 @@ if( X.Pulgin.SilverlightEnabled ){
 \r
                                        case 'CurrentStateChanged' :\r
                                                lastState        = this._lastState,\r
-                                               currentState = this._rawObject.CurrentState;\r
+                                               currentState = this[ '_rawObject' ].CurrentState;\r
                                \r
                                                // ignore consecutive events or 'Closed' == 'Error'\r
                                                if( lastState === currentState\r
@@ -159,10 +162,10 @@ if( X.Pulgin.SilverlightEnabled ){
                                                        case 'Opening' :\r
                                                                switch( this._lastUserAction ){\r
                                                                        case 'play' :\r
-                                                                               this.proxy.dispatch( 'waiting' );\r
+                                                                               this.proxy[ 'dispatch' ]( X_EVENT_MEDIA_WAITING );\r
                                                                                break;\r
                                                                        case 'seek' :\r
-                                                                               this.proxy.dispatch( 'seeking' );\r
+                                                                               this.proxy[ 'dispatch' ]( X_EVENT_MEDIA_SEEKING );\r
                                                                                break;\r
                                                                        case 'pause' :\r
                                                                                break;\r
@@ -179,7 +182,7 @@ if( X.Pulgin.SilverlightEnabled ){
                                                                this.playing = false;\r
                                                                this._ended  = true;\r
                                                                this._paused = false;\r
-                                                               this.proxy.dispatch( 'error' );\r
+                                                               this.proxy[ 'dispatch' ]( X_EVENT_ERROR );\r
                                                                break;\r
 \r
                                                        // userAction.pause()              -> MediaState('Paused') -> x\r
@@ -194,7 +197,7 @@ if( X.Pulgin.SilverlightEnabled ){
                                                                                this.seekTime = 0;\r
                                                                                this._ended   = true;\r
                                                                                this._paused  = false;\r
-                                                                               this.proxy.dispatch( 'ended' );\r
+                                                                               this.proxy[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );\r
                                                                                this._currentTime( this.startTime );\r
                                                                                break;\r
                                                                        case 'pause':\r
@@ -213,7 +216,7 @@ if( X.Pulgin.SilverlightEnabled ){
                                                                this.playing = true;\r
                                                                this._ended  = false;\r
                                                                this._paused = false;\r
-                                                               this.proxy.dispatch( 'playing' );\r
+                                                               this.proxy[ 'dispatch' ]( X_EVENT_MEDIA_PLAYING );\r
                                                                break;\r
 \r
                                                        // stop()\r
@@ -226,22 +229,22 @@ if( X.Pulgin.SilverlightEnabled ){
                                                };\r
                                                break;\r
 \r
-                                       case X.Event.KILL_INSTANCE :\r
+                                       case X_EVENT_KILL_INSTANCE :\r
                                                if( this._onload ){\r
                                                        // window への delete に ie5 は対応しないが、そもそも ie5 は Silverlight に非対応\r
-                                                       delete window[ this._onload ];\r
+                                                       window[ this._onload ] = null;\r
                                                        delete this._onload;\r
                                                        X_Callback_correct( this._callback );\r
                                                };\r
-                                               this.xnodeObject.destroy();\r
+                                               this.xnodeObject[ 'kill' ]();\r
                                                break;\r
                                };\r
                        },\r
                        \r
                        close : function(){\r
                                this.playing && this.pause();\r
-                               this.proxy.dispatch( 'ended' );\r
-                               this.kill();\r
+                               this.proxy[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );\r
+                               this[ 'kill' ]();\r
                        },\r
                        \r
                        // SilverlightAudio.play\r
@@ -258,30 +261,31 @@ if( X.Pulgin.SilverlightEnabled ){
                                this._lastUserAction = 0 <= this.seekTime ? 'seek' : 'play';\r
                                \r
                                end   = X_AudioWrapper_getEndTime( this );\r
-                               begin = X_AudioWrapper_getStartTime( this, end, true );\r
+                               begin = X_AudioWrapper_getStartTime( this, end, true ) | 0;\r
                                \r
                                console.log( '[SLAudio] play ' + begin + ' -> ' + end );\r
                                \r
-                           this._rawObject.Volume = this.volume;\r
+                           this[ '_rawObject' ].Volume = this.volume;\r
+                           this._beginTime = begin;\r
                            this._currentTime( begin );\r
                            \r
                            if( !this.playing ){\r
-                                   this._rawObject.play();\r
-                           this.proxy.dispatch( 'play' );\r
+                                   this[ '_rawObject' ].play();\r
+                           //this.proxy[ 'dispatch' ]( 'play' );\r
                            \r
                            this.playing = true;\r
                            };\r
                    \r
-                   this._timerID && X.Timer.remove( this._timerID );\r
+                   this._timerID && X_Timer_remove( this._timerID );\r
                    \r
                 if( end < this.duration ){\r
-                                       this._timerID = X.Timer.once( end - begin, this, this._onEnded );\r
+                                       this._timerID = X_Timer_once( end - begin, this, this._onEnded );\r
                 } else {\r
                        delete this._timerID;\r
                 };\r
                 \r
                                if( !this._interval ){\r
-                                       this._interval = X.Timer.add( 1000, 0, this, this._onInterval );\r
+                                       this._interval = X_Timer_add( 1000, 0, this, this._onInterval );\r
                                };\r
                        },\r
                                \r
@@ -290,30 +294,40 @@ if( X.Pulgin.SilverlightEnabled ){
                                                delete this._interval;\r
                                                return X_Callback_UN_LISTEN;\r
                                        };\r
-                                       this.proxy.dispatch( 'timeupdate' );\r
+                                       this.proxy[ 'dispatch' ]( X_EVENT_MEDIA_PLAYING );\r
                                },\r
                                \r
                                _onEnded : function(){\r
-                                       var time;\r
+                                       var time, end;\r
                                        delete this._timerID;\r
                                        \r
                            if( this.playing ){\r
+                               console.log( '> end ' + X_AudioWrapper_getEndTime( this ) + ' current:' + ( this[ '_rawObject' ].Position.Seconds * 1000 | 0 ) );\r
+                               time = this[ '_rawObject' ].Position.Seconds * 1000 | 0;\r
+                               \r
+                               if( time <= this._beginTime ){\r
+                                       console.log( '== waiting' );\r
+                                       this.proxy[ 'dispatch' ]( X_EVENT_MEDIA_WAITING );\r
+                                       this._timerID = X_Timer_once( X_AudioWrapper_getEndTime( this ) - this._beginTime, this, this._onEnded );\r
+                                       return;\r
+                               };\r
                                \r
-                               time = this._rawObject.Position.Seconds * 1000 - X_AudioWrapper_getEndTime( this ) | 0;\r
+                               time -= X_AudioWrapper_getEndTime( this );\r
                                if( time < 0 ){\r
                                        console.log( '> onEnd ' + time );\r
-                                       this._timerID = X.Timer.once( -time, this, this._onEnded );\r
+                                       this._timerID = X_Timer_once( -time, this, this._onEnded );\r
                                        return;\r
                                };\r
                                \r
                                if( this.loop ){\r
-                                       if( !( this.proxy.dispatch( 'looped' ) & X.Callback.PREVENT_DEFAULT ) ){\r
+                                       if( !( this.proxy[ 'dispatch' ]( X_EVENT_MEDIA_BEFORE_LOOP ) & X_Callback_PREVENT_DEFAULT ) ){\r
                                                this.looped = true;\r
+                                               this.proxy[ 'dispatch' ]( X_EVENT_MEDIA_LOOPED );\r
                                                this.play();\r
-                                       };\r
+                                       };\r
                                } else {\r
                                        this.pause();\r
-                                       this.proxy.dispatch( 'ended' );\r
+                                       this.proxy[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );\r
                                };\r
                            };\r
                                },\r
@@ -328,8 +342,8 @@ if( X.Pulgin.SilverlightEnabled ){
                                this._paused  = true;\r
                                this._ended   = false;\r
                                                        \r
-                               this._rawObject.pause();\r
-                               this.proxy.dispatch( 'pause' );\r
+                               this[ '_rawObject' ].pause();\r
+                               //this.proxy[ 'dispatch' ]( 'pause' );\r
                        },\r
                        \r
                        // SilverlightAudio.state\r
@@ -343,15 +357,15 @@ if( X.Pulgin.SilverlightEnabled ){
                                        loopStartTime : this.loopStartTime < 0 ? this.startTime : this.loopStartTime,\r
                                        loopEndTime   : this.loopEndTime < 0 ? ( this.endTime || this.duration ) : this.loopEndTime,\r
                                        \r
-                                       currentTime   : this.playing ? this._rawObject.Position.Seconds * 1000 : this.seekTime,\r
-                                       \r
-                                       \r
+                                       // 整数化 しておかないと seek 時に不具合がある。\r
+                                       currentTime   : this.playing ? this[ '_rawObject' ].Position.Seconds * 1000 | 0 : this.seekTime,\r
+\r
                                        loop          : this.loop,\r
                                        looped        : this.looped,\r
                                        volume        : this.volume,\r
                                        error         : this.error,\r
                                        playing       : this.playing,\r
-                                       duration      : this.duration // this._rawObject.NaturalDuration.Seconds;\r
+                                       duration      : this.duration // this[ '_rawObject' ].NaturalDuration.Seconds;\r
                                    };\r
                                };\r
                        \r
@@ -363,28 +377,28 @@ if( X.Pulgin.SilverlightEnabled ){
                                        if( result & 1 ){\r
                                                end     = X_AudioWrapper_getEndTime( this );\r
                                                halfway = end < this.duration;\r
-                                               this._timerID && X.Timer.remove( this._timerID );\r
+                                               this._timerID && X_Timer_remove( this._timerID );\r
                                                \r
                                                if( halfway ){\r
-                                                       this._timerID = X.Timer.once( end - this._rawObject.Position.Seconds * 1000, this, this._onEnded );\r
+                                                       this._timerID = X_Timer_once( end - this[ '_rawObject' ].Position.Seconds * 1000 | 0, this, this._onEnded );\r
                                                } else {\r
                                                        delete this._timerID;\r
                                                };\r
 \r
                                        };\r
                                        if( result & 4 ){\r
-                              this._rawObject.Volume = this.volume;\r
+                              this[ '_rawObject' ].Volume = this.volume;\r
                                        };\r
                                };\r
                        },\r
                        \r
                                // SilverlightAudio.currentTime\r
                                _currentTime : function( time ){ // @param Number: time\r
-                                       var position = this._rawObject.Position; // [!] create instance\r
+                                       var position = this[ '_rawObject' ].Position; // [!] create instance\r
        \r
                                        position.Seconds = time / 1000 | 0; // set current time\r
                                \r
-                                       this._rawObject.Position = position; // [!] reattach instance\r
+                                       this[ '_rawObject' ].Position = position; // [!] reattach instance\r
                                }\r
 \r
                }\r
@@ -399,7 +413,7 @@ if( X.Pulgin.SilverlightEnabled ){
 \r
                detect : function( proxy, source, ext ){\r
                        var ok = ext === 'mp3' || ext === 'wma' || ext === 'wav';\r
-                       proxy.asyncDispatch( ok ? 'support' : 'nosupport' );                            \r
+                       proxy[ 'asyncDispatch' ]( { type : X_EVENT_COMPLETE, canPlay : ok } );                          \r
                },\r
                \r
                klass : X_Audio_SLAudioWrapper\r