OSDN Git Service

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