OSDN Git Service

Version 0.6.213, add X.Node.TextRange.
[pettanr/clientJs.git] / 0.6.x / js / 07_audio / 01_XWebAudio.js
index e33630b..98eee1b 100644 (file)
@@ -1,8 +1,8 @@
 var X_Audio_constructor = 3.1 <= X_UA[ 'Safari' ] && X_UA[ 'Safari' ] < 4 ?
                                                                function( s, a ){
-                                                                       a = document.createElement( 'audio' );
-                                                                       a.src = s;
-                                                                       a.load();
+                                                                       //a = document.createElement( 'audio' );
+                                                                       //a.src = s;
+                                                                       //a.load();
                                                                        return a;
                                                                } :
                                                // Android1.6 + MobileOpera12 HTMLAudio はいるが呼ぶとクラッシュする
@@ -79,12 +79,18 @@ if( X_Audio_constructor ){
 
 
 var X_WebAudio_context      =  // 4s 以下ではない iPad 2G または iPad mini 1G 以下ではない, iPod touch 4G 以下ではない
-                                                               !X_UA[ 'iPhone_4s' ]  && !X_UA[ 'iPad_2Mini1' ]  && !X_UA[ 'iPod_4' ]  &&
+                                                               !X_UA[ 'iPhone_4s' ] && !X_UA[ 'iPad_2Mini1' ] && !X_UA[ 'iPod_4' ] &&
+                                                               // iOS7 以上で HTML Audio が鳴らない問題を見ていくよ
+                                                               // !X_UA[ 'iOS' ] &&
                                                                // Android2 + Gecko で WebAudio が極めて不安定
                                                                !( X_UA[ 'Fennec' ] && X_UA[ 'Android' ] < 3 ) &&
+                                                               // AOSP でも WebAudio を不完全に実装するものがある, touch の有無も不明のため一律に切ってしまう
+                                                               !X_UA[ 'AOSP' ] && !( X_UA[ 'ChromeWV' ] < 5 ) &&
+                                                               // Blink HTMLAudio 調査用
+                                                               //!X_UA[ 'Blink' ] &&
                                                                // Firefox40.0.5 + Windows8 で音声が途中から鳴らなくなる
                                                                // Firefox41.0.1 + Windows8 で音声が途中から鳴らなくなる
-                                                               !( 40 <= X_UA[ 'Gecko' ] && X_UA[ 'Gecko' ] < 42 && X_UA[ 'Windows' ] ) &&
+                                                               !( 40 <= X_UA[ 'Gecko' ] && X_UA[ 'Gecko' ] < 45 && X_UA[ 'Windows' ] ) &&
                                                                ( window[ 'AudioContext' ] || window[ 'webkitAudioContext' ] ),
        X_WebAudio_BUFFER_LIST  = [],
        X_WebAudio_need1stTouch = X_UA[ 'iOS' ],
@@ -162,7 +168,7 @@ if( X_WebAudio_context ){
                                _onDecodeSuccess : function( buffer ){
                                        this.onDecodeSuccess && this._onDecodeComplete();
                                        
-                       if ( !buffer ) {
+                       if( !buffer ){
                                this.errorState = 2;
                            this[ 'asyncDispatch' ]( X_EVENT_COMPLETE );
                            return;
@@ -194,9 +200,10 @@ if( X_WebAudio_context ){
                        unregister : function( webAudio ){
                                var list = this.webAudioList,
                                        i    = list.indexOf( webAudio );
+
                                if( 0 < i ){
                                        list.splice( i, 1 );
-                                       if( list.length ){
+                                       if( !list.length ){
                                                this.xhr && this.xhr[ 'kill' ]();
                                                this[ 'kill' ]();
                                        };
@@ -217,7 +224,7 @@ if( X_WebAudio_context ){
                        _startPos       : 0,
                        _endPosition    : 0,
                        _startTime      : 0,
-            _timerID        : 0,
+            //_timerID        : 0,
             _interval       : 0,
                audioBuffer     : null,
                bufferSource    : null,
@@ -285,7 +292,6 @@ if( X_WebAudio_context ){
                                        
                        if ( !buffer ) {
                                this.error = loader.errorState;
-                               
                            this.disatcher[ 'dispatch' ]({
                                                                type    : X_EVENT_ERROR,
                                                                error   : loader.errorState,
@@ -300,9 +306,7 @@ if( X_WebAudio_context ){
                        this.audioBuffer = buffer;
                        this.duration    = buffer.duration * 1000;
 
-                                       this.disatcher[ 'asyncDispatch' ]( X_EVENT_READY );
-                       
-                       console.log( 'WebAudio buffer ready' );
+                                       this.disatcher[ 'asyncDispatch' ]( X_WebAudio_touchState ? X_EVENT_MEDIA_TOUCH_FOR_LOAD : X_EVENT_READY );
                                },
                        
                        actualPlay : function(){
@@ -321,6 +325,9 @@ if( X_WebAudio_context ){
                                                // alert( 'タッチイベント以外での play! ' + ( e ? e.type : '' ) );
                                                return;
                                        };
+                                       // http://qiita.com/uupaa/items/e5856e3cb2a9fc8c5507
+                                       // iOS9 + touchstart で呼んでいた場合、 X_ViewPort['listenOnce']('pointerup',this,this.actualPlay())
+                                       this.disatcher[ 'asyncDispatch' ]( X_EVENT_READY );
                                };
                                X_WebAudio_touchState = false;
                                
@@ -347,8 +354,8 @@ if( X_WebAudio_context ){
                        //console.log( '> use onended' );
                        //this.bufferSource.onended = this._onended || ( this._onended = X_Closure_create( this, this._onEnded ) );
                 //} else {
-                       this._timerID && X_Timer_remove( this._timerID );
-                                       this._timerID = X_Timer_once( end - begin, this, this._onEnded );
+                       //this._timerID && X_Timer_remove( this._timerID );
+                                       //this._timerID = X_Timer_once( end - begin, this, this._onEnded );
                 //};
        
                    if( this.bufferSource.start ){
@@ -361,26 +368,28 @@ if( X_WebAudio_context ){
                    this._startPos    = begin;
                    this._endPosition = end;
                    this._startTime   = X_WebAudio_context.currentTime * 1000;
-                   this._interval    = this._interval || X_Timer_add( 1000, 0, this, this._onInterval );
+                   // this._interval    = this._interval || X_Timer_add( 1000, 0, this, this._onInterval );
+                   this._interval    = this._interval || X_Timer_add( 100, 0, this, this._onEnded );
                        },
                        
                                _sourceDispose : function(){
                            this.bufferSource.disconnect();
-                           delete this.bufferSource.onended;
+                           //delete this.bufferSource.onended;
                            delete this.bufferSource;
                        },
 
+                               /* 
                                _onInterval : function(){
                                        if( !this.playing ){
                                                delete this._interval;
                                                return X_CALLBACK_UN_LISTEN;
                                        };
                                        this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_PLAYING );
-                               },
-                                               
+                               }, */
+                                       
                                _onEnded : function(){
                                        var time;
-                                       delete this._timerID;
+                                       //delete this._timerID;
                                        
                            if( this.playing ){
                                time = X_WebAudio_context.currentTime * 1000 - this._startTime - this._endPosition + this._startPos | 0;
@@ -390,9 +399,10 @@ if( X_WebAudio_context ){
                                        if( time < 0 ) return;
                                } else {
                                        if( time < 0 ){
+                                               this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_PLAYING );
                                                //console.log( '> onEnd crt:' + ( X_WebAudio_context.currentTime * 1000 ) + ' startTime:' + this._startTime +
                                                //      ' from:' + this._startPos + ' to:' + this._endPosition );
-                                               this._timerID = X_Timer_once( -time, this, this._onEnded );
+                                               // this._timerID = X_Timer_once( -time, this, this._onEnded );
                                                return;
                                        };
                                };
@@ -402,6 +412,9 @@ if( X_WebAudio_context ){
                                                this.looped = true;
                                                this.disatcher[ 'dispatch' ]( X_EVENT_MEDIA_LOOPED );
                                                this.actualPlay();
+                                       } else {
+                                                               delete this._interval;
+                                                               return X_CALLBACK_UN_LISTEN;
                                        };
                                } else {
                                        this.actualPause();
@@ -413,12 +426,14 @@ if( X_WebAudio_context ){
                        actualPause : function(){
                                console.log( '[WebAudio] pause' );
                                
-                   this._timerID && X_Timer_remove( this._timerID );
-                               delete this._timerID;
+                   //this._timerID && X_Timer_remove( this._timerID );
+                               //delete this._timerID;
+                               this._interval && X_Timer_remove( this._interval );
+                               delete this._interval;
                                delete this.playing;
 
                    if( this.bufferSource ){
-                       if( this.bufferSource.onended ) delete this.bufferSource.onended;
+                       //if( this.bufferSource.onended ) delete this.bufferSource.onended;
                        
                        this.bufferSource.stop ? 
                                this.bufferSource.stop( 0 ) : this.bufferSource[ 'noteOff' ]( 0 );