OSDN Git Service

3727959efc6c2c7330a8914cea2fc4d61520cdc2
[pettanr/clientJs.git] / 0.6.x / js / 06_audio / 00_XAudio.js
1 X.Audio = {\r
2         HTML5       : 1,\r
3         Flash       : 2,\r
4         Silverlight : 3,\r
5         Unity       : 4,\r
6         WMP         : 5,\r
7         RealPlayer  : 6,\r
8         QuickTime   : 7,\r
9         \r
10         create : function( sourceList, opt_option ){\r
11                 return new X_AudioProxy( X.Type.isArray( sourceList ) ? X.copyArray( sourceList ) : [ sourceList ], opt_option || {} );\r
12         }\r
13 };\r
14 \r
15 var X_Audio_BACKENDS = [];\r
16 \r
17 function X_Audio_detectBackend( proxy, sourceList, option ){\r
18         var source = sourceList.shift() || '', \r
19                 parts  = source.split( '?' )[ 0 ].split( '#' )[ 0 ].split( '.' ),\r
20                 ext    = parts[ parts.length - 1 ],\r
21                 backend, ext, sup;\r
22         \r
23         if( source && X_Audio_BACKENDS.length ){\r
24                 sup      = [ proxy, option, sourceList, source, ext ];\r
25                 sup[ 5 ] = sup;\r
26                 X_Audio_BACKENDS[ 0 ]\r
27                         .detect( source, ext )\r
28                         .listenOnce( [ 'support', 'nosupport' ], X_Audio_detectComplete, sup );\r
29         } else {\r
30                 proxy.asyncDispatch( 0, 'nobackend' );\r
31         };\r
32 };\r
33 \r
34 function X_Audio_detectComplete( e, proxy, option, sourceList, source, ext, sup ){\r
35         var i = X_Audio_BACKENDS.indexOf( this ), backend;\r
36         \r
37         this.unlisten( [ 'support', 'nosupport' ], X_Audio_detectComplete, sup );\r
38         \r
39         switch( e.type ){\r
40                 case 'support' :\r
41                         proxy._backend = i;\r
42                         proxy.asyncDispatch( 0, { type : 'backendfound', option : option, source : source } );\r
43                         break;\r
44                 case 'nosupport' :\r
45                         if( backend = X_Audio_BACKENDS[ i + 1 ] ){\r
46                                 backend.detect( source, ext ).listen( [ 'support', 'nosupport' ], X_Audio_detectComplete, sup );\r
47                         } else\r
48                         if( sourceList.length ){\r
49                                 X_Audio_detectBackend( proxy, sourceList, option );\r
50                         } else {\r
51                                 proxy.asyncDispatch( 0, 'nobackend' );\r
52                         };\r
53                         break;\r
54         };\r
55 };\r
56 \r
57 \r
58 X_AudioProxy = X.EventDispatcher.inherits(\r
59         'X.AV.AudioProxy',\r
60         X.Class.POOL_OBJECT,\r
61         {\r
62                 source      : '',\r
63                 backendName : '',\r
64                 _backend    : -1,\r
65                 \r
66                 Constructor : function( sourceList, option ){\r
67                         X_Audio_detectBackend( this, sourceList, option );\r
68                         this.listenOnce( [ 'backendfound', 'nobackend', X.Event.KILL_INSTANCE ], X_AudioProxy_handleEvent );\r
69                 },\r
70                 \r
71                 close : function(){\r
72                         return this._backend !== -1 && X_Audio_BACKENDS[ this._backend ].close.call( this );\r
73                 },\r
74                 \r
75                 play : function( position ){\r
76                         //console.log( 'proxy play ' + this._backend );\r
77                         return this._backend !== -1 && X_Audio_BACKENDS[ this._backend ].play.call( this, position );\r
78                 },\r
79                 \r
80                 pause : function(){\r
81                         return this._backend !== -1 && X_Audio_BACKENDS[ this._backend ].pause.call( this );\r
82                 },\r
83                 \r
84                 stop : function(){\r
85                         return this._backend !== -1 && X_Audio_BACKENDS[ this._backend ].stop.call( this );\r
86                 },\r
87                 \r
88                 loop : function( v ){\r
89                         return this._backend !== -1 && X_Audio_BACKENDS[ this._backend ].loop.call( this, v );\r
90                 },\r
91 \r
92                 state : function(){\r
93                         return this._backend !== -1 && X_Audio_BACKENDS[ this._backend ].state.call( this );\r
94                 },\r
95 \r
96                 volume : function( v ){\r
97                         return this._backend !== -1 && X_Audio_BACKENDS[ this._backend ].volume.call( this, v );\r
98                 },\r
99 \r
100                 startTime : function( time ){\r
101                         return this._backend !== -1 && X_Audio_BACKENDS[ this._backend ].startTime.call( this, time );\r
102                 },\r
103 \r
104                 currentTime : function( time ){\r
105                         return this._backend !== -1 && X_Audio_BACKENDS[ this._backend ].currentTime.call( this, time );\r
106                 },\r
107 \r
108                 isPlaying : function(){\r
109                         return this._backend !== -1 && X_Audio_BACKENDS[ this._backend ].isPlaying.call( this );\r
110                 }\r
111                 \r
112         }\r
113 );\r
114 \r
115 function X_AudioProxy_handleEvent( e ){\r
116         switch( e.type ){\r
117                 case 'backendfound' :\r
118                         this.unlisten( 'nobackend', X_AudioProxy_handleEvent );\r
119                         this.source = e.source;\r
120                         this.backendName = X_Audio_BACKENDS[ this._backend ].backendName;\r
121                         X_Audio_BACKENDS[ this._backend ].register( this, e.source, e.option );\r
122                         break;\r
123                 \r
124                 case 'nobackend' :\r
125                         this.kill();\r
126                         break;\r
127                 \r
128                 case X.Event.KILL_INSTANCE :\r
129                         this.close();\r
130                         break;\r
131         };\r
132 };\r