3 * original : uupaa-js HTML5Audio.js
\r
4 * https://code.google.com/p/uupaa-js/source/browse/trunk/0.8/src/Audio/HTML5Audio.js?r=568
\r
7 var X_Audio_HTML5AudioWrapper, X_Audio_rawAudio;
\r
10 if( window.HTMLAudioElement ){
\r
12 X_Audio_HTML5AudioWrapper = X.EventDispatcher.inherits(
\r
13 'X.AV.HTML5AudioWrapper',
\r
14 X.Class.POOL_OBJECT,
\r
36 Constructor : function( proxy, source, option ){
\r
38 this._closed = false;
\r
40 X_AudioWrapper_updateStates( this, option );
\r
42 // TODO use video document.createElement('video')
\r
43 this._rawObject = X_Audio_rawAudio || new Audio( source );// X_Doc_create( 'audio', { src : source } ).appendToRoot();//( X.X_Node_systemNode );
\r
46 'loadstart', 'load', 'progress', 'suspend', 'abort', 'error', 'emptied', 'stalled', 'play', 'pause', 'loadedmetadata',
\r
47 'loadeddata', 'waiting', 'playing', 'canplay', 'canplaythrough', 'seeking', 'seeked', 'timeupdate', 'ended',
\r
48 'ratechange', 'durationchange', 'volumechange' ], this.handleEventProxy );
\r
50 if( X_Audio_rawAudio === this._rawObject ){
\r
51 X_Audio_rawAudio.src = source;
\r
52 X_Audio_rawAudio.load(); // 要る?
\r
53 X_Audio_rawAudio = null;
\r
56 //document.body.appendChild( this._rawObject );
\r
58 this._rawObject.autoplay = false;
\r
59 option.autoplay && X.Timer.once( 100, this, this.play );
\r
61 this.listenOnce( X.Event.KILL_INSTANCE );
\r
64 handleEvent : function( e ){
\r
67 case X.Event.KILL_INSTANCE :
\r
72 * http://uguisu.skr.jp/html/table3.html
\r
74 handleEventProxy : function( e ){
\r
75 //console.log(e.type);
\r
78 case 'loadstart' : // ブラウザがコンテンツの検索を開始した場合に発生
\r
80 case 'progress' : // ブラウザがコンテンツの取得を実行した場合に発生
\r
81 //console.log( e.loaded + ' ' + e.total * 100 + '%' );
\r
82 this._rawObject.duration && console.log( this._rawObject.buffered.end(0) / this._rawObject.duration * 100 + '%' );
\r
84 case 'loadedmetadata' : // ブラウザがメディアリソースの長さと寸法を判定した場合に発生
\r
85 case 'loadeddata' : // コンテンツの表示を現在の再生位置で初めて行えるようになった場合に発生
\r
86 case 'canplay' : // 今すぐに再生を再開できるが、バッファリングが不十分でコンテンツを最後まで表示できないと予測している場合に発生
\r
87 case 'canplaythrough' : // 今すぐに再生を開始してもバッファリングで停止することなく最後まで表示できると予測している場合に発生
\r
88 this.duration = this._rawObject.duration * 1000;
\r
89 //console.log( this.duration );
\r
92 case 'suspend' : // ブラウザが意図的にコンテンツの取得を現在行っていない場合に発生(ダウンロードは未完了)
\r
93 case 'abort' : // ダウンロードの完了前にコンテンツの取得を停止した場合に発生(この停止はエラーによるものではない)
\r
94 case 'error' : // コンテンツの取得実行中にエラーが発生した場合に発生
\r
95 case 'emptied' : // 読み込み中に致命的なエラーが発生したか、実行状態ででload()メソッドが実行された場合に発生
\r
96 case 'stalled' : // ブラウザがコンテンツの取得を試みたが、データがまだ用意されていない場合に発生
\r
97 case 'play' : // 再生が開始された。play()メソッドからの復帰後に発生する場合に発生
\r
98 case 'pause' : // 再生が一時停止された。pauseメソッドからの復帰後に発生する場合に発生
\r
100 case 'waiting' : // 次のフレームが利用不可のため再生を停止したが、そのフレームがやがて利用可能になると想定している場合に発生
\r
101 case 'playing' : // 再生が開始された場合に発生
\r
103 case 'seeking' : // シークがtrueに変化し、イベントを発生させるのに十分な時間がシーク操作にかかっている場合に発生
\r
104 case 'seeked' : // シークがfalseに変化した場合に発生
\r
105 case 'timeupdate' : // 通常の再生が行われ現在の再生位置の変化が起こった場合に発生
\r
109 if( !this._closed && this.loop ){
\r
110 this.looped = true;
\r
112 this.proxy.dispatch( 'looped' );
\r
114 this._timerID && X.Timer.remove( this._timerID );
\r
115 delete this._timerID;
\r
116 delete this.playing;
\r
120 case 'ratechange' : // defaultPlaybackRate属性とplaybackRate属性のどちらかが更新された場合に発生
\r
121 case 'durationchange' : // duration属性が更新された場合に発生
\r
122 case 'volumechange' : // volume属性とmuted属性のどちらかが変化した場合に発生
\r
124 //console.log( 'html5 ' + e.type + ' ' + ( this.proxy._listeners && this.proxy._listeners[ e.type ] && this.proxy._listeners[ e.type ].length ) );
\r
125 //e.type === 'canplaythrough' && console.dir( e );
\r
126 this.proxy.dispatch( e );
\r
129 close : function(){
\r
130 // 【javascript】モバイル向けブラウザでも音を鳴らしたい【WebAudio】
\r
131 // http://ingaouhou.com/archives/3633
\r
132 // ・使い終わったインスタンスはload()しておくとやや安定
\r
133 this.playing && this.pause();
\r
134 delete this._closed;
\r
136 this._rawObject.src = '';
\r
137 this._rawObject.load();
\r
141 var begin, end, halfway;
\r
143 // もし kill 後に autoplayTimer で呼ばれても、_closed==true なので平気
\r
144 if( this._closed ) return;
\r
146 end = X_AudioWrapper_getEndTime( this );
\r
147 begin = X_AudioWrapper_getStartTime( this, end, true );
\r
148 this._rawObject.currentTime = begin / 1000;
\r
150 console.log( '[HTMLAudio] play ' + begin + ' -> ' + end );
\r
152 if( !this.playing ){
\r
153 if( X_UA.Chrome ){ // [CHROME][FIX] volume TODO どの version で 修正される?
\r
155 X.Timer.once( 0, this, this._fixForChrome );
\r
156 this._rawObject.volume = 0;
\r
158 this._rawObject.volume = this.volume;
\r
160 this._rawObject.play();
\r
161 this.playing = true;
\r
164 halfway = end < this.duration;
\r
165 this._timerID && X.Timer.remove( this._timerID );
\r
168 this._timerID = X.Timer.once( end - begin, this, this._onEnded );
\r
170 delete this._timerID;
\r
174 // [CHROME][FIX] volume
\r
175 _fixForChrome : X_UA.Chrome && function(){
\r
176 !this._closed && ( this._rawObject.volume = this.volume );
\r
179 _onEnded : function(){
\r
181 delete this._timerID;
\r
183 if( this.playing ){
\r
185 time = this._rawObject.currentTime * 1000 - X_AudioWrapper_getEndTime( this ) | 0;
\r
187 console.log( '> onEnd ' + time + ' ' + ( this._rawObject.currentTime * 1000 | 0 ) + '/' + X_AudioWrapper_getEndTime( this ) );
\r
188 this._timerID = X.Timer.once( -time, this, this._onEnded );
\r
193 this.looped = true;
\r
195 this.proxy.dispatch( 'looped' );
\r
198 this.dispatch( 'ended' );
\r
203 pause : function(){
\r
204 this._timerID && X.Timer.remove( this._timerID );
\r
205 delete this._timerID;
\r
207 if( this.playing ){
\r
208 !this._rawObject.error && this._rawObject.pause();
\r
209 delete this.playing;
\r
213 state : function( obj ){
\r
216 if( obj === undefined ){
\r
218 startTime : this.startTime,
\r
219 endTime : this.endTime < 0 ? this.duration : this.endTime,
\r
220 loopStartTime : this.loopStartTime < 0 ? this.startTime : this.loopStartTime,
\r
221 loopEndTime : this.loopEndTime < 0 ? ( this.endTime || this.duration ) : this.loopEndTime,
\r
224 looped : this.looped,
\r
225 volume : this.volume,
\r
226 playing : this.playing, // && !this._rawObject.error && !this._rawObject.paused && !this._rawObject.ended,
\r
227 duration : this.duration,
\r
229 currentTime : this._rawObject.currentTime * 1000,
\r
231 http://www.w3schools.com/tags/av_prop_error.asp
\r
232 1 = MEDIA_ERR_ABORTED - fetching process aborted by user
\r
233 2 = MEDIA_ERR_NETWORK - error occurred when downloading
\r
234 3 = MEDIA_ERR_DECODE - error occurred when decoding
\r
235 4 = MEDIA_ERR_SRC_NOT_SUPPORTED - audio/video not supported
\r
237 error : this._rawObject.error || 0 // 0, 1 ~ 4
\r
241 result = X_AudioWrapper_updateStates( this, obj );
\r
243 if( result & 2 ){ // seek
\r
247 end = X_AudioWrapper_getEndTime( this );
\r
248 halfway = end < this.duration;
\r
249 this._timerID && X.Timer.remove( this._timerID );
\r
252 this._timerID = X.Timer.once( end - this._rawObject.currentTime * 1000, this, this._onEnded );
\r
254 delete this._timerID;
\r
258 this._rawObject.volume = this.volume;
\r
267 X_Audio_BACKENDS.push(
\r
269 backendName : 'HTML5 Audio',
\r
271 * HTML5 の audio 要素と video 要素でサポートされているメディアフォーマット
\r
272 * https://developer.mozilla.org/ja/docs/Web/HTML/Supported_media_formats
\r
274 * 主要ブラウザのHTML5 audioタグで使えるファイル形式の再生対応状況を調べてみた
\r
275 * http://sothis.blog.so-net.ne.jp/2010-10-27
\r
276 * ダメ元で仕様に含まれていない SHOUTcast もテストしてみました。
\r
278 * IE9 の HTML5 Audio について
\r
279 * http://kentablog.cluscore.com/2011/05/ie9-html5-audio.html
\r
280 * 1.Audioオブジェクトを作ることができないので、Audioタグを使う
\r
281 * 2.クロスドメインアクセスには、「clientaccesspolicy.xml」か「crossdomain.xml」が必要
\r
284 * IE9でHTML5 autio タグが無効になる
\r
285 * http://bbs.wankuma.com/index.cgi?mode=al2&namber=64886&KLOG=109
\r
286 * IEのバージョン9.0.8112.16421では、Audioオブジェクトのnewも対応してました。
\r
287 * createElement等で動的生成すると、よろしくない
\r
289 * media-can-play-wav-audio.html
\r
290 * https://github.com/adobe/webkit/blob/master/LayoutTests/media/media-can-play-wav-audio.html
\r
291 * testExpected("audio.canPlayType('audio/wav; codecs=1')", "probably");
\r
293 * HTML5 audioタグ ブラウザ間の違い
\r
294 * http://wiki.bit-hive.com/tomizoo/pg/HTML5%20audio%A5%BF%A5%B0%20%A5%D6%A5%E9%A5%A6%A5%B6%B4%D6%A4%CE%B0%E3%A4%A4
\r
295 * - volume, muted iPhone(iOS4-6)、Android(2.3.6)では動作せず。
\r
296 * - FireFox3.6, Android 2.3.6については、src変更後、load()を呼び出さないと切り替わらなかった。iPhoneはload()が不要。
\r
298 detect : function( proxy, source, ext ){
\r
299 var ok, mineType = 'audio/' + ext;
\r
302 ok = X_UA.IE || X_UA.Chrome || ( X_UA.Windows && X_UA.Safari );
\r
303 mineType = 'audio/mpeg';
\r
306 ok = 15 <= X_UA.Gecko || X_UA.Chrome || X_UA.Opera;
\r
309 ok = X_UA.IE || X_UA.WebKit;
\r
310 mineType = 'audio/mp4';
\r
313 ok = 2 <= X_UA.Gecko || 10.6 <= X_UA.Opera; // firefox4+(Gecko2+)
\r
316 ok = X_UA.Gecko || X_UA.Opera || ( X_UA.Windows && X_UA.Safari );
\r
317 //mineType = 'audio/wav'; // audio/x-wav ?
\r
323 if( !ok && mineType ){
\r
324 if( !X_Audio_rawAudio ) X_Audio_rawAudio = new Audio;
\r
325 ok = X_Audio_rawAudio.canPlayType( mineType );
\r
328 proxy.asyncDispatch( ok ? 'support' : 'nosupport' );
\r
331 klass : X_Audio_HTML5AudioWrapper
\r
339 mp3: !!audioTest.canPlayType('audio/mpeg;').replace(/^no$/, ''),
\r
340 opus: !!audioTest.canPlayType('audio/ogg; codecs="opus"').replace(/^no$/, ''),
\r
341 ogg: !!audioTest.canPlayType('audio/ogg; codecs="vorbis"').replace(/^no$/, ''),
\r
342 wav: !!audioTest.canPlayType('audio/wav; codecs="1"').replace(/^no$/, ''),
\r
343 aac: !!audioTest.canPlayType('audio/aac;').replace(/^no$/, ''),
\r
344 m4a: !!(audioTest.canPlayType('audio/x-m4a;') || audioTest.canPlayType('audio/m4a;') || audioTest.canPlayType('audio/aac;')).replace(/^no$/, ''),
\r
345 mp4: !!(audioTest.canPlayType('audio/x-mp4;') || audioTest.canPlayType('audio/mp4;') || audioTest.canPlayType('audio/aac;')).replace(/^no$/, ''),
\r
346 weba: !!audioTest.canPlayType('audio/webm; codecs="vorbis"').replace(/^no$/, '')
\r