2 * original : uupaa-js SilverlightAudio.js
\r
3 * https://code.google.com/p/uupaa-js/source/browse/trunk/0.8/src/Audio/SilverlightAudio.js?r=568
\r
7 * Silverlight 4 → 5における不具合の状況
\r
8 * http://www.slideshare.net/wakabayashiy/silverlight-4-5
\r
10 * SilverlLight5 ie6&7(ietester,winxp), ie8(winxp) で動作確認。firefox32 では動作しない。(4以下の方がよい?)
\r
12 var X_Audio_SLAudio, X_Audio_SLAudioWrapper,
\r
13 X_Audio_SLAudio_uid = 0,
\r
14 X_Audio_SLAudio_LIVE_LIST = [],
\r
15 X_Audio_SLAudio_POOL_LIST = [];
\r
17 if( X.Pulgin.SilverlightEnabled ){
\r
19 function getSLAudioWrapper( proxy ){
\r
20 var i = X_Audio_SLAudio_LIVE_LIST.length;
\r
22 if( X_Audio_SLAudio_LIVE_LIST[ --i ].proxy === proxy ) return X_Audio_SLAudio_LIVE_LIST[ i ];
\r
26 X_Audio_SLAudio = X.Class._override(
\r
27 new X.EventDispatcher(),
\r
29 backendName : 'Silverlight Audio',
\r
31 detect : function( source, ext ){
\r
32 var ok = ext === 'mp3' || ext === 'wma';
\r
34 this.asyncDispatch( 0, ok ? 'support' : 'nosupport' );
\r
39 register : function( proxy, source, option ){
\r
40 X_Audio_SLAudio_LIVE_LIST.push( new X_Audio_SLAudioWrapper( proxy, source, option ) );
\r
44 return getSLAudioWrapper( this ).close();
\r
47 play : function( position ){
\r
48 return getSLAudioWrapper( this ).play( position );
\r
52 return getSLAudioWrapper( this ).pause();
\r
56 return getSLAudioWrapper( this ).stop();
\r
59 loop : function( v ){
\r
60 return getSLAudioWrapper( this ).loop( v );
\r
64 return getSLAudioWrapper( this ).state();
\r
67 volume : function( v ){
\r
68 return getSLAudioWrapper( this ).volume( v );
\r
71 startTime : function( time ){
\r
72 return getSLAudioWrapper( this ).startTime( time );
\r
75 currentTime : function( time ){
\r
76 return getSLAudioWrapper( this ).currentTime( time );
\r
79 isPlaying : function(){
\r
80 return getSLAudioWrapper( this ).isPlaying();
\r
85 X_Audio_BACKENDS.push( X_Audio_SLAudio );
\r
91 X_Audio_SLAudioWrapper = X.EventDispatcher.inherits(
\r
92 'X.AV.SilverlightAudioWrapper',
\r
93 X.Class.POOL_OBJECT,
\r
95 _isSilverlight : true, // for X.EventDispatcher.listen
\r
108 _lastUserAction : '',
\r
111 _interval : 0, // setInterval timer id
\r
113 Constructor : function( proxy, source, option ){
\r
115 if( !X_Audio_SLAudio_uid ){
\r
117 //X.Dom.Node._systemNode.create( 'script', { type : 'text/xaml', id : 'silverlightaudio' } )
\r
118 // .text( '<Canvas xmlns="http://schemas.microsoft.com/client/2007" ' +
\r
119 // 'xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></Canvas>');
\r
122 // <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
125 * [Silverlight 2]JavaScriptコードからSilverlightのオブジェクトを利用するには?[C#、VB]
\r
126 * http://www.atmarkit.co.jp/fdotnet/dotnettips/902slobjcallfromjs/slobjcallfromjs.html
\r
127 * このページのサンプルは sl5+firefox32 環境で動いている。xaml を js から利用する形ではなく、.xap を sl4 以下で作るのがよさそう.
\r
129 this.proxy = proxy;
\r
130 this._onload = 'XAudioSilverlightOnLoad';// + ( ++X_Audio_SLAudio_uid );
\r
131 this._callback = window[ this._onload ] = X.Callback.create( this, this.onSLReady, [ option.autoplay ] );
\r
132 this.xnodeObject = X.Dom.Node._body
\r
133 .create( 'object', {
\r
134 type : 'application/x-silverlight-2',
\r
135 data : 'data:application/x-silverlight-2,',
\r
140 '<param name="background" value="#00000000">' + // transparent
\r
141 '<param name="windowless" value="true">' +
\r
142 '<param name="source" value="#silverlightaudio">' + // XAML ID
\r
143 '<param name="onload" value="' + this._onload + '">'// + // bond to global
\r
144 //'<param value="2.0.31005.0" name="minRuntimeVersion">' +
\r
145 //'<param value="true" name="autoUpgrade">' +
\r
146 //'<param name="onerror" value="slerror">' // bond to global
\r
149 this._loop = option.loop;
\r
150 this._source = source;
\r
151 if( option.volume ) this._volume = option.volume;
\r
152 if( option.startTime ) this._startTime = option.startTime;
\r
154 this.listenOnce( X.Event.KILL_INSTANCE );
\r
157 onSLReady : function( sender, autoplay ){
\r
158 if( !this._onload ) return;
\r
160 window[ this._onload ] = null;
\r
161 delete this._onload;
\r
162 this._callback.kill();
\r
163 delete this._callback;
\r
165 sender.children.add(
\r
169 '<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">' +
\r
170 '<MediaElement x:Name="media" Source="' + this._source + '" Volume="' + this._volume + '" AutoPlay="false" />' +
\r
173 this._rawObject = sender.findName('media'); // x:Name='media'
\r
175 this.listen( [ 'MediaFailed', 'MediaOpened', 'MediaEnded', 'CurrentStateChanged' ] );
\r
177 autoplay && X.Timer.once( 100, this, this.play );
\r
180 handleEvent : function( e ){
\r
181 var lastState, currentState;
\r
185 case 'MediaFailed' :
\r
187 this._ended = true;
\r
188 this._paused = false;
\r
190 this.proxy.dispatch( 'error' ); // open failed
\r
192 case 'MediaOpened' :
\r
193 // http://msdn.microsoft.com/ja-jp/library/bb979710(VS.95).aspx
\r
194 this._duration = this._rawObject.NaturalDuration.Seconds;
\r
196 this.proxy.dispatch( 'canplay' );
\r
198 case 'MediaEnded' :
\r
199 this.currentTime(this._startTime);
\r
202 this._rawObject.play();
\r
205 case 'CurrentStateChanged' :
\r
206 lastState = this._lastState,
\r
207 currentState = this._rawObject.CurrentState;
\r
209 // ignore consecutive events or 'Closed' == 'Error'
\r
210 if( lastState === currentState
\r
211 || ( (lastState === 'Closed' || lastState === 'Error') && ( currentState === 'Closed' || currentState === 'Error') ) ){
\r
214 this._lastState = currentState; // update last state
\r
216 switch (currentState) {
\r
221 // media.play(none supported file) -> 'Error'
\r
222 // media.play(file not found) -> 'Closed'
\r
223 // media.load -> 'Error'
\r
227 this._ended = true;
\r
228 this._paused = false;
\r
229 this.proxy.dispatch( 'error' );
\r
232 // userAction.pause() -> MediaState('Paused') -> x
\r
233 // userAction.stop() -> MediaState('Paused') -> x
\r
234 // userAction.play() + file end -> MediaState('Paused') -> uueventfire('ended')
\r
236 switch (this._lastUserAction) {
\r
237 case 'play': // play() -> file end -> event('ended')
\r
238 this._ended = true;
\r
239 this._paused = false;
\r
240 this.proxy.dispatch( 'ended' );
\r
241 this.currentTime(this._startTime);
\r
244 this._ended = false;
\r
245 this._paused = true;
\r
248 this._ended = true;
\r
249 this._paused = false;
\r
253 // media.play -> 'Playing'
\r
256 this._ended = false;
\r
257 this._paused = false;
\r
258 this.proxy.dispatch( 'playing' );
\r
263 this._ended = true;
\r
264 this._paused = false;
\r
265 this.currentTime(this._startTime);
\r
269 case X.Event.KILL_INSTANCE :
\r
270 if( this._onload ){
\r
271 window[ this._onload ] = null;
\r
272 this._callback.kill();
\r
274 this.xnodeObject.destroy();
\r
279 close : function(){
\r
280 if (this._interval) {
\r
281 X.Timer.remove( this._interval );
\r
282 delete this._interval;
\r
284 this.proxy.dispatch( 'ended' );
\r
288 // SilverlightAudio.play
\r
290 if (!this._error) {
\r
291 this._lastUserAction = 'play';
\r
293 this.currentTime(this._startTime);
\r
296 this._rawObject.play();
\r
297 this.proxy.dispatch( 'play' );
\r
299 if (!this._interval) {
\r
300 this._interval = X.Timer.add( 1000, 0, this, this._onInterval );
\r
305 _onInterval : function(){
\r
306 this.isPlaying() && this.proxy.dispatch( 'timeupdate' );
\r
309 // SilverlightAudio.pause
\r
310 pause : function(){
\r
311 if (!this._error) {
\r
312 this._lastUserAction = 'pause';
\r
313 this._paused = true;
\r
314 this._ended = false;
\r
315 this._rawObject.pause();
\r
316 this.proxy.dispatch( 'pause' );
\r
320 // SilverlightAudio.stop
\r
322 if (!this._error) {
\r
323 this._lastUserAction = 'stop';
\r
324 this._rawObject.stop();
\r
325 this._ended = true;
\r
329 // SilverlightAudio.loop
\r
330 loop : function(value){ // @param Boolean: true is loop
\r
331 // @return Boolean/void: true is loop
\r
332 if (value === undefined) {
\r
335 this._loop = value;
\r
338 // SilverlightAudio.state
\r
339 state : function(){ // @return Hash: { loop, error, paused, ended, source, duration }
\r
342 error: this._error,
\r
343 paused: this._paused,
\r
344 ended: this._ended,
\r
345 source: this._source,
\r
346 duration: this._duration
\r
350 // SilverlightAudio.volume
\r
351 volume : function(value){ // @param Number: 0.0 ~ 1.0
\r
352 // @return Number/void:
\r
353 if (value === undefined) {
\r
354 return this._volume;
\r
356 this._volume = value;
\r
357 this._rawObject.Volume = value;
\r
360 // SilverlightAudio.startTime
\r
361 startTime : function(time){ // @param Number: time
\r
362 // @return Number/void:
\r
363 if (time === undefined) {
\r
364 return this._startTime;
\r
366 this._startTime = time;
\r
369 // SilverlightAudio.currentTime
\r
370 currentTime : function(time){ // @param Number: time
\r
371 // @return Number/void:
\r
372 var position = this._rawObject.Position; // [!] create instance
\r
374 if (time === undefined) {
\r
375 return position.Seconds;
\r
378 position.Seconds = time; // set current time
\r
380 this._rawObject.Position = position; // [!] reattach instance
\r
383 // SilverlightAudio.isPlaying
\r
384 isPlaying : function(){ // @return Boolean:
\r
385 if (!this._error && !this._paused && !this._ended) {
\r