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_HTML5Audio, X_Audio_HTML5AudioWrapper, X_Audio_rawAudio,
\r
8 X_Audio_HTML5Audio_LIVE_LIST = [],
\r
9 X_Audio_HTML5Audio_POOL_LIST = [];
\r
12 if( window.HTMLAudioElement ){
\r
13 function getHTML5AudioWrapper( proxy ){
\r
14 var i = X_Audio_HTML5Audio_LIVE_LIST.length;
\r
16 if( X_Audio_HTML5Audio_LIVE_LIST[ --i ].proxy === proxy ) return X_Audio_HTML5Audio_LIVE_LIST[ i ];
\r
20 X_Audio_HTML5Audio =
\r
22 backendName : 'HTML5 Audio',
\r
24 * HTML5 の audio 要素と video 要素でサポートされているメディアフォーマット
\r
25 * https://developer.mozilla.org/ja/docs/Web/HTML/Supported_media_formats
\r
27 * 主要ブラウザのHTML5 audioタグで使えるファイル形式の再生対応状況を調べてみた
\r
28 * http://sothis.blog.so-net.ne.jp/2010-10-27
\r
29 * ダメ元で仕様に含まれていない SHOUTcast もテストしてみました。
\r
31 * IE9 の HTML5 Audio について
\r
32 * http://kentablog.cluscore.com/2011/05/ie9-html5-audio.html
\r
33 * 1.Audioオブジェクトを作ることができないので、Audioタグを使う
\r
34 * 2.クロスドメインアクセスには、「clientaccesspolicy.xml」か「crossdomain.xml」が必要
\r
37 * IE9でHTML5 autio タグが無効になる
\r
38 * http://bbs.wankuma.com/index.cgi?mode=al2&namber=64886&KLOG=109
\r
39 * IEのバージョン9.0.8112.16421では、Audioオブジェクトのnewも対応してました。
\r
40 * createElement等で動的生成すると、よろしくない
\r
42 * media-can-play-wav-audio.html
\r
43 * https://github.com/adobe/webkit/blob/master/LayoutTests/media/media-can-play-wav-audio.html
\r
44 * testExpected("audio.canPlayType('audio/wav; codecs=1')", "probably");
\r
46 * HTML5 audioタグ ブラウザ間の違い
\r
47 * 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
48 * - volume, muted iPhone(iOS4-6)、Android(2.3.6)では動作せず。
\r
49 * - FireFox3.6, Android 2.3.6については、src変更後、load()を呼び出さないと切り替わらなかった。iPhoneはload()が不要。
\r
51 detect : function( proxy, source, ext ){
\r
52 var ok, mineType = 'audio/' + ext;
\r
55 ok = X_UA.IE || X_UA.Chrome || ( X_UA.Windows && X_UA.Safari );
\r
56 mineType = 'audio/mpeg';
\r
59 ok = 15 <= X_UA.Gecko || X_UA.Chrome || X_UA.Opera;
\r
62 ok = X_UA.IE || X_UA.WebKit;
\r
63 mineType = 'audio/mp4';
\r
66 ok = 2 <= X_UA.Gecko || 10.6 <= X_UA.Opera; // firefox4+(Gecko2+)
\r
69 ok = X_UA.Gecko || X_UA.Opera || ( X_UA.Windows && X_UA.Safari );
\r
70 //mineType = 'audio/wav'; // audio/x-wav ?
\r
76 if( !ok && mineType ){
\r
77 if( !X_Audio_rawAudio ) X_Audio_rawAudio = new Audio;
\r
78 ok = X_Audio_rawAudio.canPlayType( mineType );
\r
81 proxy.asyncDispatch( ok ? 'support' : 'nosupport' );
\r
84 register : function( proxy, source, option ){
\r
85 X_Audio_HTML5Audio_LIVE_LIST.push( new X_Audio_HTML5AudioWrapper( proxy, source, option ) );
\r
88 close : function( proxy ){
\r
89 getHTML5AudioWrapper( proxy ).close();
\r
92 play : function( proxy ){
\r
93 getHTML5AudioWrapper( proxy ).play();
\r
96 pause : function( proxy ){
\r
97 getHTML5AudioWrapper( proxy ).pause();
\r
100 state : function( proxy, obj ){
\r
101 return getHTML5AudioWrapper( proxy ).state( obj );
\r
105 X_Audio_BACKENDS.push( X_Audio_HTML5Audio );
\r
107 X_Audio_HTML5AudioWrapper = X.EventDispatcher.inherits(
\r
108 'X.AV.HTML5AudioWrapper',
\r
109 X.Class.POOL_OBJECT,
\r
129 Constructor : function( proxy, source, option ){
\r
130 this.proxy = proxy;
\r
131 this._closed = false;
\r
133 X_AudioWrapper_updateStates( this, option );
\r
135 this._rawObject = X_Audio_rawAudio || new Audio( source );// X_Doc_create( 'audio', { src : source } ).appendToRoot();//( X.X_Node_systemNode );
\r
138 'loadstart', 'load', 'progress', 'suspend', 'abort', 'error', 'emptied', 'stalled', 'play', 'pause', 'loadedmetadata',
\r
139 'loadeddata', 'waiting', 'playing', 'canplay', 'canplaythrough', 'seeking', 'seeked', 'timeupdate', 'ended',
\r
140 'ratechange', 'durationchange', 'volumechange' ], this.handleEventProxy );
\r
142 if( X_Audio_rawAudio ){
\r
143 X_Audio_rawAudio.src = source;
\r
144 X_Audio_rawAudio.load(); // 要る?
\r
145 X_Audio_rawAudio = null;
\r
148 //document.body.appendChild( this._rawObject );
\r
150 this._rawObject.autoplay = false;
\r
151 option.autoplay && X.Timer.once( 100, this, this.play );
\r
153 this.listenOnce( X.Event.KILL_INSTANCE );
\r
156 handleEvent : function( e ){
\r
159 case X.Event.KILL_INSTANCE :
\r
164 * http://uguisu.skr.jp/html/table3.html
\r
166 handleEventProxy : function( e ){
\r
167 //console.log(e.type);
\r
170 case 'loadstart' : // ブラウザがコンテンツの検索を開始した場合に発生
\r
172 case 'progress' : // ブラウザがコンテンツの取得を実行した場合に発生
\r
173 //console.log( e.loaded + ' ' + e.total * 100 + '%' );
\r
174 this._rawObject.duration && console.log( this._rawObject.buffered.end(0) / this._rawObject.duration * 100 + '%' );
\r
176 case 'loadedmetadata' : // ブラウザがメディアリソースの長さと寸法を判定した場合に発生
\r
177 case 'loadeddata' : // コンテンツの表示を現在の再生位置で初めて行えるようになった場合に発生
\r
178 case 'canplay' : // 今すぐに再生を再開できるが、バッファリングが不十分でコンテンツを最後まで表示できないと予測している場合に発生
\r
179 case 'canplaythrough' : // 今すぐに再生を開始してもバッファリングで停止することなく最後まで表示できると予測している場合に発生
\r
180 this.duration = this._rawObject.duration * 1000;
\r
181 this.endTime = this.duration < this.endTime ? this.duration : this.endTime;
\r
182 //console.log( this.duration );
\r
185 case 'suspend' : // ブラウザが意図的にコンテンツの取得を現在行っていない場合に発生(ダウンロードは未完了)
\r
186 case 'abort' : // ダウンロードの完了前にコンテンツの取得を停止した場合に発生(この停止はエラーによるものではない)
\r
187 case 'error' : // コンテンツの取得実行中にエラーが発生した場合に発生
\r
188 case 'emptied' : // 読み込み中に致命的なエラーが発生したか、実行状態ででload()メソッドが実行された場合に発生
\r
189 case 'stalled' : // ブラウザがコンテンツの取得を試みたが、データがまだ用意されていない場合に発生
\r
190 case 'play' : // 再生が開始された。play()メソッドからの復帰後に発生する場合に発生
\r
191 case 'pause' : // 再生が一時停止された。pauseメソッドからの復帰後に発生する場合に発生
\r
193 case 'waiting' : // 次のフレームが利用不可のため再生を停止したが、そのフレームがやがて利用可能になると想定している場合に発生
\r
194 case 'playing' : // 再生が開始された場合に発生
\r
196 case 'seeking' : // シークがtrueに変化し、イベントを発生させるのに十分な時間がシーク操作にかかっている場合に発生
\r
197 case 'seeked' : // シークがfalseに変化した場合に発生
\r
198 case 'timeupdate' : // 通常の再生が行われ現在の再生位置の変化が起こった場合に発生
\r
202 if( !this._closed && this.loop ){
\r
205 this._timerID && X.Timer.remove( this._timerID );
\r
206 delete this._timerID;
\r
207 delete this.playing;
\r
211 case 'ratechange' : // defaultPlaybackRate属性とplaybackRate属性のどちらかが更新された場合に発生
\r
212 case 'durationchange' : // duration属性が更新された場合に発生
\r
213 case 'volumechange' : // volume属性とmuted属性のどちらかが変化した場合に発生
\r
215 //console.log( 'html5 ' + e.type + ' ' + ( this.proxy._listeners && this.proxy._listeners[ e.type ] && this.proxy._listeners[ e.type ].length ) );
\r
216 //e.type === 'canplaythrough' && console.dir( e );
\r
217 this.proxy.dispatch( e );
\r
220 close : function(){
\r
221 // 【javascript】モバイル向けブラウザでも音を鳴らしたい【WebAudio】
\r
222 // http://ingaouhou.com/archives/3633
\r
223 // ・使い終わったインスタンスはload()しておくとやや安定
\r
224 this.playing && this.pause();
\r
225 delete this._closed;
\r
227 this._rawObject.src = '';
\r
228 this._rawObject.load();
\r
231 play : function( seekTime ){
\r
232 var begin, halfway;
\r
234 // もし kill 後に autoplayTimer で呼ばれても、_closed==true なので平気
\r
235 if( this._closed ) return;
\r
237 begin = ( seekTime || seekTime === 0 ) ? seekTime : this.playing ? this.loopStartTime : this.startTime;
\r
238 this._rawObject.currentTime = begin / 1000;
\r
240 if( !this.playing ){
\r
241 if( X_UA.Chrome ){ // [CHROME][FIX] volume TODO どの version で 修正される?
\r
243 X.Timer.once( 0, this, this._fixForChrome );
\r
244 this._rawObject.volume = 0;
\r
246 this._rawObject.volume = this.volume;
\r
248 this._rawObject.play();
\r
249 this.playing = true;
\r
252 halfway = this.endTime < this.duration;
\r
253 this._timerID && X.Timer.remove( this._timerID );
\r
256 this._timerID = X.Timer.once( this.endTime - begin, this, this._onEnded );
\r
258 delete this._timerID;
\r
261 if( !this._interval ){
\r
262 this._interval = X.Timer.add( 1000, 0, this, this._onInterval );
\r
266 // [CHROME][FIX] volume
\r
267 _fixForChrome : X_UA.Chrome && function(){
\r
268 !this._closed && ( this._rawObject.volume = this.volume );
\r
271 _onInterval : function(){
\r
272 if( !this.playing ){
\r
273 delete this._interval;
\r
274 return X_Callback_UN_LISTEN;
\r
276 this.proxy.dispatch( 'timeupdate' );
\r
279 _onEnded : function(){
\r
280 delete this._timerID;
\r
281 if( this.playing ){
\r
285 console.log( '中断:' + this._rawObject.currentTime + ' ' + this.endTime );
\r
287 this.dispatch( 'ended' );
\r
292 pause : function(){
\r
293 this._timerID && X.Timer.remove( this._timerID );
\r
294 delete this._timerID;
\r
296 if( this.palying && !this._rawObject.error ){
\r
297 this._rawObject.pause();
\r
298 delete this.playing;
\r
302 state : function( obj ){
\r
305 if( obj === undefined ){
\r
307 startTime : this.startTime,
\r
308 endTime : this.endTime,
\r
309 loopStartTime : this.loopStartTime,
\r
310 currentTime : this._rawObject.currentTime * 1000,
\r
312 volume : this.volume,
\r
314 http://www.w3schools.com/tags/av_prop_error.asp
\r
315 1 = MEDIA_ERR_ABORTED - fetching process aborted by user
\r
316 2 = MEDIA_ERR_NETWORK - error occurred when downloading
\r
317 3 = MEDIA_ERR_DECODE - error occurred when decoding
\r
318 4 = MEDIA_ERR_SRC_NOT_SUPPORTED - audio/video not supported
\r
320 error : this._rawObject.error || 0, // 0, 1 ~ 4
\r
321 playing : this.palying && !this._rawObject.error && !this._rawObject.paused && !this._rawObject.ended,
\r
322 duration : this.duration || 0
\r
326 result = X_AudioWrapper_updateStates( this, obj );
\r
328 if( result & 2 ){ // seek
\r
329 this.play( this.seekTime );
\r
330 delete this.seekTime;
\r
333 halfway = this.endTime < this.duration;
\r
334 this._timerID && X.Timer.remove( this._timerID );
\r
337 this._timerID = X.Timer.once( this.endTime - this._rawObject.currentTime * 1000, this, this._onEnded );
\r
339 delete this._timerID;
\r
343 this._rawObject.volume = this.volume;
\r