X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;ds=sidebyside;f=0.6.x%2Fjs%2F07_audio%2F02_XHTMLAudio.js;h=03f8ccabd881c09dd68b42e66da070a1c4655ba8;hb=9e04e2a1221a92960a74e550bc3fd809caf3cd1c;hp=2c64eeb2652bc8a23187ef1f86d149f798f8410c;hpb=86581b41630966e02d93bc37bc221c5fc55f5322;p=pettanr%2FclientJs.git diff --git a/0.6.x/js/07_audio/02_XHTMLAudio.js b/0.6.x/js/07_audio/02_XHTMLAudio.js index 2c64eeb..03f8cca 100644 --- a/0.6.x/js/07_audio/02_XHTMLAudio.js +++ b/0.6.x/js/07_audio/02_XHTMLAudio.js @@ -60,17 +60,20 @@ var */ X_HTMLAudio_volumeEnabled = !( X_UA[ 'WinPhone' ] && X_UA[ 'IE9' ] ) && !X_UA[ 'Opera' ], // Gecko PC + Android でseek時に再生がしばしば止まる問題の修正 - X_HTMLAudio_needPlayForSeek = X_UA[ 'Gecko' ], + X_HTMLAudio_needPlayForSeek = X_UA[ 'iOS' ] || X_UA[ 'Gecko' ], // X_HTMLAudio_pauseFix = 12 <= X_UA[ 'Opera' ] && 0 < ' XP XPSP2 2003|XP64'.indexOf( X_UA[ 'Windows' ] ), // XP + Opera12 のみ? X_HTMLAudio_need1stTouch = X_UA[ 'iOS' ] || 4.2 <= X_UA[ 'AOSP' ] || X_UA[ 'ChromeWV' ] || X_UA[ 'WinPhone' ] || ( X_UA[ 'Blink' ] && X_UA[ 'Android' ] ), + + X_HTMLAudio_playTrigger = ( X_UA[ 'WinPhone' ] && X_UA[ 'IE9' ] ) ? 'canplay' : X_UA[ 'iOS' ] ? 'suspend' : X_UA[ 'Blink' ] < 32 ? 'stalled' : 'canplaythrough', + + X_HTMLAudio_durationFix = X_UA[ 'iOS' ] || X_UA[ 'ChromeWV' ] || ( X_UA[ 'WinPhone' ] && X_UA[ 'IE9' ] ) || + ( X_UA[ 'Windows' ] && 12 <= X_UA[ 'Opera' ] ) || ( X_UA[ 'Blink' ] < 36 && X_UA[ 'Android' ] ), + + X_HTMLAudio_shortPlayFix = X_UA[ 'AOSP' ], - X_HTMLAudio_playTrigger = ( X_UA[ 'WinPhone' ] && X_UA[ 'IE9' ] ) ? 'canplay' : X_UA[ 'iOS' ] ? 'suspend' : X_UA[ 'Blink' ] ? 'stalled' : 'canplaythrough', - - X_HTMLAudio_durationFix = X_UA[ 'iOS' ] || X_UA[ 'ChromeWV' ] || ( X_UA[ 'WinPhone' ] && X_UA[ 'IE9' ] ) || ( X_UA[ 'Windows' ] && 12 <= X_UA[ 'Opera' ] ), - - X_HTMLAudio_shortPlayFix = X_UA[ 'AOSP' ]; // Android 4.1.1 でも遭遇 + X_HTMLAudio_progressEnabled = !( X_UA[ 'Opera' ] && X_UA[ 'Android' ] ) && !( X_UA[ 'WinPhones' ] && X_UA[ 'IE9' ] ); // Android 4.1.1 でも遭遇 if( X_Audio_constructor ){ @@ -78,8 +81,6 @@ if( X_Audio_constructor ){ 'X.HTMLAudio', X_Class.POOL_OBJECT, { - _closed : true, - // 1: canplaythrought|timeupdateに達している、またはdurationFixが終了している // 2: READY イベント発火済 _readyState : 0, @@ -111,7 +112,6 @@ if( X_Audio_constructor ){ var raw; this.disatcher = disatcher || this; - this._closed = false; this._src = source; if( X_HTMLAudio_shortPlayFix ){ @@ -133,7 +133,6 @@ if( X_Audio_constructor ){ X_elmBody.appendChild( raw ); } else { raw = X_TEMP.rawAudio || new X_Audio_constructor( '' ); - // raw.loop = false; // loop を使えば ended で止まること回避できるかも 但し ended イベントが起きなくなる if( X_TEMP.rawAudio ) delete X_TEMP.rawAudio; }; @@ -160,14 +159,18 @@ if( X_Audio_constructor ){ 'playing', 'waiting', 'seeking', 'durationchange', 'timeupdate', 'ended' ], this.onDebug ); + if( X_HTMLAudio_endedFixAOSP2 || X_HTMLAudio_endedFixAOSP4 ){ + raw.loop = true; // loop を使えば ended で止まること回避できるかも 但し ended イベントが起きなくなる + }; + if( X_HTMLAudio_need1stTouch ){ raw.src = source; } else { // if( this.autoplay ){ raw.preload = 'auto'; raw.autoplay = true; // Android 4.0-4.1.x で必要 + //raw.autobuffer = true; //}; - //raw.autobuffer = true; raw.src = source; raw.load(); // Android4.1.1 HTL21 では必要! }; @@ -177,35 +180,30 @@ if( X_Audio_constructor ){ this.disatcher[ 'dispatch' ]( { type : X_EVENT_DEBUG, 'rawEvent' : e.type, - current : this[ '_rawObject' ].currentTime, + 'current' : this[ '_rawObject' ].currentTime, duration : this[ '_rawObject' ].duration } ); }, handleEvent : function( e ){ - if( !e || !e.type ) alert( 888 ); - var raw = this[ '_rawObject' ], actualEnded = e.type === 'ended', ended = actualEnded, + i, l, buf, time, ready, eventType, duration, end, now; - if( this._closed ) return; - - // global に公開 - //window[ '__rawAudio' ] = this[ '_rawObject' ]; - - e.type !== 'timeupdate' && console.log( ' > ' + e.type ); + if( !raw ) return; + + //e.type !== 'timeupdate' && console.log( ' > ' + e.type ); switch( e.type ){ case X_EVENT_KILL_INSTANCE : - delete this._closed; this.playing && this.actualPause(); // 【javascript】モバイル向けブラウザでも音を鳴らしたい【WebAudio】 // http://ingaouhou.com/archives/3633 - // ・使い終わったインスタンスはload()しておくとやや安定 + // ・使い終わったインスタンスはload()しておくとやや安定 raw.src = ''; raw.load(); @@ -218,7 +216,14 @@ if( X_Audio_constructor ){ // console.log( e.loaded + ' ' + e.total * 100 + '%' ); // iem9 で常に0 raw.networkState; // opera Android 12 で buffered.end() へのアクセスはエラー try catch も無効、iem9 は常に end(0) = 0 - //console.log( 'buffered.end ' + raw.buffered && raw.buffered.end(0) ); + if( X_HTMLAudio_progressEnabled && this.duration && this._readyState < 2 ){ + buf = raw.buffered; + time = 0; + for( i = 0, l = buf.length; i < l; ++i ){ + time += buf[ 'end' ]( i ) - buf[ 'start' ]( i ); + }; + this.disatcher[ 'dispatch' ]( { type : X_EVENT_PROGRESS, 'percent' : time * 1000 / this.duration } ); + }; break; case 'loadeddata' : // コンテンツの表示を現在の再生位置で初めて行えるようになった場合に発生 @@ -227,7 +232,7 @@ if( X_Audio_constructor ){ this._readyState |= 1; }; case 'canplay' : // 今すぐに再生を再開できるが、バッファリングが不十分でコンテンツを最後まで表示できないと予測している場合に発生 - if( this._durationFixPhase === 1 && !X_HTMLAudio_need1stTouch ){ + if( this._durationFixPhase === 1 && !X_HTMLAudio_need1stTouch ){ // PC Opera12 用 durationFix this._durationFixPhase = 2; this.actualPlay(); raw.currentTime = 0; // Win8 + Opera12 で必要 @@ -254,7 +259,7 @@ if( X_Audio_constructor ){ duration = raw.duration; eventType = X_EVENT_MEDIA_WAITING; } else - if( !X_HTMLAudio_durationFix && X_HTMLAudio_need1stTouch && this._touchState === 3 ){ + if( this._touchState === 3 && !X_HTMLAudio_durationFix ){ this._touchState = 0; this._readyState |= 1; } else @@ -264,12 +269,12 @@ if( X_Audio_constructor ){ if( this.playing ){ end = X_Audio_getEndTime( this ) + this._shortPlayFixTime; //console.log( now + ' / ' + end ); - // || now < this._lastCurrentTime // loop した場合 - if( 0 + end <= 0 + now ){ // 0+ なぜか iem9 で必要,,, + if( ( 0 + end <= 0 + now ) || // 0+ なぜか iem9 で必要,,, + ( now < this._lastCurrentTime ) ){ // loop した場合 if( this.autoLoop ){ console.log( '☆★☆ 曲の最後に到達 @timeupdate now-end:' + ( now - end ) ); ended = true; - if( X_HTMLAudio_endedFixIOS ) actualEnded = true; + //if( X_HTMLAudio_endedFixIOS ) actualEnded = true; } else { this.actualPause(); eventType = X_EVENT_MEDIA_ENDED; @@ -318,22 +323,21 @@ if( X_Audio_constructor ){ }; // duration は Infinity, NaN, 0 の場合があるため、これを除外する - if( 0 < duration && X_Type_isFinite( duration ) ){ + // chrome18 for Android は duration = 100 の間はシークができない? 28 は可能 + if( 0 < duration && X_Type_isFinite( duration ) && duration !== 100 ){ this.duration = duration * 1000; if( this._durationFixPhase === 4 ){ console.log( '▼ DurationFix の終了 @' + e.type ); this._durationFixPhase = 8; - if( this.autoplay ){ + if( this.autoplay || this._playReserved ){ console.log( '☆ 再生 <- DurationFix の終了' ); this.actualPlay(); } else if( X_HTMLAudio_pauseFix ){ console.log( '☆ PAUSE <- DurationFix の終了' ); - //raw.src = ''; - //raw.load(); - this.actualPause(); + this.actualPause(); }; } else if( this._durationFixPhase & 3 ){ // === 1 | 2 @@ -353,10 +357,7 @@ if( X_Audio_constructor ){ if( !( this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_BEFORE_LOOP ) & X_CALLBACK_PREVENT_DEFAULT ) ){ this.looped = true; this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_LOOPED ); - ( X_HTMLAudio_endedFixAOSP3 || X_HTMLAudio_endedFixAOSP4 || X_HTMLAudio_endedFixCWV || X_HTMLAudio_endedFixIOS ) && actualEnded && console.log( '☆★☆ 音声の継続用の play() @ended' ); - this.actualPlay( - ( X_HTMLAudio_endedFixAOSP4 || X_HTMLAudio_endedFixCWV || X_HTMLAudio_endedFixIOS ) && actualEnded, - ( X_HTMLAudio_endedFixAOSP3 || X_HTMLAudio_endedFixAOSP2 ) && actualEnded ); + this.actualPlay( X_HTMLAudio_endedFixCWV && actualEnded, X_HTMLAudio_endedFixAOSP3 && actualEnded ); }; } else { this.seekTime = 0; @@ -365,9 +366,8 @@ if( X_Audio_constructor ){ }; } else if( this._readyState === 1 && this.duration ){ - this._readyState = 3; + this._readyState = 2; this.disatcher[ 'asyncDispatch' ]( X_EVENT_READY ); - this.autoplay && X_Timer_once( 16, this, this.actualPlay ); console.log( '> Audio Loaded!! ' + e.type + ' d:' + ( this.duration | 0 ) ); } else if( eventType ){ @@ -379,13 +379,20 @@ if( X_Audio_constructor ){ var raw = this[ '_rawObject' ], e, begin, end; - if( this._closed ) return; + if( !raw ) return; - if( !raw.src ){ // X_HTMLAudio_pauseFix によって src が空になっている - console.log( '○ 削除された audio.src の復帰' ); - raw.src = this._src; - return; - }; + this._playReserved = true; + + if( X_HTMLAudio_pauseFix ){ + if( !raw.src ){ // X_HTMLAudio_pauseFix によって src が空になっている + console.log( '○ 削除された audio.src の復帰' ); + raw.src = this._src; + return; + }; + if( this._durationFixPhase < 2 ){ + return; + }; + }; if( this._touchState === 2 ){ e = X_EventDispatcher_CURRENT_EVENTS[ X_EventDispatcher_CURRENT_EVENTS.length - 1 ]; @@ -395,11 +402,12 @@ if( X_Audio_constructor ){ }; this._touchState = 3; } else - if( this._readyState !== 3 && this._durationFixPhase < 2 ){ - this.autoplay = true; + if( this._readyState !== 2 && this._durationFixPhase < 2 ){ return; }; + delete this._playReserved; + if( this._durationFixPhase & 3 ){ // 1 or 2 console.log( '▲ DurationFix の開始' ); this._durationFixPhase = 4; @@ -431,7 +439,6 @@ if( X_Audio_constructor ){ } else if( X_HTMLAudio_needPlayForSeek || forcePlay ){ raw.play(); - console.log( '[HTMLAudio] currentTime より先.' ); }; //http://himaxoff.blog111.fc2.com/blog-entry-97.html @@ -444,15 +451,12 @@ if( X_Audio_constructor ){ console.log( '[HTMLAudio] play ' + begin + ' -> ' + end + ' crt:' + ( raw.currentTime | 0 ) + ' last:' + this._lastCurrentTime ); - if( forceReload || ( X_HTMLAudio_endedFixAOSP4 && raw.duration && raw.currentTime === raw.duration ) ){ - raw.src = ''; - raw.src = this._src; + if( forceReload ){ this.playing = false; this._endedFixON = true; + raw.src = this._src; console.log( '△ onEndedFix の開始' ); - raw.currentTime = this._lastCurrentTime; this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_WAITING ); - X_HTMLAudio_endedFixAOSP2 && raw.load(); }; }; @@ -466,8 +470,6 @@ if( X_Audio_constructor ){ var raw = this[ '_rawObject' ]; console.log( '[HTMLAudio] pause' ); - - this.seekTime = this.getActualCurrentTime(); delete this._currentFixStart;