X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=0.6.x%2Fjs%2F07_audio%2F02_XHTMLAudio.js;h=1c88ae5cb9c1a49f8744289864f1a46e561c283d;hb=4e4ab3be10850546063d4a4b93250ed142bb8cd2;hp=03f8ccabd881c09dd68b42e66da070a1c4655ba8;hpb=9e04e2a1221a92960a74e550bc3fd809caf3cd1c;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 03f8cca..1c88ae5 100644 --- a/0.6.x/js/07_audio/02_XHTMLAudio.js +++ b/0.6.x/js/07_audio/02_XHTMLAudio.js @@ -39,8 +39,9 @@ var X_HTMLAudio, - - // ended が発生しない timeupdate 内で play() を呼ぶ (未検証) 不具合確認は iOS4,6 + // iOS7.1, 8.3 で確認.seeking -> seeked の間の currentTime の値が全くあてにならないので無視する。 + X_HTMLAudio_seekingFixIOS = 7 <= X_UA[ 'iOS' ], + // ended が発生しない timeupdate 内で play() を呼ぶ (未検証) 不具合確認は iOS4,6 iOS7.1,8.3ではpause->ended起きてる 但し iOS7.1 でも 6 と同じ症状になることがある X_HTMLAudio_endedFixIOS = X_UA[ 'iOS' ] < 7, // Android 2.3.5 で ended 時に audio.src='';audio.src=src;audio.load() を実施。 2.3.4 でも問題なし。 X_HTMLAudio_endedFixAOSP2 = X_UA[ 'AOSP' ] < 3, @@ -53,23 +54,29 @@ var // Opera Mobile 12 は 2回目以降の currentTime へのセットで currentTime が更新されなくなるため、タイマーを使用する X_HTMLAudio_currentTimeFix = !!X_UA[ 'OperaMobile' ] || !!X_UA[ 'OperaTablet' ], + // Firefox44.0.2 で音声の再生開始に難あり... + X_HTMLAudio_playStartFix = X_UA[ 'Windows' ] && X_UA[ 'Gecko' ] === 44, X_HTMLAudio_volumeFix = X_UA[ 'Chrome' ], /* * win opera12 volume, mute の変更が2度目以降できない */ X_HTMLAudio_volumeEnabled = !( X_UA[ 'WinPhone' ] && X_UA[ 'IE9' ] ) && !X_UA[ 'Opera' ], - // Gecko PC + Android でseek時に再生がしばしば止まる問題の修正 + // Gecko PC + Android でseek時に再生がしばしば止まる問題の修正、iOS8でも確認 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_playTrigger = ( X_UA[ 'WinPhone' ] && X_UA[ 'IE9' ] ) ? 'canplay' : + X_UA[ 'iOS' ] < 8 ? 'suspend' : // iOS7.x以下 + X_UA[ 'iOS' ] ? 'loadedmetadata' : // iOS8以上は + 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_durationFix = // iOS8.1(シュミレータでは不要) + X_UA[ 'iOS' ] < 8 || 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' ], @@ -83,6 +90,7 @@ if( X_Audio_constructor ){ { // 1: canplaythrought|timeupdateに達している、またはdurationFixが終了している // 2: READY イベント発火済 + // 3: 1, 2 が済 _readyState : 0, _src : '', @@ -101,13 +109,15 @@ if( X_Audio_constructor ){ // 8:duration 取得済 // :timeupdate イベントで durationFixは完了 _durationFixPhase : X_HTMLAudio_durationFix ? 1 : 0, - _lastCurrentTime : 0, + _lastCurrentTime : 0, // ms _shortPlayFixON : false, _shortPlayFixTime : 0, _endedFixON : false, + _seekingFixON : false, + 'Constructor' : function( disatcher, source, option ){ var raw; @@ -145,7 +155,8 @@ if( X_Audio_constructor ){ //'loadstart', 'load', 'progress', //'error', // 'suspend', 'abort', 'emptied', 'stalled', - // 'play', 'pause', 'seeked', 'ratechange', 'volumechange', + // 'play', 'pause', 'ratechange', 'volumechange', + 'seeked', 'loadedmetadata', 'loadeddata', 'canplay', 'canplaythrough', 'playing', 'waiting', 'seeking', 'durationchange', 'timeupdate', 'ended' ] ); @@ -160,17 +171,17 @@ if( X_Audio_constructor ){ 'durationchange', 'timeupdate', 'ended' ], this.onDebug ); if( X_HTMLAudio_endedFixAOSP2 || X_HTMLAudio_endedFixAOSP4 ){ - raw.loop = true; // loop を使えば ended で止まること回避できるかも 但し ended イベントが起きなくなる + raw.loop = true; // loop を使えば ended で止まること回避できる 但し ended イベントが起きなくなる }; if( X_HTMLAudio_need1stTouch ){ raw.src = source; } else { - // if( this.autoplay ){ + if( this.autoplay ){ raw.preload = 'auto'; raw.autoplay = true; // Android 4.0-4.1.x で必要 //raw.autobuffer = true; - //}; + }; raw.src = source; raw.load(); // Android4.1.1 HTL21 では必要! }; @@ -194,7 +205,7 @@ if( X_Audio_constructor ){ if( !raw ) return; - //e.type !== 'timeupdate' && console.log( ' > ' + e.type ); + // e.type !== 'timeupdate' && console.log( ' > ' + e.type ); switch( e.type ){ @@ -216,13 +227,12 @@ 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 - if( X_HTMLAudio_progressEnabled && this.duration && this._readyState < 2 ){ + if( X_HTMLAudio_progressEnabled && this.duration && this._readyState < 3 ){ buf = raw.buffered; - time = 0; - for( i = 0, l = buf.length; i < l; ++i ){ + for( i = time = 0, l = buf && buf.length; i < l; ++i ){ time += buf[ 'end' ]( i ) - buf[ 'start' ]( i ); }; - this.disatcher[ 'dispatch' ]( { type : X_EVENT_PROGRESS, 'percent' : time * 1000 / this.duration } ); + this.disatcher[ 'dispatch' ]( { type : X_EVENT_PROGRESS, 'percent' : time * 1000 / this.duration * 100 } ); }; break; @@ -251,6 +261,9 @@ if( X_Audio_constructor ){ // TODO firefox で 短い音声でtimeupdate, ended が発火しない <- 最後の音に無音部分を追加する case 'timeupdate' : // 通常の再生が行われ現在の再生位置の変化が起こった場合に発生 + if( this._seekingFixON ){ + eventType = X_EVENT_MEDIA_SEEKING; + } else if( this._durationFixPhase === 8 ){ this._durationFixPhase = 0; this._readyState |= 1; @@ -266,13 +279,26 @@ if( X_Audio_constructor ){ if( ( now = this.getActualCurrentTime() ) === this._lastCurrentTime ){ eventType = X_EVENT_MEDIA_WAITING; } else + if( X_HTMLAudio_playStartFix && ( now < this._lastCurrentTime ) ){ + eventType = X_EVENT_MEDIA_WAITING; + //console.log( '@Firefox44.0.2 !! ' + now + ' / ' + this._lastCurrentTime ); + // Firefox44.0.2 で音声の再生開始に難あり... + // 20回程度 currentTime の更新に失敗する.... + this.actualPlay(); + } else if( this.playing ){ end = X_Audio_getEndTime( this ) + this._shortPlayFixTime; - //console.log( now + ' / ' + end ); + + //console.log( '@ ' + now + ' / ' + this._lastCurrentTime + ' / ' + end );// Firefox44.0.2 で音声の再生開始に難あり... + if( ( 0 + end <= 0 + now ) || // 0+ なぜか iem9 で必要,,, - ( now < this._lastCurrentTime ) ){ // loop した場合 + ( now < this._lastCurrentTime && now < 2000 ) ){ + //( ( X_HTMLAudio_endedFixAOSP2 || X_HTMLAudio_endedFixAOSP4 ) && ( now < this._lastCurrentTime && now < 1000 ) ) ){ + // loop して0付近に戻った場合 + // iOS8.4 ではこのタイミングで now が last より 0.1秒後退している場合がある + // iOS7.1 ではもっと小さい場合がある,,, if( this.autoLoop ){ - console.log( '☆★☆ 曲の最後に到達 @timeupdate now-end:' + ( now - end ) ); + console.log( '☆★☆ 曲の最後に到達 @timeupdate now-end:' + ( now - end ) + ' now:' + now + ' last:' + this._lastCurrentTime ); ended = true; //if( X_HTMLAudio_endedFixIOS ) actualEnded = true; } else { @@ -309,17 +335,21 @@ if( X_Audio_constructor ){ eventType = !this._durationFixPhase && !this._endedFixON ? X_EVENT_MEDIA_PLAYING : X_EVENT_MEDIA_WAITING; //case 'play' : // 再生が開始された。play()メソッドからの復帰後に発生する場合に発生 //case 'pause' : // 再生が一時停止された。pauseメソッドからの復帰後に発生する場合に発生 - //case 'seeked' : //case 'ratechange' : // defaultPlaybackRate属性とplaybackRate属性のどちらかが更新された場合に発生 //case 'volumechange' : // volume属性とmuted属性のどちらかが変化した場合に発生 break; + + case 'seeking' : // シークがtrueに変化し、イベントを発生させるのに十分な時間がシーク操作にかかっている場合に発生 + eventType = X_EVENT_MEDIA_SEEKING; + if( X_HTMLAudio_seekingFixIOS ) this._seekingFixON = true; + break; + case 'seeked' : + if( X_HTMLAudio_seekingFixIOS ) this._seekingFixON = false; + break; case 'waiting' : // 次のフレームが利用不可のため再生を停止したが、そのフレームがやがて利用可能になると想定している場合に発生 eventType = X_EVENT_MEDIA_WAITING; break; - case 'seeking' : // シークがtrueに変化し、イベントを発生させるのに十分な時間がシーク操作にかかっている場合に発生 - eventType = X_EVENT_MEDIA_SEEKING; - break; }; // duration は Infinity, NaN, 0 の場合があるため、これを除外する @@ -349,7 +379,7 @@ if( X_Audio_constructor ){ if( this._touchState === 1 ){ if( e.type === X_HTMLAudio_playTrigger ){ this._touchState = 2; - this.disatcher[ 'asyncDispatch' ]( X_EVENT_MEDIA_TOUCH_FOR_LOAD ); + this.disatcher[ 'asyncDispatch' ]( X_EVENT_MEDIA_WAIT_FOR_TOUCH ); }; } else if( ended ){ @@ -366,7 +396,7 @@ if( X_Audio_constructor ){ }; } else if( this._readyState === 1 && this.duration ){ - this._readyState = 2; + this._readyState |= 2; this.disatcher[ 'asyncDispatch' ]( X_EVENT_READY ); console.log( '> Audio Loaded!! ' + e.type + ' d:' + ( this.duration | 0 ) ); } else @@ -377,7 +407,7 @@ if( X_Audio_constructor ){ actualPlay : function( forcePlay, forceReload ){ var raw = this[ '_rawObject' ], - e, begin, end; + begin, end; if( !raw ) return; @@ -395,14 +425,16 @@ if( X_Audio_constructor ){ }; if( this._touchState === 2 ){ - e = X_EventDispatcher_CURRENT_EVENTS[ X_EventDispatcher_CURRENT_EVENTS.length - 1 ]; + //@dev{ + var e = X_EventDispatcher_CURRENT_EVENTS[ X_EventDispatcher_CURRENT_EVENTS.length - 1 ]; if( !e || !e[ 'pointerType' ] ){ alert( 'タッチイベント以外での play! ' + ( e ? e.type : '' ) ); return; }; + //@} this._touchState = 3; } else - if( this._readyState !== 2 && this._durationFixPhase < 2 ){ + if( this._readyState !== 3 && this._durationFixPhase < 2 ){ return; }; @@ -416,7 +448,7 @@ if( X_Audio_constructor ){ end = X_Audio_getEndTime( this ); begin = X_Audio_getStartTime( this, end, true ); - this._lastCurrentTime = begin / 1000; + this._lastCurrentTime = begin; if( this._shortPlayFixON ){ this._shortPlayFixTime = ( 1000 < end - begin ) ? 200 : 400; @@ -447,7 +479,7 @@ if( X_Audio_constructor ){ // iOS で duration が 0 の時に触ると error // 0 or 8 - if( !( this._durationFixPhase % 8 ) && this.duration ) raw.currentTime = this._lastCurrentTime; + if( !( this._durationFixPhase % 8 ) && this.duration ) raw.currentTime = this._lastCurrentTime / 1000; console.log( '[HTMLAudio] play ' + begin + ' -> ' + end + ' crt:' + ( raw.currentTime | 0 ) + ' last:' + this._lastCurrentTime ); @@ -487,6 +519,7 @@ if( X_Audio_constructor ){ getActualCurrentTime : function(){ return ( X_HTMLAudio_currentTimeFix ? X_Timer_now() - this._currentFixStart + this._currentFixBegin : + this._seekingFixON ? this._lastCurrentTime : this[ '_rawObject' ].currentTime * 1000 | 0 ); }, /* @@ -547,7 +580,9 @@ if( X_Audio_constructor ){ * - volume, muted iPhone(iOS4-6)、Android(2.3.6)では動作せず。 * - FireFox3.6, Android 2.3.6については、src変更後、load()を呼び出さないと切り替わらなかった。iPhoneはload()が不要。 */ - detect : function( proxy, source, ext ){ + detect : function( proxy, ext, hash ){ + // TODO hash.CBR + // 得意度で返す proxy[ 'asyncDispatch' ]( { type : X_EVENT_COMPLETE, canPlay : X_Audio_codecs[ ext ] } ); },