OSDN Git Service

Version 0.6.193, bug fixes X.Node.Anime.
[pettanr/clientJs.git] / 0.6.x / js / 07_audio / 05_XWMPAudio.js
1 // https://msdn.microsoft.com/ja-jp/library/cc410695.aspx
2 // Windows Media Player コントロール Version 6.4
3
4 // http://www.tohoho-web.com/wwwmmd2.htm
5
6 // http://devedge.primedirective.net/viewsource/2003/windows-media-in-netscape/index.html
7
8 var X_WMPAudio;
9
10 if( X_Pulgin_WMP_VERSION ){ // IETester で 6.x は不可
11         X_WMPAudio = X_AudioBase[ 'inherits' ](
12                 'X.WMPAudio',
13                 X_Class.POOL_OBJECT,
14                 {
15                 xnodeObject     : null,
16                 wmp             : null,
17                 _wmp            : null,
18                 // 0 : no <object>
19                 // 1 : loading
20                 // 2 : loaded
21                 _readyState     : 0,
22                 _seekDirection  : 0,
23                         _timerID        : 0,
24                         
25                         'Constructor' : function( disatcher, source, option ){
26                                 this.disatcher   = disatcher || this;
27                                 this._source     = source;
28                                 
29                                 if( 7 <= X_Pulgin_WMP_VERSION ){
30                                 this.xnodeObject = X_Node_systemNode[ 'create' ]( 'object', {
31                                                         'classID' : 'CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6',
32                                                         width     : 1,
33                                                         height    : 1
34                                                 })[ 'html' ](
35                                                         X_UA[ 'IE55' ] ? '' : '<param name="uiMode" value="none">'
36                                                         //+ '<param name="URL" value="' + source + '">'
37                                                         //+ '<param name="AutoStart" value="' + option.autoplay + '">'
38                                                 );
39                                 } else {
40                                 this.xnodeObject = X_Node_systemNode[ 'create' ]( 'object', {
41                                                         classID  : 'CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95',
42                                                         width    : 0,
43                                                         height   : 0
44                                                 })[ 'html' ](
45                                                         X_UA[ 'IE55' ] ? '' : '<param name="ShowControls" value="false">'
46                                                         //+ '<param name="FileName" value="' + source + '">'
47                                                         //+ '<param name="AutoStart" value="' + option.autoplay + '">'
48                                                 );
49                                 };
50                                 // TODO embed
51
52                                 this.setState( option );
53
54                                 X_ViewPort[ 'listenOnce' ]( X_EVENT_AFTER_UPDATE, this );
55                                 this[ 'listenOnce' ]( X_EVENT_KILL_INSTANCE );
56                         },
57                         
58                         handleEvent : function( e ){
59                                 switch( e.type ){
60                                         case X_EVENT_AFTER_UPDATE :
61                                                 this._readyState = 1;
62                                                 if( 7 <= X_Pulgin_WMP_VERSION ){
63                                                         this._wmp          = this.xnodeObject[ '_rawObject' ];
64                                                         this._wmp[ 'URL' ] = this._source;
65                                                         this.wmp           = this._wmp[ 'controls' ];
66                                                 } else {
67                                                         this.wmp = this.xnodeObject[ '_rawObject' ];
68                                                         this.wmp[ 'FileName' ] = this._source;
69                                                 };
70                                                 this._timerID = X_Timer_add( 100, 0, this, this._onTimer );
71                                                 break;
72
73                                         case X_EVENT_KILL_INSTANCE :
74                                                 this.playing && this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );
75                                                 this.playing && this.actualPause();
76                                             this.wmp.stop();
77                                                 this.xnodeObject[ 'kill' ]();
78                                                 break;
79                                 };
80                         },
81                         
82                         // WMPAudio.play
83                         actualPlay : function(){
84                                 var begin, offset, end;
85
86                                 if( this._readyState < 2 ){
87                                         this._playReserved = true;
88                                         return;
89                                 };
90                                 
91                                 end   = X_Audio_getEndTime( this );
92                                 begin = this._beginTime = X_Audio_getStartTime( this, end, true ) | 0;
93
94                             console.log( '[play] ' + begin + ' -> ' + end );
95                             
96                             if( !this.playing ){
97                                         this.setVolume();
98                                         this.wmp.play();
99                                         
100                             this.playing = true;
101                             } else {
102                                 this._seekDirection = this.getActualCurrentTime() < begin ? 1 : -1;
103                             };
104                             
105                                 // 1 秒以下は指定できないため四捨五入
106                                 //begin = ( begin / 1000 | 0 ) * 1000 + ( 500 < begin % 1000 ? 1000 : 0 );
107                             this.wmp[ 'CurrentPosition' ] = begin / 1000;
108                                 
109                                 if( !this._timerID ) this._timerID = X_Timer_add( 100, 0, this, this._onTimer );
110                         },
111                                 
112                                 _onTimer : function(){
113                                         var progress, time;
114                                         
115                                         // road 中の場合
116                                         if( this._readyState === 1 ){
117                                                 if( 7 <= X_Pulgin_WMP_VERSION ){
118                                                 progress = this._wmp[ 'network' ][ 'downloadProgress' ];
119                                                 } else {
120                                                 progress = this.wmp[ 'BufferingProgress' ];
121                                                 };
122                                                 if( progress < 100 ){
123                                                         this.disatcher[ 'dispatch' ]( { type : X_EVENT_PROGRESS, 'percent' : progress } );
124                                                 } else {
125                                                         this._readyState = 2;
126                                                         if( 7 <= X_Pulgin_WMP_VERSION ){
127                                                         this.duration = this._wmp[ 'currentMedia' ].duration * 1000 | 0;
128                                                         } else {
129                                                         this.duration = this.wmp[ 'Duration' ] * 1000 | 0;
130                                                         };
131                                                         this.disatcher[ 'dispatch' ]( X_EVENT_READY );
132                                                 };
133                                         } else
134                                         // ended の判定
135                             if( this.playing ){
136                                 time = this.getActualCurrentTime();
137                                 
138                                 // waiting
139                                 if( this._seekDirection ){
140                                         if( this._seekDirection === 1 ? ( time < this._beginTime ) : ( this._lastCurrentTime <= time ) ){
141                                                 this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_SEEKING );
142                                                 return;
143                                         };
144                                         delete this._seekDirection;
145                                 };
146                                 if( time === this._lastCurrentTime ){
147                                         this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_WAITING );
148                                         return;
149                                 };              
150                                 this._lastCurrentTime = time;
151                                 
152                                 // ended ではない
153                                 if( time - X_Audio_getEndTime( this ) < -50 ){
154                                         this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_PLAYING );
155                                         return;
156                                 };
157                                 
158                                 // ended
159                                 if( this.autoLoop ){
160                                         if( !( this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_BEFORE_LOOP ) & X_CALLBACK_PREVENT_DEFAULT ) ){
161                                                 this.looped = true;
162                                                 this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_LOOPED );
163                                                 this.actualPlay();
164                                         };
165                                 } else {
166                                         this.actualPause();
167                                         this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_ENDED );
168                                         delete this._timerID;
169                                                         return X_CALLBACK_UN_LISTEN;
170                                 };
171                             };
172                                 },
173                         
174                         // WMPAudio.pause
175                         actualPause : function(){
176                                 this.playing  = false;
177                                 this._timerID && X_Timer_remove( this._timerID );
178                                 delete this._timerID;
179                                 
180                             this.wmp.pause();
181                         },
182
183                         setVolume : function(){
184                                 if( 7 <= X_Pulgin_WMP_VERSION ){
185                                 this._wmp[ 'settings' ][ 'Volume' ] = this.gain * 100;
186                                 } else {
187                                 this.wmp[ 'Volume' ] = ( 1 - this.gain ) * 10000;
188                                 };                              
189                         },
190
191                         getActualCurrentTime : function(){
192                             return this.wmp[ 'CurrentPosition' ] * 1000 | 0;
193                         },
194                         
195                         afterUpdateState : function( result ){
196                                 if( result & 3 ){ // seek
197                         this.actualPlay();
198                                 } else
199                                 if( result & 4 ){
200                                         this.setVolume();
201                                 };                      
202                         }
203
204                 }
205         );
206
207         X_Audio_BACKENDS.push( {
208                 backendID   : 16,
209                 
210                 backendName : 'WMP' + X_Pulgin_WMP_VERSION,
211
212                 canPlay : {
213                         'mp3'  : true,
214                         'm4a'  : true,
215                         'wma'  : true,
216                         'wav'  : true,
217                         'mid'  : true,
218                         'midi' : true,
219                         'snd'  : true,
220                         'au'   : true,
221                         'aif'  : true,
222                         'aiff' : true,
223                         'aicf' : true
224                 },
225
226                 detect : function( proxy, source, ext ){
227                         proxy[ 'asyncDispatch' ]( { type : X_EVENT_COMPLETE, canPlay : 0 <= 'mp3 m4a wma wav midi snd au aiff aicf'.indexOf( ext ) } );                         
228                 },
229                 
230                 klass : X_WMPAudio
231                 
232         } );
233
234 };