OSDN Git Service

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