代码如下:
m; I$ G) k, @, h<!doctype html>: X- ~8 E; F7 S ?' {6 P9 o
<html lang=en>- N- ~0 A5 R2 {2 B4 n! v6 L& Z% j
<head>4 X) b2 _! y4 r w5 q
<meta charset=utf-8>
9 `% J: A7 O0 |, }6 u6 n% g2 H2 x<title>html audio api</title>
4 ?, R. O4 y1 p4 _<link rel=stylesheet href=./style/main.css />0 p3 B' L, i3 Q, H5 g
<script src=./script/audio-controls.js></script>
- _% F3 M# C! E" m3 F</head>$ e& J* d% @ F+ j6 t, {
<body>8 T, x3 x$ c" B; q5 r4 r1 E+ R) c
<header>; @/ Q+ _( A* t% c) ~9 G
<h1>html5 audio api</h1>
$ P& q- ?1 T+ K! @9 ]- q<p>html5 audio api demo by <a href=<a href=> target=_blank>learnshare</a>.</p>
) E( \- f# f$ Q B<p>: v' k8 k( H* f: @. g2 c
last update @2013-04-23 20:40:00
' ^6 |# m8 ~2 I: z" [9 H M3 G+ add info table
# K( n* J8 ~' A4 \/ v6 k$ D% dupdate @2013-04-22 14:54:00
* q7 h3 D1 B& n3 O, ]+ add dom events* \4 l6 s: j9 z1 E& p0 r
update @2013-04-22 12:47:00
- m% v% A# J- [6 I1 `9 u8 r+ add getcurrentsrc button
9 H7 |# Y5 v$ Q* k7 o7 S</p>
1 z- V0 \: v0 I<p>5 q0 M6 ?0 E" `
view code on <a href=<a href=>
" c3 N; ]/ O I: k M> target=_blank>learnshare.github.io</a>.
8 T, E7 Z5 _& P- F: zapi reference: <a href=<a href=> target=_blank>htmlmediaelement</a> and <a href=<a href=> target=_blank>audio/video dom references</a>8 m( [( a& T3 e2 V9 {" j! [
</p>$ Z E9 Z' ]4 k5 v& Y8 a
</header>. o$ R5 ?3 n% s' ^ N1 l0 ^
<article> Y& n. y6 t! l+ x; @8 E+ @' M$ R
<section>
" R" W2 v, L5 C' z2 D- R, l<h2>audio element</h2>
/ I0 v9 P/ m) D4 j6 Q1 T! b<audio id=audio src=./media/music1.mp3 controls=controls></audio>2 I' t5 X4 I7 G1 g: y
<p>open the <strong>developer tool</strong> to view console logs.</p>+ _: V7 E* l3 j F
</section>
- y" d2 n$ ^$ v- w9 P' ^7 j<section>4 g& X8 E' {8 R9 B
<h2>controls</h2>' Y, M9 J. ~: E) Z
<button id=play>play</button>
1 ?1 X: A7 o$ ~: m5 R& W0 {3 p<button id=pause>pause</button>
! N$ k9 [1 A# H- @6 x<button id=get_paused>getpaused</button>' g' a8 ^+ ?* N4 a1 b
<button id=get_ended>getended</button>
( r. s: X: k9 }4 z, `. w: f+ F+ N, H; _2 R0 s+ `/ r
<button id=volume_down>volume-</button>$ X& `3 y& `0 Z/ k+ j* I
<button id=volume_up>volume+</button>
- d( X! J% i. t4 y: c<button id=get_volume>getvolume</button>4 g2 n x; u& {, ?- C
8 x! F: P8 ?% ]' J4 \; U6 q, x<button id=get_src>getsrc</button>7 j o- ?6 F9 R7 K0 ~
<button id=play_music1>playmusic1</button>
8 Q' h7 J5 g& W/ ^<button id=play_music2>playmusic2</button>- i# b9 K6 y; v+ ~' E! l
<button id=remove_music>removemusic</button>, s6 Q: g( v, o/ ?; b
<button id=get_current_src>getcurrentsrc</button> V' ^- i1 h6 K8 T
8 o# p; ^( ~ y; m) K4 R<button id=get_initial_time>getinitialtime</button>" R8 ^7 P+ `# I0 j# L
<button id=get_duration>getduration</button>' a& w- E, U9 M, _1 D3 w
<button id=get_seeking>getseeking</button>
6 T" @* B" c# h. C<button id=jump_to>jumpto_30s</button>7 E& ~. s( J/ k- x0 \& T
<button id=get_current_time>getcurrenttime</button>9 T2 [( h8 x A
<button id=get_played>getplayed</button>
4 t( z n( j. n7 @& Y' k! N6 i0 T9 P+ x- j4 `$ `0 c
<button id=autoplay_on>autoplay_on</button>1 y" f- u. y$ R8 k1 P6 t- M Z
<button id=autoplay_off>autoplay_off</button>
; |7 y N4 l1 l. I<button id=get_autoplay>getautoplay</button>
! K% L" ]" R7 j$ v2 `9 j# r$ p, B; Y; f; H- m, i& |: N( b
<button id=controls_show>controls_show</button>6 B) `: f/ d# b7 |4 i
<button id=controls_hide>controls_hide</button>
9 s$ |; B) p6 O/ g<button id=get_controls>getcontrols</button>9 e2 ^" f9 z, |9 P
" Z' H8 D3 j, D: t5 E2 x. d
<button id=loop_on>loop_on</button>
: K* p) t$ u- H, @: _7 u9 x<button id=loop_off>loop_off</button>
( q5 v' B: G3 P# J( k6 k4 d<button id=get_loop>getloop</button>
4 }3 @5 y y8 l0 \( z$ v/ H/ o
$ s6 x$ k' w0 @5 F( X( T- Z0 g<button id=preload_metadata>preload_metadata</button>2 I9 W- Z9 z( x+ ?
<button id=get_preload>getpreload</button>
5 J7 E: }, s) c D
( f% b: w, `5 e# {% k3 E<button id=get_default_muted>getdefaultmuted</button>+ r: i. u3 W0 k9 r2 y6 | p
<button id=mute>mute</button>
& E) `2 ]7 Z; S$ d8 A0 Y- w<button id=unmute>unmute</button>4 n9 w% T9 O0 m% h6 g4 r
<button id=get_muted>getmuted</button>/ c& n+ i# J) i5 G( `
8 w+ W: R9 g" R' z* S0 a1 U3 n
<button id=get_default_playback_rate>getdefaultplaybackrate</button>
' G9 E5 x" q, K+ \. k<button id=playback_rate_down>playbackrate-</button>: s% u. ~5 ^" h7 k' V/ E' G0 b
<button id=playback_rate_up>playbackrate+</button>7 I' K. B/ U1 b1 B
<button id=get_playback_rate>getplaybackrate</button>8 X& K( U/ J( ^/ t
" [3 y- f- K2 e* R) p
<button id=get_network_state>getnetworkstate</button>
& x, l; b; {" U<button id=get_ready_state>getreadystate</button>' B4 I+ \$ `5 R# }& Q+ ^
<button id=get_buffered>getbuffered</button>+ L8 b# p, t3 s# X+ B8 F+ c
<button id=get_seekable>getseekable</button>' A% X% o9 ~, k7 W* o. x/ Y
</section>' l4 P$ m8 z x8 ~/ \! w( e2 Z
</article>/ ^: Z0 a; |* K$ F
<aside>
; y, O' X( f1 o9 O<section>
4 p2 q3 H. |6 o3 j' ]<h3>player attrs</h3>
) ~! P8 F7 y# N3 q( h3 h) e% v* ^; A<table>! n1 b. x6 ]; ~* y# D& }
<tr>
. b9 U/ G: o+ y9 c/ ?<td>autoplay:</td>
% u. v" i1 o8 V& o( f<td id=autoplay></td>0 ]/ A7 `" ~, O) Z
</tr>2 C% [- [. A+ C( ?/ v
<tr>
& f3 G8 X/ |2 v( \9 I" u<td>controls:</td>
* ~% g! U, s( j( W- \" @9 F9 L<td id=controls></td>5 z8 b; X( l8 ^6 x
</tr>
( k+ V8 Y! T! W! ^<tr>$ s7 i* _6 }; K$ R* A, e
<td>defaultmuted:</td>
" x1 C9 U% ]1 Y: L" ^8 o<td id=default_muted></td>4 J1 n3 y' ^- v* ~
</tr>
$ W4 W( r8 g H" ?& x. s3 ^<tr>
# d5 S/ S2 @9 F7 l2 b<td>defaultplaybackrate:</td>
1 j1 B: e1 M B* t<td id=default_playback_rate></td>
9 s; g- N! D# ]</tr>
4 n" v2 S' {" T<tr>
4 z5 y8 B- U! f2 G% w<td>loop:</td>
( E$ b; `. J3 d+ {! a; t& F<td id=loop></td>
0 i4 d- ~( Y$ j8 E$ g u</tr>
- r& e% A$ J- ^7 w6 z6 y( N<tr>/ w8 |( d5 S9 I2 O! O
<td>preload:</td>+ L' o N& L7 x
<td id=preload></td>
$ w' T$ ^' Q2 A</tr>
* n9 ^% b( j F, {( f% P, U( P8 f: `</table>2 \; N4 k5 k5 }: J
</section>5 U) Q3 A% ^' _; I
<section>; a. m9 C, N8 I& q; g, y" g4 y: [
<h3>player info</h3>
* s. u8 j2 G9 P; A! G# q<table>
! ]1 h7 O* R$ O. Y, G1 n7 h<tr>4 r; p/ H: o: R6 P9 s7 ]4 Y: K
<td>src:</td>: m _3 g3 K4 e/ V9 C0 z
<td id=src></td>/ k0 E5 J7 ^( m# A; h+ h
</tr>' f- @ {/ S2 r( g; t7 Z* ~
<tr>
" t1 Y6 _: t- a- ^) Y<td>currentsrc:</td>
5 E, M u2 B; n1 |$ F<td id=current_src></td>8 W5 J; k) T" |9 y1 b: \
</tr>
# k2 d7 g% d8 T' a3 ]) N<tr>% s2 _7 Z/ j# H) K
<td>duration:</td>2 S0 ~& N. Z. Z( k7 ~( G
<td id=duration></td>$ X- z. t% e4 |) ?+ ^6 D: M1 I
</tr>
$ p: n3 r) b) A$ w$ d9 N7 e* f<tr>" ~3 L4 O9 U* z. q" }4 a$ V
<td>currenttime:</td>' j) b9 g! e! o6 X; l+ |
<td id=current_time></td>
; e' C1 V; B0 W</tr>) E1 p. ~3 G- o( E$ ~
<tr>3 F' L# }4 X/ m- s" m
<td>ended:</td>2 T+ N( }, B' u- d
<td id=ended></td>. Q; }7 w* ]4 \ o; i1 Z9 i4 ?
</tr>( S: R& P) D, z4 I
<tr>' n) N% h8 g, m/ L( R/ s$ b
<td>paused:</td>
: b; X& r; s9 I1 ^$ E/ P, F<td id=paused></td>
5 Z3 t$ A4 m0 w( x% z1 b</tr>
/ p. U4 e. V( q5 q$ S0 W2 X<tr>
9 z9 e. I& ~7 b' j/ p% |3 i7 f<td>muted:</td>
) A' `' ?* \: T$ h5 e<td id=muted></td>
$ f2 X( i: ~' p; w @3 g, V</tr>: Y" P! p2 k) P" P+ ]
<tr>9 [1 e; q# [& M k6 l' Q0 s
<td>volume:</td>
. Q8 Y! h2 q% V" |<td id=volume></td>; \/ t4 Z2 |/ M- e- b* S4 D
</tr>+ y* X( E& R8 p5 N9 L3 h" Z
<tr>
9 f/ @, I8 w1 T, u; c9 m) a( X<td>playbackrate:</td>
8 o8 N& o" i4 z6 N* f+ s<td id=playback_rate></td>
4 e4 \% l- r; O, r1 g# Q9 g% j</tr>8 r: k9 U- [0 P2 R* H
</table>
- K. ~" e6 z7 G- v: j</section>
( q( Q1 a# R" }% S2 R: w4 k<section>, a2 ?/ A2 O: a; w* Z) P- T
<h3>play status</h3>% {2 Y6 b8 l3 f4 h) f
<table>* c- K% g/ L8 x: z* ~( Z
<tr>
% F7 f7 D4 E, @- u' O<td>networkstate:</td>
! @7 q# B7 W3 y! K+ X' Y1 W<td id=network_state></td>8 R; r( @$ [+ S
</tr>
) e" [' [; }5 I( f<tr>9 v' t' u4 @) V
<td>readystate:</td> U4 o: K; q2 o) o
<td id=ready_state></td>
+ q$ w# L& _/ a/ [</tr>
$ r1 I3 K" s1 M' M; f: ?1 [ d<tr>
6 X i( T" F% d: E( F2 j3 A" o<td>buffered:</td>6 Q/ C. c/ i: L
<td id=buffered></td>
1 ^0 C4 G" @0 Y( u/ u</tr>; D, u. `' S* q
<tr>
6 V$ A5 O9 b" A9 F<td>seekable:</td>- m0 f& m7 c" k4 I$ K& f( _
<td id=seekable></td>7 ?: M' c4 y, I5 t+ m
</tr>5 Z L1 T" H* h$ o' N: v
<tr>
' s% I) ~9 D8 d( i: P<td>played:</td>$ ~/ u. D; p% J7 c
<td id=played></td>
/ U; M2 `( l& \. K: \9 \2 }</tr>: F8 U4 f5 p) y; y! g, Q2 n. h
<tr>" V9 f D' j4 X! Q
<td>error:</td>
" f8 x7 P: H* ]6 ^# p<td id=error></td>% Q9 [; c% E" Y3 X- k. p
</tr>
' n1 f5 D/ G& Z</table>
% k* \0 }9 L) A2 o/ M% f3 T</section>
( I! `; p) J. \# y</aside>
1 J& `+ D, r9 c1 E</body>
+ R% D& |3 b @0 B</html>6 Z- w( c1 K& U6 k& s$ f# F
) s9 ?) t0 A4 l# q+ Yaudio-controls.js9 j5 N1 U- _5 H7 j
6 H- Q# U4 v5 l) G5 `+ H R# c代码如下:+ u0 r' E W/ E; T1 v" H
window.onload=function(){. p1 G& h/ q7 J( I8 a
// get autio element8 Q' h: K& e# ?' b. ^5 g! z+ O
var audio=document.getelementbyid(audio);5 u r8 A( ]5 w/ d
// play()$ r: ]" U. Z% ~* K
document.getelementbyid(play).onclick=function(){& b e. b4 t0 R! F+ C9 S5 |
audio.play();
6 \. u: f- S2 K& v2 @, M% I- `! mconsole.log(play);
. u9 J5 |# L: Z" E; `};5 z* d/ H1 V" `6 U6 m; z
// pause()7 [" i. u# ~# u$ b
document.getelementbyid(pause).onclick=function(){
2 g3 }) O K3 t3 s" ^+ k; l- Zaudio.pause();; x. C5 K! `* e3 u
console.log(pause);& v) s( y# k" H/ f+ r% {
};
3 E) G/ V" ?2 O5 Z0 N- v// get paused& }% x8 z- k A. x0 d3 D0 ]
document.getelementbyid(get_paused).onclick=function(){
) F) q, R1 R0 nconsole.log(audio.paused: +audio.paused);0 P( d3 L j# b; I9 x
};
; ]2 a: R, o& b: N$ V// get ended% @$ k1 A8 J: |" Z, R
document.getelementbyid(get_ended).onclick=function(){
2 J, c9 b- h2 B) k0 @- ?: o ~0 jconsole.log(audio.ended: +audio.ended);: Y8 G& e7 S2 e. _2 t
};
: M' H% ]2 E w. y; ^# C' R// set volume-
; I6 k6 Q0 ?$ Y. k* R4 ?; ]7 ldocument.getelementbyid(volume_down).onclick=function(){+ s) m3 I4 {5 x5 I4 z
audio.volume-=0.2;: h0 z! Z' t9 ?: f
console.log(volume-0.2);/ n7 o; J1 Y# e/ \0 d
};2 T! v- L7 I- c4 J" E. o$ }
// set volume+4 g' S0 z: P, z
document.getelementbyid(volume_up).onclick=function(){! G. S9 t# r8 o0 K* D3 s
audio.volume+=0.2;
/ p( f0 @) v4 l& Iconsole.log(volume+0.2);
0 {0 H: U6 s% s! H( N v' u};
1 t5 d2 f& I( J, V$ J( u, V// get volume. @ E0 [6 _' V( l0 |2 C
document.getelementbyid(get_volume).onclick=function(){, K- s P: |* F
console.log(audio.volume: +audio.volume);& k4 m: ]0 a4 N& l
};
1 `4 ~5 R2 H! a; @$ A. Z: g// get src
8 {' E8 d6 X' a4 J# N, zdocument.getelementbyid(get_src).onclick=function(){2 E$ Y0 @: J) R$ p" x
console.log(audio.src: +audio.src);
& N# N4 V4 Y$ w- A};0 w* J6 \! T7 q# R* M4 f; _
// set src_music1
1 V/ V5 ^: u3 [1 }, ?& X- g" Z6 Odocument.getelementbyid(play_music1).onclick=function(){
9 v7 x6 T2 l8 G' uaudio.src=./media/music1.mp3;
9 \) R! A- R4 X5 xupdatesrc();! |3 R& Z7 m4 h5 R- ?, b0 f- u3 W
updatecurrentsrc();! s5 S% S6 g* V7 r
console.log(play music1);
* H& f" @ x: d( ^" C};
1 ^: F% S2 T: Z; l/ Y) E: ?% C// set src_music24 c/ K( E: P; i% C0 I8 R2 e9 Y- Z
document.getelementbyid(play_music2).onclick=function(){
* a! t8 Q& y* s- |- K. `/ @audio.src=./media/music2.mp3;
: V- O' q9 \! I4 k# {0 z& n; S# Vupdatesrc();
* m3 J6 u" h( ~updatecurrentsrc();
4 a3 v% X4 x0 K2 k dconsole.log(play music2);
. |; t& q1 s4 Z& P8 u};
& L3 E) Q5 H+ l. j. g// set remove_music% u7 `! h1 ^; [. I1 S
document.getelementbyid(remove_music).onclick=function(){
d& J8 m$ l9 |2 A% ]: u) Daudio.src=;
( @& F5 b8 }/ dconsole.log(remove music);
0 Q3 j6 ?+ n4 B. Q# |6 z};
. p- z- G- _9 v! @5 ^// get currentsrc
|* P) f) A; _* S' O3 Rdocument.getelementbyid(get_current_src).onclick=function(){2 ^6 Q# k- t3 a
console.log(audio.currentsrc: +audio.currentsrc);
C5 U' l2 m, l7 E" Y};
. F! y3 U: p' W2 C% M// get initialtime \2 u, _9 M, L: H
document.getelementbyid(get_initial_time).onclick=function(){
! i) [0 {/ [: T6 C; yconsole.log(audio.initialtime: +audio.initialtime);# @- l" F' y2 |# D! v
};, l- O; P" I' v: u q7 _, c6 W9 v- N
// get duration& s: S3 W* z$ z
document.getelementbyid(get_duration).onclick=function(){
w7 s; `) N7 \console.log(audio.duration: +audio.duration);
& l. Q' _- C. o/ [5 z};! p: B' O) g6 B7 X. w# l2 \' X6 V
// get seeking2 r; Y, J$ A6 [1 P: J/ Y
document.getelementbyid(get_seeking).onclick=function(){2 N* I0 j8 O2 a. ~
console.log(audio.seeking: +audio.seeking);
: |8 p( {. [% E5 l3 \};. { {, ?; R8 {* i
// set currenttime- l0 ^: n' z0 k- r7 U, k; ^
document.getelementbyid(jump_to).onclick=function(){" ~7 k- r+ V1 G- h
audio.currenttime=30;
" [: X) ~# f; k9 K0 a2 S0 kconsole.log(jumpto 30s);
+ p6 l: w" l0 p% F};
/ @1 m9 ?2 N$ R; O" ?// get currenttime8 q% A0 E' U* F; b/ f+ T/ I
document.getelementbyid(get_current_time).onclick=function(){: \, u7 C- h! }" W8 ]4 }! _4 ]
console.log(audio.currenttime: +audio.currenttime);& B$ @0 I P( @* j! k: u( \
};3 w3 J" i( K' O6 m, ]* y
// get played; [7 G" z1 x6 k0 ]9 l7 h" o* V
document.getelementbyid(get_played).onclick=function(){' @3 Y% [2 h( d5 \1 H
console.log(audio.played:);
, C4 `" a; Z- P3 m1 T9 [var ranges=audio.played;
4 ?6 B/ Q3 E7 l4 j+ l1 ivar n=ranges.length;
# g& i2 Y& u7 I2 g5 z& l- X% y' zfor(var i=0;i<n;i++){" f$ a$ f0 r1 u8 N, f6 \
console.log((+ranges.start(i)+,+ranges.end(i)+));
% c& f: i2 l- I' w}' l* }( l4 C+ p6 }4 O' a
};% T1 K/ k! p: t m$ O" w6 @) k- I
// autoplay on
. \. _+ j, e( ^" N! A8 r. t5 k$ S; ?document.getelementbyid(autoplay_on).onclick=function(){9 x0 M8 w' P# d- K! z2 |- B
audio.autoplay=true;! l9 k: I2 S/ P5 X: `
updateautoplay();
; Z: K' Q. V* Gconsole.log(autoplay on);2 j/ k% @0 K" t2 [( a- H+ h
};
- g- Y# r9 b9 K) l( Z2 {% Q// autoplay off4 ^) q( b8 v! P5 c7 P- \+ J
document.getelementbyid(autoplay_off).onclick=function(){
1 C$ M, [6 B/ V. y/ k5 R9 f& Baudio.autoplay=false;
7 P, P* i4 x0 Supdateautoplay(); ?2 q$ |( l* S9 x0 C$ \% R
console.log(autoplay off);: q' H0 ?8 a3 c. |9 _ m
};
- w0 B0 [# U& m6 u% E. n// get autoplay; L9 w0 G' J/ d( | s+ `0 K
document.getelementbyid(get_autoplay).onclick=function(){" Q- `8 A, a& q* N+ n
console.log(audio.autoplay: +audio.autoplay);
" l6 G: m/ N8 @& W3 B! b' ^};
3 U; G% r; t* P// controls show
- w6 s( z5 W1 Z! d. Rdocument.getelementbyid(controls_show).onclick=function(){+ o, ?: V. E/ g0 q- v
audio.controls=true;4 G$ o( x0 n, d
updatecontrols();& \1 v7 ^- j0 O+ b+ s
console.log(controls show);6 Q/ G2 N: m6 Y2 |9 _3 d
};
N3 G; T9 [3 r0 g+ D1 c// controls hide3 [: I$ l# G) ~9 \
document.getelementbyid(controls_hide).onclick=function(){
% [0 y9 F7 c' A, N: X3 Z+ D" N5 Haudio.controls=false;) u' w" O0 V5 V# E
updatecontrols();; R! O/ }! j( k+ g7 h' P" O3 a2 r' k
console.log(controls hide);
& y' G# p A- O- ?};
2 Y2 b# _! u! B" E) @ G) {3 \// get controls& H: N# z& Y N
document.getelementbyid(get_controls).onclick=function(){! ~- i% y( `- m8 Z
console.log(audio.controls: +audio.controls);
) E: B1 i( Q. R4 t- m$ L4 b) B0 T};
3 Y# i& I6 i; t5 f// loop on, h. T3 m1 N) Q' e# ?/ c! M
document.getelementbyid(loop_on).onclick=function(){
; @/ y, Z8 F2 f! x- jaudio.loop=true;
8 w. G$ y& p3 v# Uupdateloop();7 a, C" `4 x' `7 t
console.log(loop on);' s% f$ k1 i1 j' }( X5 k1 ~! X& N
};- C, k1 l# ~% O4 m% \# U" g
// loop off
/ _! D9 b. Z5 \ f5 C" Adocument.getelementbyid(loop_off).onclick=function(){0 v0 n7 r+ p0 d2 c. i5 I* ]( m
audio.loop=false;
3 e0 S/ I3 s, Y& g, \ [updateloop();) Z; o3 I; z1 D8 [- [7 _
console.log(loop off);
' o$ z% K" x1 E1 A" h};
2 ]0 J! C! a5 Q. F; T% l4 Q// get loop
& m! t8 N l% F! y& f7 ^' udocument.getelementbyid(get_loop).onclick=function(){' m" m8 i' Z! E$ M+ Q8 m
console.log(audio.loop: +audio.loop);6 L# ]# Q4 j8 N6 ], S3 @/ g
};0 A* H" L. y! t$ q
// preload metadata
; b3 _1 M5 ^# s* tdocument.getelementbyid(preload_metadata).onclick=function(){1 y( P* Y( |$ `
audio.preload=metadata;% _6 l$ C1 ]+ h; @5 D1 {- B3 H
updatepreload();" r6 ^1 x) n6 c2 t; k8 h
console.log(preload metadata);' v- F' b2 i( v; P5 ~$ h0 n4 H
};' R: K9 U; `' t2 X6 C/ M7 a1 H
// get preload9 ^0 _6 v1 I3 i, F C- o( v& \
document.getelementbyid(get_preload).onclick=function(){
- m; @7 s$ x4 a4 Yconsole.log(audio.preload: +audio.preload);0 g9 \/ Y, D3 T$ n/ r$ F+ g8 X
};
7 w$ {/ f j5 a0 x// get defaultmuted$ Y: m9 d4 R' c3 N3 g6 b0 A3 o" t/ a
document.getelementbyid(get_default_muted).onclick=function(){: t( }- p. N6 u! d# w! P+ q% S
console.log(audio.defaultmuted: +audio.defaultmuted);
8 j; S( X9 ?" ^, C- i& j- o" l* r};
! n+ h2 Q" A, y2 b% T// mute- L: p# N# |- ]
document.getelementbyid(mute).onclick=function(){
3 W$ V6 E# f) V- M+ Baudio.muted=true;- [+ X( S) r0 ~$ R& X! w
updatemuted();+ U! _; p% e6 ]1 j; x
console.log(audio mute);
: c& [# I) f2 p2 ^# h" U};
: m8 G9 I1 O3 k! @! X, _$ [# Z9 @// unmute
' F. Y% i/ ~3 s& [6 d7 V2 }5 E& Ndocument.getelementbyid(unmute).onclick=function(){
" C+ k/ {2 f( I% w: B7 }- zaudio.muted=false;
2 @& |( z" \! ?; Yupdatemuted();+ v3 C$ J+ i, |1 X% l
console.log(audio unmute);
, D, y$ B: f3 B* v9 z' P: s};
7 l% K4 t) f$ K( M7 G// get muted# M4 Q; f" u" _: p0 T# G6 W
document.getelementbyid(get_muted).onclick=function(){
7 }( V7 P$ A2 Z" v# aconsole.log(audio.muted: +audio.muted);
; w1 J/ ~5 w2 v' h/ z};) m# Y! e3 g6 |# C$ J: e
// get defaultplaybackrate
5 k, P- m3 K, F9 ^1 w6 r; Zdocument.getelementbyid(get_default_playback_rate).onclick=function(){
' a2 R2 Q9 t% g$ ~$ O5 Tconsole.log(audio.defaultplaybackrate: +audio.defaultplaybackrate);0 c4 F: `4 K6 u/ R( v
};; l% o- R0 x, t3 y
// set playbackrate-$ w% X- a- i5 i
document.getelementbyid(playback_rate_down).onclick=function(){
9 b( z$ k! T! r9 c& ]audio.playbackrate-=0.2;5 ?1 J6 p7 T) H }0 t! s: \
console.log(playbackrate-0.2);
9 X5 A3 k$ H: w6 t0 \}; G! D- d7 u& c
// set playbackrate+
- D8 s# W2 R/ Q8 h2 O& Xdocument.getelementbyid(playback_rate_up).onclick=function(){. v( F2 g3 \( h9 g O
audio.playbackrate+=0.2;, g: p$ O' Z3 I( q8 G
console.log(playbackrate+0.2);
0 ]' f* |3 P: L; L/ v3 D; z};
6 S! V& K% W# R- O6 g// get playbackrate
/ ~5 j3 |" U% Y, y1 U; \" ~) Z! _document.getelementbyid(get_playback_rate).onclick=function(){
, k6 R* I" [2 h$ \$ U* O9 Econsole.log(audio.playbackrate: +audio.playbackrate);3 G/ A) t% s' `- ~* [
};" u1 z# l6 O2 n9 U
// get networkstate1 i% f/ l; j7 p o- q
document.getelementbyid(get_network_state).onclick=function(){
* L" d3 D! Z6 b) \8 B, Tconsole.log(audio.networkstate: +audio.networkstate);
! e- a9 W% ~2 u: q) ?};
0 o+ w0 Z+ Y1 f, A' M1 s// get readystate
5 _8 Z, @' y- {, [- Ldocument.getelementbyid(get_ready_state).onclick=function(){/ f& L, u! E. E* G8 u+ q$ v
console.log(audio.readystate: +audio.readystate);
: h) |5 n: b. X# P2 P% X0 y3 R) a};6 a( F# F* L' X8 Z& Z9 o
// get buffered
: |2 f _( q# k# r) M! [4 Y4 M0 F9 mdocument.getelementbyid(get_buffered).onclick=function(){
6 ]0 I! `5 H# cconsole.log(audio.buffered:);
* p3 h- k: y6 Kvar ranges=audio.buffered;
) r5 m: h( A' E, }& C8 {9 I: kvar n=ranges.length;5 Q2 N2 w0 y0 ^6 j
for(var i=0;i<n;i++){% W- i- O4 p* h9 t
console.log((+ranges.start(i)+,+ranges.end(i)+));
1 u( y: O1 w: N7 ~& S- P}
1 [: i( A) E- D% Q};1 B/ Z1 g9 L% s5 i: N7 [0 |
// get seekable2 v# Q7 e8 e C$ H
document.getelementbyid(get_seekable).onclick=function(){, _2 m8 r. |" V4 K/ j; D5 c
console.log(audio.seekable:);7 k- e9 n1 j* P0 k0 j
var ranges=audio.seekable;+ \6 _; B4 M6 q% {4 c2 o
var n=ranges.length;
. L7 U! e& F- S" cfor(var i=0;i<n;i++){8 |2 _, Y, K6 [+ k$ o* V
console.log((+ranges.start(i)+,+ranges.end(i)+));
M$ |* X" i7 O1 R% z- V}
7 _ n; l* [/ S2 C' T};
n; Z2 ~1 T W* P// dom events8 o0 x" K X% b+ s
// abort" R9 q, {8 E' C% j. v9 n+ Q! z
audio.addeventlistener(abort,function(){
9 u; b# C9 M/ s. rconsole.log(event:abort);: r. a+ W* @# s! O
});
: P+ T# y% V) Q// canplay
6 R, w7 C# s! s' [$ yaudio.addeventlistener(canplay,function(){8 J0 d; K3 B5 J$ B8 `0 K: h: n' ^
console.log(event:canplay);6 q" f D& O) U2 {5 v# _1 W% r
});
) ?: J- p$ `# k# Y. ^, T// canplaythrough' `4 n! f' n- s% G/ t `1 Y+ l7 T
audio.addeventlistener(canplaythrough,function(){
5 @ t0 H1 m& @8 ^4 Sconsole.log(event:canplaythrough);- P, T5 e. ~* X( i8 S7 E
});
, a! Q" `- p* T$ }// durationchange+ d1 h2 n. _& F" V4 C4 M/ {
audio.addeventlistener(durationchange,function(){
( z: q9 Q2 K2 l8 xupdateduration();
/ b6 o, @6 f6 q$ t: Q& q+ h: Nconsole.log(event:durationchange);
+ W' s) ^5 |4 t6 C. c6 c});6 H7 f0 o) Z6 e# V" k B
// emptied% x5 V) |6 \- l7 ^4 q
audio.addeventlistener(emptied,function(){
- `' h3 z* r9 h; X- C6 p! rupdatesrc();8 l" i9 g8 Z2 f" \
updatecurrentsrc();" B2 I2 U9 e* \% D1 w+ C% j+ H6 r c
updateduration();. H) P i H; C9 @* ?1 b" `/ p
updatepaused();8 `* S5 M) u5 X3 z) |
updatenetworkstate();' I7 L% b2 z1 g0 v9 y8 O
updatereadystate();3 A9 g" X" X m0 G) b3 \* G. y0 x4 n
updatebuffered();
7 m" l) S) Q: X( ^ w5 bupdateseekable();. `' _) L8 k( r9 R4 Z
updateplayed();/ Q, ]$ V+ j3 J' J! R$ j3 D
console.log(event:emptied);
3 s' x; E: [+ B8 }* Z; x* l});% M- j' b* A2 |3 ?: |) {! V
// ended
1 ?( J' e4 O5 {: ?0 X, naudio.addeventlistener(ended,function(){1 P! S. K6 j4 `9 T- v* I
updateended();
6 l; y# Q- ~4 xconsole.log(event:ended);6 Q- Z: S1 [/ \: W: Y3 s
});
( ^# O& g7 Q9 \) J! l// loadeddata
" E0 o$ Q: s7 \, X2 t& O) W {6 H$ M5 u# Saudio.addeventlistener(loadeddata,function(){" s; m4 ?6 k2 Y( r) w
updatenetworkstate();* e! E( P9 {( R, c. R7 U7 M
updatereadystate();
+ J% c' g9 S% e1 S W% eupdatebuffered();
2 w/ o" B$ W9 L- `: d) Tupdateseekable();
2 \8 ]; o: @1 s0 uconsole.log(event:loadeddata);3 G8 O# n9 O5 ^/ f
});
0 X, [4 A: n1 ~- c$ Q// loadedmetadata
3 t8 D8 ~2 T' C' z Faudio.addeventlistener(loadedmetadata,function(){9 P4 }- l; l8 `, L% o- ^; a
console.log(event:loadedmetadata);" J+ P2 Z0 k" R- q! h: M
});( W l4 y: n& f8 x" x6 h, Q8 ^
// loadstart
7 B6 @5 A: `: G, F5 aaudio.addeventlistener(loadstart,function(){4 o: s$ s1 G0 V9 c. {+ n
console.log(event:loadstart);, R4 W2 }# T8 x$ V9 N" c
});
0 d5 c+ b7 O7 w6 w5 Z4 ?// pause
6 }% H& e; ` e# d2 Taudio.addeventlistener(pause,function(){6 C Z* y; p9 O6 j' F
updatepaused();
- M4 E; V% O$ P4 t) Hconsole.log(event:pause);
5 o& Y! |8 H5 S});7 C; T* h) z, F9 Y1 z
// play
2 S, |5 o0 o8 W0 T. |5 Eaudio.addeventlistener(play,function(){" ~" `2 O$ A3 b
updatepaused();
8 C6 E6 d: n+ Xconsole.log(event:play);
+ i. I n1 a* ]8 L9 X& s- a});
' n9 c- T6 A* }" K' R// playing+ |, k4 b1 u1 d* |, `
audio.addeventlistener(playing,function(){- j F3 J4 F+ ^/ Z1 I
console.log(event:playing);
0 Q- q! e& g% y/ Q$ L5 K3 B});
p' ^9 O/ W" l r0 q// progress
5 I) l. d; M3 G9 faudio.addeventlistener(progress,function(){" E6 i4 d9 p) r
updatenetworkstate();
. s1 a0 @7 X$ O0 I2 D# Yupdatereadystate();% M' [8 q M- j
updatebuffered();
# z; l, T$ A- @2 vupdateseekable();
) d2 ?5 Q4 D, s' D7 r6 Econsole.log(event:progress);/ m% M, b \* [ a9 o: a
});
" {% j! T9 I9 M2 y7 V// ratechange
. Q# X3 E# \$ ]& v8 ^audio.addeventlistener(ratechange,function(){
/ m2 Q) Y m4 V. _8 T+ Pupdateplaybackrate();
8 m7 ~% d' z; D4 aconsole.log(event:ratechange);
" T: u8 @5 m( P5 A/ D% e0 K( F( D});8 F4 A. h4 m5 ^0 V
// seeked5 {8 }$ a9 y/ e* q; E! ?
audio.addeventlistener(seeked,function(){
& N" x* e+ `) S. I1 {$ x- O7 Zconsole.log(event:seeked);
) `7 n0 K8 Y8 d$ Z3 |/ F" h$ y});+ o6 w' {3 Y- s" n
// seeking
# b+ e0 [/ C4 q7 {/ k8 G4 {* vaudio.addeventlistener(seeking,function(){
6 C* x) c1 H& R+ `console.log(event:seeking);
$ b2 `+ Z) r" t0 Z});
+ H. m( d, J8 M9 C% A( g// stalled `, @% m$ ^) }2 Q5 a9 t4 B' k& v
audio.addeventlistener(stalled,function(){) f# G Y; c% Q# o4 R8 E( s* O! K
console.log(event:stalled);3 B6 @# C& X/ O3 u j
});3 ?2 l* V& q5 X+ m0 ]+ l5 J
// suspend
* J- p; {1 t; F& ]/ yaudio.addeventlistener(suspend,function(){$ [4 Z) w+ G1 E: x) `/ b
console.log(event:suspend);/ m! h' b" \# k1 g# g- r" `
});" Y8 E9 f' g2 C& b; u
// timeupdate
0 x* T* T! c) s% U1 i2 Eaudio.addeventlistener(timeupdate,function(){0 O8 Y6 ]1 E0 _8 e$ _
updatecurrenttime();
3 [% j5 }: m4 B/ P- eupdateended();
, q% N( R" `3 {5 A" gupdateplayed();
: f! d& _2 w9 m; h' I: Lconsole.log(event:timeupdate);9 ~' I' `6 Z _: V `% z9 C0 u7 J1 U
});" Y8 j9 O/ D0 _6 E0 U% T
// volumechange
. B2 k4 L6 r: @% ?audio.addeventlistener(volumechange,function(){
8 p" e9 M- w! j- H$ C. Pupdatevolume();$ n) ?- C" r* h
console.log(event:volumechange);( w }& }% w5 F. e2 L3 H
});. H' _$ p I# m
// waiting; }+ B% U' P7 W$ F/ n
audio.addeventlistener(waiting,function(){: J& p5 p5 D4 v, _
console.log(event:waiting);
, }2 m/ {9 p; }1 F/ U2 _$ |7 Z8 z: Y});
. K p8 P1 J% U4 |0 O2 t' o. kupdateautoplay();
, d8 g s6 w {- h. J% b" }updatecontrols();+ ]1 O5 J8 J; k
updatedefaultmuted();
7 J: b* T+ \% Lupdatedefaultplaybackrate();! a/ q% E7 u% C3 ^$ q* U6 X
updateloop();
+ U- w0 @& u* ?- n7 {updatepreload();
/ Q1 m: `, x. `5 L7 Oupdatesrc();
% b4 C1 I& m; Z0 H2 i8 I" q3 S# _" Iupdatecurrentsrc();
; ^% p: _5 }: {8 b( Vupdateduration();$ y5 r# W9 P- [' V
updatecurrenttime();
4 |- O4 Y4 q, e$ D* {updatevolume();
" C4 U b9 D; }* t' kupdatepaused();
- K% S7 Q1 a6 C/ p, I vupdatemuted();
$ s1 A; S& O& t. ^& Vupdateended();$ q8 y" f1 {4 v: k
updateplaybackrate();
3 H1 f% S* `+ a' h+ y8 x3 @9 [/ Z4 vupdatenetworkstate();2 c) _/ E& _' @) J
updatereadystate();
0 G- Z' j& H) R% p- A& {& Uupdatebuffered();/ ^4 R+ \) d' ]' T
updateseekable();- O8 S5 A: c# b" Q- V! s6 t
updateplayed();
x* i7 v3 | h, ~$ j+ pupdateerror();+ A; O0 Y4 y, w9 o; P% @& Q( Z
};
7 P3 P8 M) ?* m' t0 s/ s// functions to update info table
, B1 ^! k: N/ P8 v/ x// autoplay9 h9 _% q4 C. H3 g+ @
function updateautoplay(){
! x/ X: W4 K" j6 ydocument.getelementbyid(autoplay).innerhtml=audio.autoplay;: q0 A, K+ Y7 i/ K0 @
}
' K2 [( z+ Y& L( E' F// controls
2 c6 c, z- L' s0 }& i8 G' Vfunction updatecontrols(){) c' T: u, }% J* G
document.getelementbyid(controls).innerhtml=audio.controls;/ V/ I5 {) x' ~& d6 e7 K- ]
}( G0 G, H, j5 L6 q
// defaultmuted
, \- P* I' ]! O( @9 [- X* \$ jfunction updatedefaultmuted(){
# x' A5 v( O, j+ U, O0 D8 Cdocument.getelementbyid(default_muted).innerhtml=audio.defaultmuted;
) j! O3 q/ A1 T, e}
' Y$ x9 o* u6 K# Q' _' r9 g// defaultplaybackrate4 g% u. {5 ?& Z0 ^+ D
function updatedefaultplaybackrate(){
9 D, @& t$ M, r5 ?+ M ?* tdocument.getelementbyid(default_playback_rate).innerhtml=audio.defaultplaybackrate;
( K5 B { [9 G+ ~! u+ O. Y$ g2 s g}& c$ j, k/ Y" l& w) x8 g
// loop3 X7 B2 u# F% y
function updateloop(){
. Q. H! ]& b" \document.getelementbyid(loop).innerhtml=audio.loop;
0 J" w: {$ L. I5 w" g1 g$ B F" e" f}
0 ?& w& U- ` }& M6 N. V+ w// preload
7 c5 e$ C B$ C0 S, L" I$ Ffunction updatepreload(){
! B; e: O& [4 O) |8 H5 E$ {0 c+ E* fdocument.getelementbyid(preload).innerhtml=audio.preload;7 ] B9 P' r: ?
}
8 O+ Q2 [- z+ ^' _3 {7 z// src
4 F) X' Q- D# ^function updatesrc(){
r: w! Y# ~. h; z& xdocument.getelementbyid(src).innerhtml=audio.src;, K6 A: X: ]7 J; u1 [' c' L. I
}
4 w9 S% Y) G, j0 e// currentsrc3 ~$ e ]- ~7 ]* C/ n- |) u) \8 v2 b2 m
function updatecurrentsrc(){4 I% F9 l7 O6 r( z2 m
document.getelementbyid(current_src).innerhtml=audio.currentsrc;/ c" B. g, }* ^1 j3 ~) O( a u
}
: M+ _/ L, O2 @/ b" s# x% _! T// duration
3 G, |' M2 B) ~& M6 s! zfunction updateduration(){
# R8 ?: b& H! [9 c+ u4 Y6 pdocument.getelementbyid(duration).innerhtml=audio.duration;5 s8 b: |* ]! V, W2 y. x; w8 _% U
}
4 f5 }& j! z& o$ Y// currenttime) E. A6 x- H& g0 b# w- i
function updatecurrenttime(){
' f' i7 z _ p3 r u! ydocument.getelementbyid(current_time).innerhtml=audio.currenttime;; Q. L { d. Y* P" R; p/ g
}4 d6 |( G9 R, f8 U0 o y2 }8 {9 I. l
// ended
& R4 | U( S" _% ~" ufunction updateended(){
. |0 Y5 V$ r G# }3 d3 cdocument.getelementbyid(ended).innerhtml=audio.ended;, A+ }$ @; K% {2 A+ h; u/ m
}
# o( h7 D( y1 ]7 E$ c// paused
) s2 Q+ V4 N: O0 p; L" |! ^function updatepaused(){' d/ N, Y, |8 P2 l, H" S% C( L; U
document.getelementbyid(paused).innerhtml=audio.paused;
* L& {" T2 H; t% I8 A5 N8 D}/ r: n7 l9 r" m% A4 k5 w7 F/ c7 t
// muted
2 j$ V, `3 M5 G4 V l2 Sfunction updatemuted(){3 I* ?9 T4 H% @- T7 e5 ^
document.getelementbyid(muted).innerhtml=audio.muted;: a* n. r; h3 h# ]5 L3 l# H6 Q4 H
}
( H9 T8 q6 S$ W: t& d// volume% H- b9 V L8 H: Z. \
function updatevolume(){
/ }( D, P- \5 d4 v3 B0 Udocument.getelementbyid(volume).innerhtml=audio.volume;; m- O& N9 p) _$ y
}
# k) ?# K! u3 O$ `7 M7 A* ^+ z// playbackrate% {9 g& J* J7 g1 H/ |9 ^7 g
function updateplaybackrate(){8 {1 p) o1 V9 V4 y
document.getelementbyid(playback_rate).innerhtml=audio.playbackrate;9 ^2 S. K0 |) r6 s1 x* C
}
& S( V) J) s4 O n// networkstate! M5 [6 }2 Y% x, ?
function updatenetworkstate(){" ^5 V; s' \4 x I7 c- T; ^( s
document.getelementbyid(network_state).innerhtml=audio.networkstate;3 W! {; V9 S* g! E! w: c
}# |: H2 S- R7 \$ r6 d: t
// readystate5 B! w+ d4 j* I+ G( O. B. h& |
function updatereadystate(){
0 g1 w9 n: t# v" a) ^# @document.getelementbyid(ready_state).innerhtml=audio.readystate;! M0 E6 i# i2 E+ t
}" I: `$ g: ?2 U* B
// buffered
4 I* N- s4 d4 J4 i, Efunction updatebuffered(){
% V# P# ?% u2 @. q% j. A3 |var ranges=audio.buffered;! c7 V6 H4 m" n, G n8 ?
var str=;' L$ P8 e3 W1 b$ Q( o# v Z
var n=ranges.length;9 g3 j4 [9 ^$ z2 r! V
for(var i=0;i<n;i++){
; n' \6 `, h4 z, Ustr+=(+ranges.start(i)+,+ranges.end(i)+);
- m* j) F4 O: Sif(i!=n-1){
0 u z% {7 |2 mstr+=
6 L% n% N( ?3 d+ _4 p G;2 o( D2 R4 Y0 ~3 h1 f& C# z
}8 u# L" ?3 x9 x5 X+ C
}! E3 ~$ {+ j2 S4 I: W" x
document.getelementbyid(buffered).innerhtml=str;2 k) E- b: S6 A {* A
}
* h! X5 q# {6 b: N// seekable
' \" I# g. f* W: n5 a- Afunction updateseekable(){
/ K. p1 G, m' j' |% k9 ^var ranges=audio.seekable;
! \; ?, {7 p- {- O0 f+ U7 hvar str=;
8 ? K2 j& w6 g" B! Pvar n=ranges.length;
0 o* Q. a& `0 w- i; O1 L2 r, vfor(var i=0;i<n;i++){
. A. W+ t# A/ _' { jstr+=(+ranges.start(i)+,+ranges.end(i)+);
4 Q# C( n, ]1 H% T+ \7 M0 Rif(i!=n-1){
+ k+ p- \5 B6 w7 U. Ystr+=
' E+ F" }0 U, ]1 N: A4 p9 r( Y( ^;5 L6 G+ s! S: i' n, T
}0 K* k& R2 v Y, ^5 r5 G2 m
}. h$ U% t( h3 `; i x
document.getelementbyid(seekable).innerhtml=str;) y* ^# U/ [7 q1 U. J( u) f
}
- m6 v' B4 ~+ K" M; F. ^& m// played
0 g( f- T* P8 \function updateplayed(){" {5 _1 z& g5 j+ ?2 p! r8 e2 E, \8 u! C
var ranges=audio.played;
$ J( r+ ] l$ |0 Evar str=;
" Z# `: y9 K/ O# Lvar n=ranges.length;
; H1 D) l0 B/ l1 _5 nfor(var i=0;i<n;i++){
; R* j1 E; f+ R4 ?7 R& _str+=(+ranges.start(i)+,+ranges.end(i)+);
. e9 Y2 ]. v2 K* oif(i!=n-1){+ x5 a- t: ?; K2 Y8 H6 X" L
str+=) M) w0 y: } B$ F+ M2 p$ z
;
7 X4 n: K2 z6 }2 ?}
+ C" k6 j8 M1 I8 Z}
: D0 V6 G7 D1 R6 hdocument.getelementbyid(played).innerhtml=str;
0 Q5 S4 Q% N. _( D( ]" x} D5 I2 f, M# ~: ^
// error
8 q* q1 C2 N( s i% lfunction updateerror(){# R9 H- y; ]* r9 t3 D# v J% ~; L
document.getelementbyid(error).innerhtml=audio.error;" T$ P0 E: }' P7 Y, O5 T
}# y) k% J" K" v
5 C) ]3 ]/ L, F) n更多网页制作信息请查看: 网页制作 |
|