3 * original : uupaa-js SilverlightAudio.js
\r
4 * https://code.google.com/p/uupaa-js/source/browse/trunk/0.8/src/Audio/SilverlightAudio.js?r=568
\r
6 * Silverlight 4 → 5における不具合の状況
\r
7 * http://www.slideshare.net/wakabayashiy/silverlight-4-5
\r
9 * IE10以降でSilverlightでF5押したらフリーズする不具合と対処
\r
10 * http://katsuyuzu.hatenablog.jp/entry/2014/01/11/003550
\r
12 * SilverlLight5 ie6&7(ietester,winxp), ie8(winxp) で動作確認。firefox32 では動作しない。(4以下の方がよい?)
\r
15 var X_Audio_SLAudio, X_Audio_SLAudioWrapper,
\r
16 X_Audio_SLAudio_uid = 0,
\r
17 X_Audio_SLAudio_LIVE_LIST = [],
\r
18 X_Audio_SLAudio_POOL_LIST = [];
\r
20 if( X.Pulgin.SilverlightEnabled ){
\r
22 function getSLAudioWrapper( proxy ){
\r
23 var i = X_Audio_SLAudio_LIVE_LIST.length;
\r
25 if( X_Audio_SLAudio_LIVE_LIST[ --i ].proxy === proxy ) return X_Audio_SLAudio_LIVE_LIST[ i ];
\r
29 X_Audio_SLAudio = X.Class._override(
\r
30 new X.EventDispatcher(),
\r
32 backendName : 'Silverlight Audio',
\r
34 detect : function( source, ext ){
\r
35 var ok = ext === 'mp3' || ext === 'wma';
\r
37 this.asyncDispatch( 0, ok ? 'support' : 'nosupport' );
\r
42 register : function( proxy, source, option ){
\r
43 X_Audio_SLAudio_LIVE_LIST.push( new X_Audio_SLAudioWrapper( proxy, source, option ) );
\r
47 return getSLAudioWrapper( this ).close();
\r
50 play : function( position ){
\r
51 return getSLAudioWrapper( this ).play( position );
\r
55 return getSLAudioWrapper( this ).pause();
\r
59 return getSLAudioWrapper( this ).stop();
\r
62 loop : function( v ){
\r
63 return getSLAudioWrapper( this ).loop( v );
\r
67 return getSLAudioWrapper( this ).state();
\r
70 volume : function( v ){
\r
71 return getSLAudioWrapper( this ).volume( v );
\r
74 startTime : function( time ){
\r
75 return getSLAudioWrapper( this ).startTime( time );
\r
78 currentTime : function( time ){
\r
79 return getSLAudioWrapper( this ).currentTime( time );
\r
82 isPlaying : function(){
\r
83 return getSLAudioWrapper( this ).isPlaying();
\r
88 X_Audio_BACKENDS.push( X_Audio_SLAudio );
\r
94 X_Audio_SLAudioWrapper = X.EventDispatcher.inherits(
\r
95 'X.AV.SilverlightAudioWrapper',
\r
96 X.Class.POOL_OBJECT,
\r
98 _isSilverlight : true, // for X.EventDispatcher.listen
\r
102 xnodeObject : null,
\r
111 _lastUserAction : '',
\r
114 _interval : 0, // setInterval timer id
\r
116 Constructor : function( proxy, source, option ){
\r
118 if( !X_Audio_SLAudio_uid ){
\r
120 //X.Dom.Node._systemNode.create( 'script', { type : 'text/xaml', id : 'silverlightaudio' } )
\r
121 // .text( '<Canvas xmlns="http://schemas.microsoft.com/client/2007" ' +
\r
122 // 'xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></Canvas>');
\r
125 // <script id="silverlightaudio" type="text/xaml"><Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></Canvas></script>
\r
128 * [Silverlight 2]JavaScriptコードからSilverlightのオブジェクトを利用するには?[C#、VB]
\r
129 * http://www.atmarkit.co.jp/fdotnet/dotnettips/902slobjcallfromjs/slobjcallfromjs.html
\r
130 * このページのサンプルは sl5+firefox32 環境で動いている。xaml を js から利用する形ではなく、.xap を sl4 以下で作るのがよさそう.
\r
132 this.proxy = proxy;
\r
133 this._onload = 'XAudioSilverlightOnLoad';// + ( ++X_Audio_SLAudio_uid );
\r
134 this._callback = window[ this._onload ] = X_Callback_create( this, this.onSLReady, [ option.autoplay ] );
\r
135 this.xnodeObject = X.Dom.Node._body
\r
136 .create( 'object', {
\r
137 type : 'application/x-silverlight-2',
\r
138 data : 'data:application/x-silverlight-2,',
\r
143 '<param name="background" value="#00000000">' + // transparent
\r
144 '<param name="windowless" value="true">' +
\r
145 '<param name="source" value="#silverlightaudio">' + // XAML ID
\r
146 '<param name="onload" value="' + this._onload + '">'// + // bond to global
\r
147 //'<param value="2.0.31005.0" name="minRuntimeVersion">' +
\r
148 //'<param value="true" name="autoUpgrade">' +
\r
149 //'<param name="onerror" value="slerror">' // bond to global
\r
152 this._loop = option.loop;
\r
153 this._source = source;
\r
154 if( option.volume ) this._volume = option.volume;
\r
155 if( option.startTime ) this._startTime = option.startTime;
\r
157 this.listenOnce( X.Event.KILL_INSTANCE );
\r
160 onSLReady : function( sender, autoplay ){
\r
161 if( !this._onload ) return;
\r
163 window[ this._onload ] = null;
\r
164 delete this._onload;
\r
165 this._callback.kill();
\r
166 delete this._callback;
\r
168 sender.children.add(
\r
172 '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">' +
\r
173 '<MediaElement x:Name="media" Source="' + this._source + '" Volume="' + this._volume + '" AutoPlay="false" />' +
\r
176 this._rawObject = sender.findName('media'); // x:Name='media'
\r
178 this.listen( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] );
\r
180 autoplay && X.Timer.once( 100, this, this.play );
\r
183 handleEvent : function( e ){
\r
184 var lastState, currentState;
\r
188 case 'MediaFailed' :
\r
190 this._ended = true;
\r
191 this._paused = false;
\r
193 this.proxy.dispatch( 'error' ); // open failed
\r
195 case 'MediaOpened' :
\r
196 // http://msdn.microsoft.com/ja-jp/library/bb979710(VS.95).aspx
\r
197 this._duration = this._rawObject.NaturalDuration.Seconds;
\r
198 // TODO 'canplaythrough'
\r
199 this.proxy.dispatch( 'canplay' );
\r
201 case 'MediaEnded' :
\r
202 this.currentTime(this._startTime);
\r
205 this._rawObject.play();
\r
208 case 'CurrentStateChanged' :
\r
209 lastState = this._lastState,
\r
210 currentState = this._rawObject.CurrentState;
\r
212 // ignore consecutive events or 'Closed' == 'Error'
\r
213 if( lastState === currentState
\r
214 || ( (lastState === 'Closed' || lastState === 'Error') && ( currentState === 'Closed' || currentState === 'Error') ) ){
\r
217 this._lastState = currentState; // update last state
\r
219 switch (currentState) {
\r
224 // media.play(none supported file) -> 'Error'
\r
225 // media.play(file not found) -> 'Closed'
\r
226 // media.load -> 'Error'
\r
230 this._ended = true;
\r
231 this._paused = false;
\r
232 this.proxy.dispatch( 'error' );
\r
235 // userAction.pause() -> MediaState('Paused') -> x
\r
236 // userAction.stop() -> MediaState('Paused') -> x
\r
237 // userAction.play() + file end -> MediaState('Paused') -> uueventfire('ended')
\r
239 switch (this._lastUserAction) {
\r
240 case 'play': // play() -> file end -> event('ended')
\r
241 this._ended = true;
\r
242 this._paused = false;
\r
243 this.proxy.dispatch( 'ended' );
\r
244 this.currentTime(this._startTime);
\r
247 this._ended = false;
\r
248 this._paused = true;
\r
251 this._ended = true;
\r
252 this._paused = false;
\r
256 // media.play -> 'Playing'
\r
259 this._ended = false;
\r
260 this._paused = false;
\r
261 this.proxy.dispatch( 'playing' );
\r
266 this._ended = true;
\r
267 this._paused = false;
\r
268 this.currentTime(this._startTime);
\r
272 case X.Event.KILL_INSTANCE :
\r
273 if( this._onload ){
\r
274 window[ this._onload ] = null;
\r
275 this._callback.kill();
\r
277 this.xnodeObject.destroy();
\r
282 close : function(){
\r
283 if (this._interval) {
\r
284 X.Timer.remove( this._interval );
\r
285 delete this._interval;
\r
287 this.proxy.dispatch( 'ended' );
\r
291 // SilverlightAudio.play
\r
293 if (!this._error) {
\r
294 this._lastUserAction = 'play';
\r
296 this.currentTime(this._startTime);
\r
299 this._rawObject.play();
\r
300 this.proxy.dispatch( 'play' );
\r
302 if (!this._interval) {
\r
303 this._interval = X.Timer.add( 1000, 0, this, this._onInterval );
\r
308 _onInterval : function(){
\r
309 this.isPlaying() && this.proxy.dispatch( 'timeupdate' );
\r
312 // SilverlightAudio.pause
\r
313 pause : function(){
\r
314 if (!this._error) {
\r
315 this._lastUserAction = 'pause';
\r
316 this._paused = true;
\r
317 this._ended = false;
\r
318 this._rawObject.pause();
\r
319 this.proxy.dispatch( 'pause' );
\r
323 // SilverlightAudio.stop
\r
325 if (!this._error) {
\r
326 this._lastUserAction = 'stop';
\r
327 this._rawObject.stop();
\r
328 this._ended = true;
\r
332 // SilverlightAudio.loop
\r
333 loop : function(value){ // @param Boolean: true is loop
\r
334 // @return Boolean/void: true is loop
\r
335 if (value === undefined) {
\r
338 this._loop = value;
\r
341 // SilverlightAudio.state
\r
342 state : function(){ // @return Hash: { loop, error, paused, ended, source, duration }
\r
345 error : this._error,
\r
346 paused : this._paused,
\r
347 ended : this._ended,
\r
348 source : this._source,
\r
349 duration : this._duration
\r
353 // SilverlightAudio.volume
\r
354 volume : function(value){ // @param Number: 0.0 ~ 1.0
\r
355 // @return Number/void:
\r
356 if (value === undefined) {
\r
357 return this._volume;
\r
359 this._volume = value;
\r
360 this._rawObject.Volume = value;
\r
363 // SilverlightAudio.startTime
\r
364 startTime : function(time){ // @param Number: time
\r
365 // @return Number/void:
\r
366 if (time === undefined) {
\r
367 return this._startTime;
\r
369 this._startTime = time;
\r
372 // SilverlightAudio.currentTime
\r
373 currentTime : function(time){ // @param Number: time
\r
374 // @return Number/void:
\r
375 var position = this._rawObject.Position; // [!] create instance
\r
377 if (time === undefined) {
\r
378 return position.Seconds;
\r
381 position.Seconds = time; // set current time
\r
383 this._rawObject.Position = position; // [!] reattach instance
\r
386 // SilverlightAudio.isPlaying
\r
387 isPlaying : function(){ // @return Boolean:
\r
388 if (!this._error && !this._paused && !this._ended) {
\r