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( 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 //if( sender.findName('media') ) alert( 'exist' );
\r
170 sender.children.add(
\r
174 '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">' +
\r
175 '<MediaElement x:Name="media" Source="' + this._source + '" Volume="' + this._volume + '" AutoPlay="false" />' +
\r
178 this._rawObject = sender.findName('media'); // x:Name='media'
\r
180 this.listen( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] );
\r
182 autoplay && X.Timer.once( 100, this, this.play );
\r
185 handleEvent : function( e ){
\r
186 var lastState, currentState;
\r
190 case 'MediaFailed' :
\r
192 this._ended = true;
\r
193 this._paused = false;
\r
195 this.proxy.dispatch( 'error' ); // open failed
\r
197 case 'MediaOpened' :
\r
198 // http://msdn.microsoft.com/ja-jp/library/bb979710(VS.95).aspx
\r
199 this._duration = this._rawObject.NaturalDuration.Seconds;
\r
200 // TODO 'canplaythrough'
\r
201 this.proxy.dispatch( 'canplay' );
\r
203 case 'MediaEnded' :
\r
204 this.currentTime(this._startTime);
\r
207 this._rawObject.play();
\r
210 case 'CurrentStateChanged' :
\r
211 lastState = this._lastState,
\r
212 currentState = this._rawObject.CurrentState;
\r
214 // ignore consecutive events or 'Closed' == 'Error'
\r
215 if( lastState === currentState
\r
216 || ( (lastState === 'Closed' || lastState === 'Error') && ( currentState === 'Closed' || currentState === 'Error') ) ){
\r
219 this._lastState = currentState; // update last state
\r
221 switch (currentState) {
\r
226 // media.play(none supported file) -> 'Error'
\r
227 // media.play(file not found) -> 'Closed'
\r
228 // media.load -> 'Error'
\r
232 this._ended = true;
\r
233 this._paused = false;
\r
234 this.proxy.dispatch( 'error' );
\r
237 // userAction.pause() -> MediaState('Paused') -> x
\r
238 // userAction.stop() -> MediaState('Paused') -> x
\r
239 // userAction.play() + file end -> MediaState('Paused') -> uueventfire('ended')
\r
241 switch (this._lastUserAction) {
\r
242 case 'play': // play() -> file end -> event('ended')
\r
243 this._ended = true;
\r
244 this._paused = false;
\r
245 this.proxy.dispatch( 'ended' );
\r
246 this.currentTime(this._startTime);
\r
249 this._ended = false;
\r
250 this._paused = true;
\r
253 this._ended = true;
\r
254 this._paused = false;
\r
258 // media.play -> 'Playing'
\r
261 this._ended = false;
\r
262 this._paused = false;
\r
263 this.proxy.dispatch( 'playing' );
\r
268 this._ended = true;
\r
269 this._paused = false;
\r
270 this.currentTime(this._startTime);
\r
274 case X.Event.KILL_INSTANCE :
\r
275 if( this._onload ){
\r
276 window[ this._onload ] = null;
\r
277 this._callback.kill();
\r
279 this.xnodeObject.destroy();
\r
284 close : function(){
\r
285 if (this._interval) {
\r
286 X.Timer.remove( this._interval );
\r
287 delete this._interval;
\r
289 this.proxy.dispatch( 'ended' );
\r
293 // SilverlightAudio.play
\r
295 if (!this._error) {
\r
296 this._lastUserAction = 'play';
\r
298 this.currentTime(this._startTime);
\r
301 this._rawObject.play();
\r
302 this.proxy.dispatch( 'play' );
\r
304 if (!this._interval) {
\r
305 this._interval = X.Timer.add( 1000, 0, this, this._onInterval );
\r
310 _onInterval : function(){
\r
311 this.isPlaying() && this.proxy.dispatch( 'timeupdate' );
\r
314 // SilverlightAudio.pause
\r
315 pause : function(){
\r
316 if (!this._error) {
\r
317 this._lastUserAction = 'pause';
\r
318 this._paused = true;
\r
319 this._ended = false;
\r
320 this._rawObject.pause();
\r
321 this.proxy.dispatch( 'pause' );
\r
325 // SilverlightAudio.stop
\r
327 if (!this._error) {
\r
328 this._lastUserAction = 'stop';
\r
329 this._rawObject.stop();
\r
330 this._ended = true;
\r
334 // SilverlightAudio.loop
\r
335 loop : function(value){ // @param Boolean: true is loop
\r
336 // @return Boolean/void: true is loop
\r
337 if (value === undefined) {
\r
340 this._loop = value;
\r
343 // SilverlightAudio.state
\r
344 state : function(){ // @return Hash: { loop, error, paused, ended, source, duration }
\r
347 error : this._error,
\r
348 paused : this._paused,
\r
349 ended : this._ended,
\r
350 source : this._source,
\r
351 duration : this._duration
\r
355 // SilverlightAudio.volume
\r
356 volume : function(value){ // @param Number: 0.0 ~ 1.0
\r
357 // @return Number/void:
\r
358 if (value === undefined) {
\r
359 return this._volume;
\r
361 this._volume = value;
\r
362 this._rawObject.Volume = value;
\r
365 // SilverlightAudio.startTime
\r
366 startTime : function(time){ // @param Number: time
\r
367 // @return Number/void:
\r
368 if (time === undefined) {
\r
369 return this._startTime;
\r
371 this._startTime = time;
\r
374 // SilverlightAudio.currentTime
\r
375 currentTime : function(time){ // @param Number: time
\r
376 // @return Number/void:
\r
377 var position = this._rawObject.Position; // [!] create instance
\r
379 if (time === undefined) {
\r
380 return position.Seconds;
\r
383 position.Seconds = time; // set current time
\r
385 this._rawObject.Position = position; // [!] reattach instance
\r
388 // SilverlightAudio.isPlaying
\r
389 isPlaying : function(){ // @return Boolean:
\r
390 if (!this._error && !this._paused && !this._ended) {
\r