OSDN Git Service

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