代码如下:1 S/ w( k8 z1 O/ [' _
<!doctype html>7 v( `! e$ X4 M
<html lang=en>
" k0 S$ Z9 `' C, w$ x<head>' l; t1 @ T* t& N7 H# F5 ]1 ^2 p
<meta charset=utf-8>
+ ^/ \. j j# D5 d* J. i' W+ n<title>html audio api</title>5 U! b; g" i7 A% U5 t# e; `( b& u
<link rel=stylesheet href=./style/main.css />
0 h3 y; l. Q6 U* A$ s5 t" I5 l<script src=./script/audio-controls.js></script>
6 m6 x% D$ y7 j' O" y% U. |. g</head>
. R) d+ l* w+ r# _: e$ l; J5 Z; d<body>3 x# t2 W! C/ u( g' g
<header>
1 n9 \0 A' @8 b( h2 q<h1>html5 audio api</h1>
( z+ p4 q+ Z+ S; F; k& @( X! E5 ]<p>html5 audio api demo by <a href=<a href=> target=_blank>learnshare</a>.</p>4 S2 P" }# I7 ?6 c9 B% `
<p>
2 [' H* n4 c% d2 n1 Llast update @2013-04-23 20:40:00
$ Z; X0 S* e3 d4 |) D+ add info table
# t; C* |2 w( t5 h9 xupdate @2013-04-22 14:54:00. f: ~! u$ }8 \: H6 D
+ add dom events
0 k0 A3 P+ K5 [7 F8 ]' S* Lupdate @2013-04-22 12:47:00/ e9 g( B& K1 M: M4 |( `' U
+ add getcurrentsrc button: Q1 y1 U+ |) s M- O
</p>* V( H& G L: N: V$ |' |9 c
<p>! V8 m4 i+ p8 z4 M
view code on <a href=<a href=>
8 O9 j( c' h+ @- J/ k> target=_blank>learnshare.github.io</a>. {3 G+ p" J7 q
api reference: <a href=<a href=> target=_blank>htmlmediaelement</a> and <a href=<a href=> target=_blank>audio/video dom references</a>0 O; U+ C4 P9 e# w
</p>8 b- N) K" L% ?/ o0 K: s7 G
</header>. p' U' g$ Q P+ O$ p
<article>
4 ^$ Y7 B. T/ r8 k* y7 @: ^<section>
6 c( m. Z: K. M2 _: J<h2>audio element</h2>5 L5 ^7 Q1 h5 _; G1 w9 u
<audio id=audio src=./media/music1.mp3 controls=controls></audio>
- \2 k) P: L( `# ~% ~% u" F# b<p>open the <strong>developer tool</strong> to view console logs.</p>
5 _- u) F0 R h( b</section>2 ?( a! A6 ]3 S2 u$ G+ P$ U
<section>
4 z. } T- ]% |& B, [; d- O. e<h2>controls</h2>" b( f% l) `; W7 ~/ G
<button id=play>play</button>2 u) J/ ]6 \ E' F9 W: r1 S
<button id=pause>pause</button>' o- y. g4 D2 p2 u, d: \9 \* f
<button id=get_paused>getpaused</button>
! I7 z: {0 w$ e<button id=get_ended>getended</button>1 j& a6 ~% u2 ~& N2 p
/ K8 ^' a. Q# a" q( k+ t x
<button id=volume_down>volume-</button>
# I. a+ v7 A$ D8 \<button id=volume_up>volume+</button>( N4 T8 u( i7 Z2 E3 }$ v
<button id=get_volume>getvolume</button>) K7 M) D ^% i4 a! h' T
/ ^. h2 f5 }# x6 X- [2 F8 v
<button id=get_src>getsrc</button>
p1 Q+ u; l9 Z2 P) r# z<button id=play_music1>playmusic1</button>
( d* r2 U. K: b<button id=play_music2>playmusic2</button>8 S/ w- {1 X3 q8 N9 Q0 B+ o
<button id=remove_music>removemusic</button>! ]% L) \- q9 C8 @% j; z
<button id=get_current_src>getcurrentsrc</button>- Z; }" N l9 e0 k! e
$ N6 g* ? P4 a5 K. Z+ ?! f
<button id=get_initial_time>getinitialtime</button>
( x# ]: w" k! q<button id=get_duration>getduration</button>
8 o7 D. ~' Y9 U6 j. I0 }: w! m<button id=get_seeking>getseeking</button>
: Y6 U- a4 P# q! a<button id=jump_to>jumpto_30s</button>
% O8 g. j8 c. z4 k, ?<button id=get_current_time>getcurrenttime</button>
& y+ t: y' M0 V* n5 A, Y<button id=get_played>getplayed</button>
, k' y" }# ] B, k+ I
9 `2 n4 p% E& \& a7 ?5 O0 }) `, T<button id=autoplay_on>autoplay_on</button>/ |# s b8 _ w( q2 c* x
<button id=autoplay_off>autoplay_off</button>) w8 u4 h& C7 V- y) V" c- d8 \
<button id=get_autoplay>getautoplay</button>' w) P1 Z3 V* R1 d4 o8 U, L
: w) }9 q$ w3 `0 j' q
<button id=controls_show>controls_show</button>
- M$ ]% @1 C3 f5 n# x<button id=controls_hide>controls_hide</button>
6 L! f3 F; F# u+ B! ^( R<button id=get_controls>getcontrols</button>$ Z# X2 h# |8 u% m' W. c
% d H" H" H. ^/ T0 V* o. g<button id=loop_on>loop_on</button>
% G, x' J# X0 l. R |$ j4 W- Y+ p<button id=loop_off>loop_off</button>
. W+ c' e7 ~( X6 [3 n. f<button id=get_loop>getloop</button>- X: o% o# a" _* \' t1 H; o# ]
8 ~! `! g' v3 z2 @! D
<button id=preload_metadata>preload_metadata</button>3 J6 p: a" g! J0 Q& i6 R- m
<button id=get_preload>getpreload</button>
7 l8 T4 P0 f: k7 B" k% P% K
# M6 _! Y5 i: X) x) m0 E<button id=get_default_muted>getdefaultmuted</button>
, {) K5 U; e3 S' G<button id=mute>mute</button>
7 v+ w5 L6 _5 W, L: k- b1 i- z<button id=unmute>unmute</button>
5 M( c# r+ K' Y, N<button id=get_muted>getmuted</button>2 }* M$ Z6 v0 X8 Q* L' }+ k
7 t3 v7 o3 {) ?<button id=get_default_playback_rate>getdefaultplaybackrate</button>5 h% c6 L! E5 ^" f
<button id=playback_rate_down>playbackrate-</button>' N$ H, h6 C1 G4 z2 J
<button id=playback_rate_up>playbackrate+</button>1 `1 E5 L+ { f! |
<button id=get_playback_rate>getplaybackrate</button>9 W% w+ q! e$ J: H. g: n& M* m
- I; ]8 G+ \" X6 s6 N) N<button id=get_network_state>getnetworkstate</button> e. N; V0 [- P$ N
<button id=get_ready_state>getreadystate</button>1 K( q* u# ^& P6 K$ u* q
<button id=get_buffered>getbuffered</button>/ K8 {; a& S" @, \
<button id=get_seekable>getseekable</button>
6 f @0 b6 s- w* ?7 W</section>
4 y& S! X3 f7 ^6 `! w</article>2 @+ v6 Q3 d1 ?9 W6 [! [) J
<aside>2 n& |4 m4 w1 R
<section>; Y, j/ K+ a# |% y9 [
<h3>player attrs</h3>
3 M3 Y( K0 @- z% Y: A3 g0 p$ @<table>
! ?4 d5 Z9 B2 q' T8 _<tr> H" x4 h# V. X
<td>autoplay:</td>
$ C' ?8 k9 n& o<td id=autoplay></td>
1 }2 R/ ` k4 ?3 y</tr>5 T4 U& ?0 b8 O' b3 Y% |3 V! s
<tr>" [5 i* `7 ?& V; y& K' d# B# u/ _
<td>controls:</td>2 f5 K7 L5 [, N& j* }" w
<td id=controls></td>
- b- h8 k# p% @$ o</tr>0 z7 z2 {- ?' x2 d: |
<tr>9 ]4 `& g! s% n; v$ O
<td>defaultmuted:</td>
" d: c* n- L" w: U, Y$ y$ p<td id=default_muted></td>
( p+ G: a+ c# `! P0 ^</tr>8 |$ l& M; {, z7 } V) M o* }- f1 u9 W
<tr>2 g/ t; e% ^5 g( W7 T# T; G& q
<td>defaultplaybackrate:</td>+ T+ f, |3 A# ^" {' z% m& L
<td id=default_playback_rate></td>
+ ]9 M8 P. B4 `</tr>
; ~) i8 b4 d% p4 k: E: ^<tr>
0 m% r" p( r' }' \; H( Q [<td>loop:</td>7 t r9 k2 s4 e4 z
<td id=loop></td>% G; r1 ?/ D, q: q6 D2 Q3 D
</tr>& h# v- s( c6 j; S# | k
<tr>! m, u0 s% k& H" K: s U
<td>preload:</td>. z+ m$ r' Y. l
<td id=preload></td>
, j+ @: n' L0 V8 T k0 k</tr>
" r* v3 K2 g: j* r \: _</table>
9 |5 k" L& o5 T, K0 d) A</section>" J" s% o& o; E
<section>) e& V- _/ t) U
<h3>player info</h3>; O5 H: X ~) p$ G+ d/ H6 D! F5 `- X
<table>
0 R- Y$ A2 S9 x<tr>8 p% V9 N. f' P% r1 ~
<td>src:</td>; q2 g* M6 s" d% q x
<td id=src></td>
6 q1 K; p1 M: s</tr>! r" B" }& x& u6 y( Z
<tr>
" b- @+ S* V9 R8 S<td>currentsrc:</td>4 d- s: x4 [$ w, ^
<td id=current_src></td>
# `- h8 R7 ^2 a5 @5 `0 Y2 g</tr>
( l3 H* d) S& Z* l- K<tr>
% I% a8 V7 d! D2 N* o8 k<td>duration:</td>0 p. T) l& y; |' Z0 P
<td id=duration></td>
! W7 w5 a) O7 J1 F) _</tr>
6 Z) ^3 N2 u2 X4 Z, m<tr>
5 e- w% T& b9 L* |1 E9 W% i<td>currenttime:</td>7 @! R2 p2 } P5 L
<td id=current_time></td>
& ]/ A$ `, W; ?( Q</tr>; _1 Y- K% q6 r* S7 g. I6 T7 K
<tr>
' U: @5 Y, p+ M n) n<td>ended:</td>
- I9 g" i. W8 T; T' v$ S<td id=ended></td>$ ~5 m% ~0 Q2 d& {6 a1 B# e. b- ]
</tr>- Q; J4 u: M6 G" V
<tr>
- U, @8 F% I0 B, W<td>paused:</td>
, D2 i c0 d8 a$ C. T6 b. d: ^<td id=paused></td>
" W( K* w) l8 `3 X6 u</tr>
1 j* K& \% F. Z- g3 ?<tr>& M6 h. E. V" O9 W
<td>muted:</td>
# W5 F. q3 v! [, a# G<td id=muted></td>9 U0 i9 S \3 {; g+ A3 W3 v n
</tr>
1 e: E/ G2 w6 o+ a" h3 [0 @<tr>
8 M9 \9 Z: R/ [6 _<td>volume:</td>
1 Q1 N1 l. h4 m8 }1 g9 b2 F<td id=volume></td>
0 r( ]$ b+ P- w5 T- q* f/ L! j& N</tr>' j- O7 ]! j; L P/ ?
<tr>
# a7 s$ w0 j2 M" s* {<td>playbackrate:</td>
* F7 N$ z3 d2 A<td id=playback_rate></td>" v/ U( ~% w4 S/ q; O* T, B
</tr>1 L0 i' w# \# z7 i) [0 Y: V; G( ]
</table># Q1 u; \* s3 A! h+ X! o8 y/ S& ^
</section>$ n3 r3 G% t1 a$ j' g
<section>( H3 g+ m5 M* V0 M' F+ f
<h3>play status</h3>
" E8 O8 t( p5 W$ ?<table>
9 A5 _% M0 V% q. t) |<tr>
( v! n+ u" U0 J0 L<td>networkstate:</td>4 V# Q( z7 Y+ p; _) H! h7 }
<td id=network_state></td>
7 @* a3 Z+ x& Q' \( b/ b</tr>9 h5 a; s# U3 H
<tr>
( @6 Y7 ^" X* t9 [) w" [6 t<td>readystate:</td>2 y, O' l. u+ p) M; k6 f) ^
<td id=ready_state></td>
2 b- } l$ e' {0 O$ w9 y7 W</tr>
6 Z9 R8 }1 u# U' q<tr>2 S F5 z$ u$ ]# x7 q
<td>buffered:</td>$ b0 T5 ?, y' M/ ]. F! B* `: b
<td id=buffered></td>7 o5 E Q" d( _0 `3 G
</tr>: I8 N c' G6 Z- @. H
<tr>
+ ~/ f# {6 x2 R8 K B6 t% B" p<td>seekable:</td>
% z$ i- S" @% I<td id=seekable></td>! p, X6 v. \8 A) ?1 f
</tr>, S! X) e( ~! x5 o0 A; k
<tr>
5 F4 |7 W3 D R' b<td>played:</td>" e8 {+ R" i% d8 \3 }/ c* Y# Y4 F
<td id=played></td>9 x8 V8 Q" Z6 c
</tr>* ^' O$ y3 J* S" W: b6 T
<tr>0 ?. F& s* |- s
<td>error:</td>5 O( A/ T( ?( E# Q0 _* k+ R
<td id=error></td>
) F2 y, c2 y+ M9 T( t</tr>( R8 K5 \% O: d) |: \: R9 B! T
</table>
9 P& N* i) Q9 s4 h: ~</section>
v! b! _4 ?. `+ U! ]; }5 `' h</aside>2 [7 X: S2 u, ?; v% |7 n6 K4 R6 {8 j
</body>
$ I: q" p+ a/ x: T</html>5 J9 I/ @' e- {" M
9 D+ {9 T' r2 h9 L6 {8 c. I) oaudio-controls.js% k( c, U# d: h
3 W$ D; M& t$ [( f. }
代码如下:
7 X0 I$ Y* l% J* E8 ]! i9 Nwindow.onload=function(){4 P7 o2 k9 G0 ^- e$ `- x
// get autio element
% n9 V# ]/ O4 W! l1 t, E; dvar audio=document.getelementbyid(audio);4 ~1 u/ z" K2 v* Z+ _. K
// play(). I& v3 m, t. v% D& c
document.getelementbyid(play).onclick=function(){5 k* ` d: I' X8 @" R, z
audio.play();
( N0 Y% v( ]/ |. lconsole.log(play);
$ T$ M- J/ ]3 {- w};
% U; }7 v# x7 r9 A// pause()# w& i& b1 L) E# k: F |
document.getelementbyid(pause).onclick=function(){4 Q$ L" y: t, f7 ~5 n7 N' W8 t+ F; w
audio.pause();
, W0 L6 i1 i6 Xconsole.log(pause);
7 t7 d" c9 d$ d1 o8 Q};% I7 S7 e2 F: s. W. G3 D2 P
// get paused# A; z2 y/ v0 r4 [$ [# h1 n
document.getelementbyid(get_paused).onclick=function(){4 u, o2 B2 b! @+ I
console.log(audio.paused: +audio.paused);
) L1 F( d& D: Q% A! i# q};
4 K G# v& a; H. a9 n& A// get ended
, D6 L" M6 {' Q7 gdocument.getelementbyid(get_ended).onclick=function(){ ]1 `" z. f2 i$ n. C* z
console.log(audio.ended: +audio.ended);3 l: f2 E' V7 V. u9 N @" \
};
0 o6 Z1 ]' I& U$ p# v$ P// set volume-3 g+ ~6 u: p- E3 n! r4 }
document.getelementbyid(volume_down).onclick=function(){
$ A3 {2 z1 X, ^/ faudio.volume-=0.2;
4 l! Q+ Q% i! Nconsole.log(volume-0.2);
# {; T2 S( C5 I};
2 r1 j! | C4 r* o4 S# b% I// set volume+6 t; q* g% k* T4 {6 w
document.getelementbyid(volume_up).onclick=function(){6 q# G3 b* j; {. X9 F" Z* X; ?
audio.volume+=0.2;
7 D* z) ]4 I* e3 S/ Gconsole.log(volume+0.2);
/ U+ h; T) y4 g$ F" X};, {$ {9 I/ @+ \9 E* q4 Q0 H8 W
// get volume! y! L0 F/ A! i6 b% `$ r
document.getelementbyid(get_volume).onclick=function(){) G5 N+ ^7 L3 C
console.log(audio.volume: +audio.volume);1 _+ U, M- y& l4 T( @# c5 d
};
- y, m" h3 b# U, [7 S: r1 S$ K. ^// get src
& Z5 b" T. p% [ Jdocument.getelementbyid(get_src).onclick=function(){
. I* Y* ?8 F/ cconsole.log(audio.src: +audio.src);7 q* o) `- c* U4 k, V
};" _' L0 X0 N8 P) \
// set src_music1
) G1 v7 e0 `2 e3 c4 q: b# Hdocument.getelementbyid(play_music1).onclick=function(){
& A5 l J, B% d7 D$ }audio.src=./media/music1.mp3;! {; J2 ~, @" \
updatesrc();8 o' E7 i$ J, A8 V* W3 r
updatecurrentsrc();
- R" d# b$ d2 m: E- ?console.log(play music1);
9 F$ d* x8 X5 P H9 [; y};# J T$ r$ r# J1 \
// set src_music2
2 b$ ]( \1 r' ?: xdocument.getelementbyid(play_music2).onclick=function(){. L' `& @" ]; V" ]4 G
audio.src=./media/music2.mp3;
0 p9 q3 W8 [5 Z& n1 W+ O* h3 jupdatesrc();
/ v I& _/ D$ _/ y( X3 q! Vupdatecurrentsrc();
+ @ t2 Q& d4 D9 z4 ? ~$ l1 O9 Mconsole.log(play music2);9 N: Z* V0 @! v7 X
};
# x* G/ W6 O2 T" T" I9 h( j+ t// set remove_music8 W; D: B: q, ^" ~& R
document.getelementbyid(remove_music).onclick=function(){9 P" o I9 P$ E
audio.src=;% y8 O- z" ]/ s7 E2 B$ }+ [$ W6 p0 n) Y
console.log(remove music);
& ]1 p* v. N0 q7 C+ V};
6 s/ O* T" x% q+ t; K, _+ ~// get currentsrc# b) w- K) Z; L: X
document.getelementbyid(get_current_src).onclick=function(){' _9 X, {; |. s3 J( u! j
console.log(audio.currentsrc: +audio.currentsrc);
3 F# Y6 P3 X6 d, D/ l};
' E6 O8 C% i' Z' h3 r! f$ p// get initialtime* R$ f! F9 b1 s
document.getelementbyid(get_initial_time).onclick=function(){3 F' q0 p( t4 B& O+ r/ h
console.log(audio.initialtime: +audio.initialtime);
: ]6 F. g6 u2 o8 l* X) v};
* e3 Y8 x) j% R# X$ r& ?// get duration
) }' e* U9 ]; E+ r1 Pdocument.getelementbyid(get_duration).onclick=function(){' O7 k- \* w9 c! t- G
console.log(audio.duration: +audio.duration);+ N6 ~9 x, ?: r& C1 N2 K# [
};
4 w' K7 I2 n( A& b// get seeking
$ ^; Q# t7 S9 j2 edocument.getelementbyid(get_seeking).onclick=function(){
$ g! }8 Z Y3 r: h( Fconsole.log(audio.seeking: +audio.seeking);
- J5 W# d% P0 F& ?};
3 v4 }# X- U# R" j+ s) q9 r// set currenttime
( I' s' B4 Q- u9 }8 mdocument.getelementbyid(jump_to).onclick=function(){
% S% H9 C3 ]/ o7 c' g* N! M/ kaudio.currenttime=30;. ~% Z3 s# N k% r' a: \
console.log(jumpto 30s);+ J% N. f: r4 U, j9 q, w
};3 n! s% O1 u5 g2 W5 r$ Z
// get currenttime
9 w( e( p d, `+ y9 d7 F- Adocument.getelementbyid(get_current_time).onclick=function(){
; f- ~- D \! C: l ~6 h( Uconsole.log(audio.currenttime: +audio.currenttime);
! w7 R4 ~( @7 i# ^, _};6 m3 |) g6 I' r4 O
// get played( N5 B$ f. l9 v) u
document.getelementbyid(get_played).onclick=function(){
6 b+ G7 q' O! a. U5 F6 O. bconsole.log(audio.played:);8 B* E- n# J6 ?. p& k, A
var ranges=audio.played;
" u d+ J- L4 B2 xvar n=ranges.length;, q4 e; F8 m6 O2 P& p- |0 W5 q( `
for(var i=0;i<n;i++){
0 X# Z# _5 b2 Zconsole.log((+ranges.start(i)+,+ranges.end(i)+));; b, }7 F) `" F/ G+ V! J- ]! m6 A
}
8 t) ~* _) p- s9 U* M, b) ^};
@; w$ R/ Y, J' U. a+ X7 C# U// autoplay on3 `3 `7 g9 V% F% {6 H
document.getelementbyid(autoplay_on).onclick=function(){) A: k4 `" H# b$ D; d$ F# U3 U
audio.autoplay=true;
( t9 W- J. b! B/ d& tupdateautoplay();1 f z; i: y9 j& ~2 ^: W1 M$ {
console.log(autoplay on);
5 B8 Q9 b2 E' b};1 {$ K( s, a7 ^4 o
// autoplay off
- |# S2 M- [1 U& _, ~/ ydocument.getelementbyid(autoplay_off).onclick=function(){
U* R8 M" a% }5 F2 r( t* naudio.autoplay=false;8 J8 X4 s2 Y! s) y$ W! y; O
updateautoplay();6 @; B& h2 f% D6 k- K4 E
console.log(autoplay off);
( j& n K* }# H+ j; k# ~! Z; G};. n R( q% R) u
// get autoplay
' O; b6 y; n% t0 h6 w2 idocument.getelementbyid(get_autoplay).onclick=function(){
% V! F) ~& i! }& t4 m; o+ g( Nconsole.log(audio.autoplay: +audio.autoplay);& v+ b9 D! I d7 b) O$ h
};
# x: N2 E5 x- @// controls show
; Z/ ]7 V, Q/ L% Zdocument.getelementbyid(controls_show).onclick=function(){' o8 ~/ e* M" T2 I j1 K6 b
audio.controls=true;, h0 i$ Q5 x7 r. K' u& j
updatecontrols();6 P, E2 l1 b$ T- l
console.log(controls show);
/ D9 W _7 t/ e& t u/ C6 f2 ^5 `% O};
4 _- j, j3 s( o& U) H5 O' H6 t// controls hide- @ r; A" {1 H; T8 k( L
document.getelementbyid(controls_hide).onclick=function(){: `$ G4 R C) {) o- `5 X; u' u
audio.controls=false;
2 I* o; [$ d( v, |8 T& o. Jupdatecontrols();
1 a2 Z6 R! }. }console.log(controls hide); S" [: _6 `# R* t$ v$ F! p* G; U
};3 C6 c! d3 U; `- K: E1 ~& R
// get controls
. t3 J( U8 l( `1 W; wdocument.getelementbyid(get_controls).onclick=function(){- p: o1 c0 E: E* T! K
console.log(audio.controls: +audio.controls);1 r/ I! x% B7 g
};
7 L3 j5 C& C' F4 e8 k// loop on+ o" O5 b6 s, J4 [
document.getelementbyid(loop_on).onclick=function(){
+ M2 w. v* D haudio.loop=true;
# R; B- s( x6 a' B2 e+ b. u% k8 [1 Mupdateloop();' ]. t U; {, X9 d
console.log(loop on);) X( ?0 q# |5 `
};1 ]8 d: O5 N7 a( r Q3 i6 e
// loop off9 G; u. j9 N: y) U
document.getelementbyid(loop_off).onclick=function(){5 ~' m, x. V V6 t$ o, U$ ?% S: X
audio.loop=false;
( S, \* ]- }* W+ C1 lupdateloop();
: B: k+ \. T6 T4 n2 r) D' Zconsole.log(loop off);1 {% i& n( k/ D( E1 g+ k6 O
};
9 g7 Q/ d! S5 ?$ f3 V// get loop
' b, O3 s- B( K/ ?6 |/ @# bdocument.getelementbyid(get_loop).onclick=function(){
7 F3 y+ g9 o( f! r0 [console.log(audio.loop: +audio.loop);
+ @ s' L1 J% A: s};
}( X- \9 Z u9 i! T- [// preload metadata3 s& s# A U( L8 o! i! O
document.getelementbyid(preload_metadata).onclick=function(){" H$ m( D! ~9 w6 c* h$ R
audio.preload=metadata;: g8 @6 r' _% c( U3 W; U! ^/ B+ `' Q
updatepreload();! `; X' b* H7 m& b
console.log(preload metadata);( L- f1 M2 `1 q0 C7 v
};- o" ]4 ?+ T9 A4 e; y6 Q
// get preload4 r' y4 Y d0 E# n6 U& K
document.getelementbyid(get_preload).onclick=function(){& i& ^1 e6 a) i+ z
console.log(audio.preload: +audio.preload);
: ]) q) S! n% \5 p};4 X3 r" g! }6 a
// get defaultmuted
0 C9 b2 m# p4 n( ~/ ], ^5 Pdocument.getelementbyid(get_default_muted).onclick=function(){# T: }! n( ?% i- C
console.log(audio.defaultmuted: +audio.defaultmuted);
4 b+ z/ o) Y1 J8 j3 T};
# h7 x; ?' C" ~0 l// mute
% c' f* r& {, z7 d; ]' G5 Adocument.getelementbyid(mute).onclick=function(){2 o) I- ~ d; @
audio.muted=true;
% w' ]( h4 U: U) A1 Q% aupdatemuted();# v$ s% B u" ^; ]6 L
console.log(audio mute);
8 e% K+ O# u2 O$ N};$ g# b0 ~7 r/ _3 I3 b
// unmute- }+ ~1 L- E( L' G
document.getelementbyid(unmute).onclick=function(){3 y' n) d# H4 H
audio.muted=false;
3 `: {7 }5 e( z; G. gupdatemuted();
/ Z+ l1 o% k) N4 ?& qconsole.log(audio unmute);, q" b/ U, f6 ^
};
- U2 X: J, [: n# s5 n1 V- y& ] d// get muted
4 B7 j* y, j- i( f+ V/ J# ~. odocument.getelementbyid(get_muted).onclick=function(){
( a* Y: @1 i: C2 F6 [console.log(audio.muted: +audio.muted);
% U! [% J2 x6 J: ?% A- j};
. {: X. `0 u' M( q3 N( d" c// get defaultplaybackrate+ {6 y9 K5 [9 ~. g5 D; y
document.getelementbyid(get_default_playback_rate).onclick=function(){
! [- P2 _* p9 e# i. Pconsole.log(audio.defaultplaybackrate: +audio.defaultplaybackrate);$ w9 b) F8 Q6 h1 t1 m
};6 ?) x& Y# p; ^* l6 h* }. D
// set playbackrate-" V. i8 I( z U& @
document.getelementbyid(playback_rate_down).onclick=function(){. f1 x+ W4 t# l5 {$ w, t+ G8 x* M
audio.playbackrate-=0.2;- p \; ~# ^) q/ Q& N* f" n
console.log(playbackrate-0.2);6 b& E4 Y3 O0 p! c, A
};
4 r7 |& W$ L$ {// set playbackrate+5 @4 n8 r- \0 [ T- i6 W( [1 {" r
document.getelementbyid(playback_rate_up).onclick=function(){7 p# ^2 R' R! M4 a
audio.playbackrate+=0.2;
5 s" I! @! K, Dconsole.log(playbackrate+0.2);
3 s5 d7 ^' O% K" W* \};$ I9 D# U9 E9 k
// get playbackrate: z. S* J; @- c e% I
document.getelementbyid(get_playback_rate).onclick=function(){0 G8 H% x% J' O( o8 |' n% M
console.log(audio.playbackrate: +audio.playbackrate);4 e/ `+ v0 @: ?* C
};
7 f9 g _+ o# I* {7 d+ g( V// get networkstate
4 Z; A# G+ b$ p2 }: O+ j! D+ L- [document.getelementbyid(get_network_state).onclick=function(){
& q7 b4 z; E: p( m8 \console.log(audio.networkstate: +audio.networkstate);: S; M9 I( W. D
};
/ A0 d8 e4 }% l; `6 i// get readystate
3 [# R% v) O8 ~$ hdocument.getelementbyid(get_ready_state).onclick=function(){
% @# C7 u1 f$ l$ j3 H0 |, Sconsole.log(audio.readystate: +audio.readystate);
4 F% ~) T: D& U' K0 J/ G0 K ?};
0 S0 P" }: l( p- J// get buffered
3 O, b0 x4 e2 H' a% d9 u7 Wdocument.getelementbyid(get_buffered).onclick=function(){
9 x1 H u) a- B: R: }console.log(audio.buffered:);
- j" u( G' u' ^var ranges=audio.buffered;" T( ?, U4 f+ g* q0 s1 {1 l
var n=ranges.length; X3 o8 G+ y. @* ]1 W9 w% V6 }
for(var i=0;i<n;i++){
- E6 X% n; a+ ]/ `7 d; `! Wconsole.log((+ranges.start(i)+,+ranges.end(i)+));
+ {, \# h* O& l$ B. \1 w/ f) ~; ?}9 l% i3 M! w# r& {- {7 Z- P
};/ x" n: x# K, f0 J; l1 M' b5 Z
// get seekable, I% Z/ _* ~' F! _ E( \, e8 w
document.getelementbyid(get_seekable).onclick=function(){& f7 ~$ A; E0 `4 X, ?. x6 F
console.log(audio.seekable:);
7 `6 f6 g# p( i' F/ ~4 mvar ranges=audio.seekable;
# K5 q1 }1 @% E2 `4 \var n=ranges.length;
) F- l9 V" P0 b. \for(var i=0;i<n;i++){( ?9 i. h s2 Y* J: t2 ?' E0 a
console.log((+ranges.start(i)+,+ranges.end(i)+));5 `) |- O$ v4 u6 Y& v, U
}
4 T0 R. x9 L! n% Z R( ^};
) m) ]. M* k/ V. z& A4 p// dom events
1 b S* R/ o; V8 K- c. O0 M// abort2 m3 M! D/ m0 z6 G2 r
audio.addeventlistener(abort,function(){" E" U6 f2 o5 O2 b3 {
console.log(event:abort);' n1 z) {/ X# B9 W% C. E9 S0 g
});
i. V( c3 G( r// canplay, b* N3 P, h6 }5 l* P6 z( N$ x; V7 V
audio.addeventlistener(canplay,function(){/ U M: I& \0 D5 q& s
console.log(event:canplay);
6 N* F+ m% r8 U0 u {5 x( m});
+ E- e) m$ y! z' o) M// canplaythrough# U; Q) @- C# _7 u1 V
audio.addeventlistener(canplaythrough,function(){% _' h- c+ B- l, m" Q
console.log(event:canplaythrough);3 I# h# P# k( ?1 q
});: w& H; x7 N8 q
// durationchange
& l3 _* t2 y2 Paudio.addeventlistener(durationchange,function(){
8 y' v% R# E' [6 C4 @7 x6 e3 D, X; Lupdateduration();8 K3 l: h8 G. M( @1 |& F
console.log(event:durationchange);
- l: M% Y7 W3 Y# P% \: M5 u, m});
. e# e# d% i2 }2 V; s// emptied
! z- A) `* j7 y; ~& c! gaudio.addeventlistener(emptied,function(){
" N. h. S4 H1 B7 x6 ?updatesrc();
& S; `6 [ y$ X1 h/ @updatecurrentsrc();
9 H0 a6 Y( w+ T; R% qupdateduration();8 h" |' P5 V! X) h, Q
updatepaused();8 s, y' y9 \2 A0 @1 }
updatenetworkstate();' ^4 f! y8 D. { ? W0 h
updatereadystate();5 n0 F5 v3 B- p! p
updatebuffered();+ r, B4 b8 o2 P1 J! V3 H! m* c
updateseekable();! M1 B+ w, ], N9 x
updateplayed();
/ e: l* |/ s2 m k* _/ h* d2 h( Pconsole.log(event:emptied);; P6 I) q1 W5 i! |8 v( s, W r3 [
});/ e, {$ X$ D9 P9 Y2 g. ]5 c
// ended5 ^; _5 k: _4 _1 v# w( I; o
audio.addeventlistener(ended,function(){
; w; l- h7 F, U7 D+ o% T6 xupdateended();
7 k* T+ o+ C6 g& |" M4 X/ |6 f& Iconsole.log(event:ended);6 v3 { P4 \8 |3 ~
});. j$ Y h3 a5 a
// loadeddata
- q) p- X" x: t( i: Baudio.addeventlistener(loadeddata,function(){
& m% ?& ]7 k8 i+ h% G3 e$ ]& dupdatenetworkstate();
# h# S6 W: S7 i7 }8 G, iupdatereadystate();
2 \) E& t, s$ I3 I+ `4 O6 Oupdatebuffered();- E# u8 N) z4 I4 D
updateseekable();
0 X1 r% J9 z, t2 f, M% @9 Cconsole.log(event:loadeddata);$ l5 J* }6 L( `4 P
});
8 |5 c x3 o' }" m" R5 n9 [$ [// loadedmetadata- z3 d- {( e: h
audio.addeventlistener(loadedmetadata,function(){
9 p1 R( ~7 |3 c4 K! }; b3 zconsole.log(event:loadedmetadata);) y6 v( y' I" G" [+ J- p5 g0 s
});
" ?6 ^. x7 e( c9 Q! E; E- `0 N// loadstart5 y B" V7 e" k
audio.addeventlistener(loadstart,function(){6 M7 F0 j8 F' }: e- ?
console.log(event:loadstart);3 L6 S; q I: D/ f
});& o/ V' g; J9 B. I+ w5 E
// pause
" a/ q$ T5 P" p1 l$ Kaudio.addeventlistener(pause,function(){
/ H& y6 P3 t6 b, n: t4 s. ?4 mupdatepaused();
4 @8 f5 o0 p! t7 q: ]6 n8 P1 S: ?) ~console.log(event:pause);
- B( Z; L- d; W; @$ b5 h});
& j% ^* x8 i, B// play6 k* _) E3 v4 R6 ^3 D g' l/ g
audio.addeventlistener(play,function(){
4 L, a" S: m3 ^updatepaused();
' m* t( \- B, j) T# p" jconsole.log(event:play);! c, r. k* O4 K* _7 ?7 V8 h
});
/ J" m6 `6 T2 v: C J// playing
' N1 F7 b5 S+ @% _ naudio.addeventlistener(playing,function(){
1 \, H2 G* F j C4 M8 `4 dconsole.log(event:playing);
' V9 [3 Z9 R; [- F- D3 d* D+ f});
, A s& i5 P0 j6 H! t, }- Z// progress
& x/ T9 c& I; v/ C! {audio.addeventlistener(progress,function(){
}; `# v6 o5 y: j9 E. _6 Fupdatenetworkstate();9 `7 U3 Q* s* [. [. }4 l
updatereadystate();, S6 V! K. y( F
updatebuffered();
( ^2 j T1 C& P2 f& N# Eupdateseekable();
3 c4 o# C& Z: r9 U, j# u6 X" n6 y+ Nconsole.log(event:progress);
4 u5 Y" O: @" z0 ]% C6 S" {$ h});/ x0 r) o4 c# w8 a
// ratechange
8 Z, A8 v3 q raudio.addeventlistener(ratechange,function(){- O5 k/ t: w+ u1 Q: g- r' H6 a3 p
updateplaybackrate();* ^7 i9 X1 ]: p, z
console.log(event:ratechange);
7 k9 D; U* ~% L- B n/ w});9 x! R* i2 U$ E! W: Q
// seeked
5 h5 Z) h% }; |# X2 l9 \2 iaudio.addeventlistener(seeked,function(){/ p: x$ p2 j1 m& z
console.log(event:seeked);+ r: o: {) K; P9 k5 a6 h
});5 X9 j# b1 B6 J$ p& N) }
// seeking. \# M0 i+ u$ B5 o- m
audio.addeventlistener(seeking,function(){% d+ B# u' J4 S3 F
console.log(event:seeking);
4 o; H9 [0 c f& v& T( R! ]3 t1 ]});9 M& Z" h- Q# M' s0 b8 z( J" {
// stalled
8 b2 N0 A+ w: I7 I! y- x( ?3 Jaudio.addeventlistener(stalled,function(){
% M8 a4 y/ o" S7 o7 N+ ?7 Xconsole.log(event:stalled);
0 }: r8 C" J+ d! ^});
: K+ g9 }! {' U, D; J$ j- a// suspend
+ A D- e! C( S k* Y9 u3 Eaudio.addeventlistener(suspend,function(){) J( `# G7 e8 U& {' [/ a
console.log(event:suspend);7 O/ T- a: Z }0 s# }. M4 i
});( }! u8 b& v, E$ P
// timeupdate
# n& ?1 d$ x# e" j" L! H' Vaudio.addeventlistener(timeupdate,function(){5 x* H9 W4 w% Q2 q. C# C
updatecurrenttime();' h: ?4 q b2 _) k8 X
updateended();
+ l: I, {' E) J, k. U! ?updateplayed();
" I8 P. G4 X9 rconsole.log(event:timeupdate);* c* K$ Z4 ~7 O% U* L/ h) R
});; A0 V" w/ C& [, Q' x
// volumechange
+ c C7 E5 |$ D) `. t3 h& e6 Daudio.addeventlistener(volumechange,function(){ i$ c& {2 ?3 X8 t
updatevolume(); D* }* u$ `+ J8 E' f y$ V
console.log(event:volumechange);) h+ @2 |7 _7 e
});
/ ]/ B3 [$ ]7 F1 G// waiting
- h# i( _4 ^9 @& |( laudio.addeventlistener(waiting,function(){- j& R+ F+ V9 m& X8 s( t3 _+ M
console.log(event:waiting);1 C& ^. R+ B- c$ x0 g( I
});% r$ t8 V8 F+ y' H- m5 }
updateautoplay();7 l3 C2 T' U Q, i3 `
updatecontrols();
" S- f% ?8 w9 @7 l: nupdatedefaultmuted();
" N3 G2 U3 r. W% G4 ~1 Lupdatedefaultplaybackrate();
& w5 e8 ~" Q! @& X ~- S4 Uupdateloop();" o7 i% a3 U. @' t6 P- l6 J+ c# Z2 O
updatepreload();
: Q! X: f$ L; \, z3 |updatesrc();
0 _* |( D( Y6 k4 z, I- zupdatecurrentsrc();' H, J3 p6 M, p3 w" v( m7 V
updateduration();0 D4 A& e8 g' F* v5 j6 v& ?
updatecurrenttime();
$ h( v& j% c* E' v( P) aupdatevolume();+ X; l* d5 {% e
updatepaused();/ ?( `/ ^- R8 P: `' U2 y5 e( J
updatemuted();
; X, ~, j- O4 V, S5 y' Nupdateended();
% ?2 ? a+ f6 kupdateplaybackrate();
% a6 s% A* r, E7 Yupdatenetworkstate();
' Z6 a# H' j$ }& z D9 P) Gupdatereadystate();
2 X" q5 M( G; v; Y m& N) g, Nupdatebuffered();' Q& E( \! q# f% f! P" p
updateseekable();2 j% a' \/ M7 D8 `
updateplayed();, g( [4 Y. y5 R0 f3 E
updateerror();
* f0 j, B$ I# o) q4 O/ b+ e};
. r: E* F# \. }5 p// functions to update info table0 h; p8 v Z& s- i
// autoplay
" I" R/ y& l2 Z c8 A1 efunction updateautoplay(){
9 J8 A" G: s" a f, K5 E( }' h0 |' Cdocument.getelementbyid(autoplay).innerhtml=audio.autoplay;$ {5 g8 b# A2 R& R, v. T* ?( V: ]
}( ]8 @4 k+ O/ X
// controls
6 f; h: r* r( m0 A7 Zfunction updatecontrols(){
* \( Y# r3 _8 s/ Y7 D" ~2 Wdocument.getelementbyid(controls).innerhtml=audio.controls;# N1 i: H2 g* k6 o4 a
}3 y+ q( m9 K" t
// defaultmuted
/ g/ Z' Q9 {1 Q m' U5 @1 u% Wfunction updatedefaultmuted(){- @3 N: O( e! k" g1 g6 T4 R# t# ?
document.getelementbyid(default_muted).innerhtml=audio.defaultmuted;
$ A, h6 B/ h% {6 K. b}
3 z/ k" N$ R* O. k* a# c( P* k+ ]1 O// defaultplaybackrate4 d) m4 {* j- N3 ]/ w# s- ?
function updatedefaultplaybackrate(){6 S% S* y( x4 v i K4 D: o9 J3 w
document.getelementbyid(default_playback_rate).innerhtml=audio.defaultplaybackrate;
. ]1 @6 `# u5 r4 k/ h}
3 a2 Z% B% K; m// loop& M8 q b8 I2 z
function updateloop(){
' X( [# S4 ?7 d% _9 n" V% Ydocument.getelementbyid(loop).innerhtml=audio.loop;
& F# ~ E6 n' o1 V) R9 n}8 t5 d, }$ V+ }# C; y
// preload, H# M0 P+ r7 a; D" J
function updatepreload(){
' K$ N$ m1 Y( u' Ndocument.getelementbyid(preload).innerhtml=audio.preload;
+ Q0 r# z; M0 T8 c9 C t6 m}) O; h' m0 v; n7 ~* v
// src+ D9 A( A; w5 K( Y5 Q
function updatesrc(){
2 K1 S3 c% q) t/ o5 E, @document.getelementbyid(src).innerhtml=audio.src;- x4 u3 s3 f* k
}/ ]/ J7 f/ w& S, o. Y
// currentsrc
) S# m6 F8 ^% ] M3 Yfunction updatecurrentsrc(){" p- n2 V3 d- z
document.getelementbyid(current_src).innerhtml=audio.currentsrc;6 P O% F) m9 E7 x4 S
}7 W* _. J1 Z1 C$ K6 B
// duration
/ K) [1 {3 w# D/ J) f3 I" n9 e+ lfunction updateduration(){6 ^8 h/ l8 H. q3 r0 S
document.getelementbyid(duration).innerhtml=audio.duration;1 L/ B6 N* x& W! ?
}
- o X+ a, T6 p! w2 h// currenttime5 d2 h: M: ~% r# Z( W8 @; j
function updatecurrenttime(){& `* f% n, d( D6 M! m
document.getelementbyid(current_time).innerhtml=audio.currenttime;% k- D9 K \, q3 ?' }5 Z
}, i2 l- ]* N4 F* U
// ended7 ]& m: G* D) D/ j# I! N2 D
function updateended(){7 N6 B9 A& W* h( A( M6 I% f
document.getelementbyid(ended).innerhtml=audio.ended;( Y8 ^! q& o) Y7 z3 D& M/ s
}# o5 _ m) L1 b8 Y9 C, r
// paused
' W% l7 L4 [$ D e! o. v9 k9 c1 gfunction updatepaused(){
) N8 c1 @/ g3 m) |8 Jdocument.getelementbyid(paused).innerhtml=audio.paused;
$ {# a. S1 f6 [+ x}
" f$ H0 l1 L+ P, Q// muted
( S9 d- `' l$ f0 zfunction updatemuted(){8 N9 x& d F+ U. }2 k
document.getelementbyid(muted).innerhtml=audio.muted;! O- o- F; q3 N% n, q6 A" H
}
]0 _3 ]( V: W* w: r/ b// volume# u: R7 W i Q# S- F7 X- D) q. l
function updatevolume(){( a4 p) F( [1 q1 \
document.getelementbyid(volume).innerhtml=audio.volume;9 V, a& Y/ a# i5 ?, v0 m1 F, _ w
}1 N) C$ o6 U1 K; @$ H6 g
// playbackrate# k3 u% S# }1 G9 H
function updateplaybackrate(){
" m! O- F+ L: Adocument.getelementbyid(playback_rate).innerhtml=audio.playbackrate;. w5 A1 X! b5 P6 |, |/ b; P# S4 j3 n' P0 A
}6 O4 x5 T% E" u, e5 ^! {3 U
// networkstate$ J9 Z- c; q, b+ V9 [1 M! h0 ~, l
function updatenetworkstate(){3 `9 M" ^2 z2 Z+ s" Q: ^1 e
document.getelementbyid(network_state).innerhtml=audio.networkstate;) e' ~+ @: g0 }; Z/ G
}
/ O4 W- z! e" i3 R// readystate
7 Y, A/ ?, U- p/ jfunction updatereadystate(){ x) `" _( g* @6 `9 f
document.getelementbyid(ready_state).innerhtml=audio.readystate;/ w; H4 U: }1 H
}
$ t: R, I( [# _4 c* V+ v// buffered
; c2 B* R4 T% S% A' C4 Tfunction updatebuffered(){1 B. J% Q; r0 C0 A! U1 Z
var ranges=audio.buffered;7 b+ D& S1 r; P* z8 r
var str=;1 \# _3 y, ]# `
var n=ranges.length;
7 h: |; _- D* {; Ofor(var i=0;i<n;i++){
. E1 x; f5 ~8 B. ystr+=(+ranges.start(i)+,+ranges.end(i)+);( l7 E. x/ c4 p4 k& a/ d
if(i!=n-1){
# t) P$ N8 ]; I2 ]0 Jstr+=
# W8 ^1 B3 ?" b! V+ {* n;
4 d8 A) G" u) s7 q) C% q6 P}
( |: h d* s: p9 Q}# y+ F' {: I4 C8 Z
document.getelementbyid(buffered).innerhtml=str; f$ T. @0 ~( P0 n% [' i
}$ k8 `1 c0 H4 s0 @) s9 T. ?3 c
// seekable# X0 E/ O$ L5 B, Y
function updateseekable(){3 F9 o8 {% z. k: m( h+ h
var ranges=audio.seekable;
" G. Z+ R0 g+ T6 b+ nvar str=;
! \7 I9 _, c; I, e6 Dvar n=ranges.length;
4 G+ a' ^' {9 ]for(var i=0;i<n;i++){
8 q# b2 ]% a+ g5 q$ O/ i' u0 Estr+=(+ranges.start(i)+,+ranges.end(i)+);
: y, I3 Z$ n M3 f- _if(i!=n-1){) S3 m4 [) i2 L3 b6 y! Z0 P
str+=! m" e* {1 C# X8 |. @
;( i" i. i J5 c/ J& _1 [
}
8 U( t0 d9 s3 ^% V: i$ @# S}
, e/ Z# E! S, N' \) edocument.getelementbyid(seekable).innerhtml=str;$ o* S5 g+ B5 Q% R% }- D; ~& J
}& o' F! F- V8 W$ A
// played
' }5 u0 j- W4 L% L5 qfunction updateplayed(){
: j7 a& K# _. Jvar ranges=audio.played;+ }" p5 Q( K$ F: A& k* G- H0 o' w4 U
var str=;
* P* _6 @: T) L2 c% mvar n=ranges.length;
1 M8 X7 e& ~3 q. o: B/ F% ]% nfor(var i=0;i<n;i++){' K2 c+ |9 r( n
str+=(+ranges.start(i)+,+ranges.end(i)+);
0 U3 ~% Q' V+ ^if(i!=n-1){: T9 a; h, W+ E$ K! r
str+=2 G) }' }4 m- A/ ~" z* Q# u
;
- d; x) a7 ?& D7 x: a3 H}! q! {9 d) l9 M
}3 {7 a, l- T1 {7 t& D$ \
document.getelementbyid(played).innerhtml=str;
5 E! r7 _. C' O4 ]} `) t8 |% ?* B4 Z5 C/ q
// error
) g# l& w4 W! G5 j# `; ufunction updateerror(){( L$ Q/ q D/ y V
document.getelementbyid(error).innerhtml=audio.error;- Z: d5 B3 u4 P% O1 j* u0 K
}
* t9 B( k# \, Z, z% d/ \8 o% m6 _" Y0 O' x7 n# s( |6 M I
更多网页制作信息请查看: 网页制作 |
|