HTML5中的新元素標簽
- src:音頻文件路徑。
- autobuffer:設置是否在頁面加載時自動緩沖音頻。
- autoplay:設置音頻是否自動播放。
- loop:設置音頻是否要循環播放。
- controls:屬性供添加播放、暫停和音量控件。
這些屬性和<video>元素標簽的屬性很類似
如何工作
<audio src="song.mp3"></audio>
同樣 <audio> 與 </audio> 之間插入的內容是供不支持 audio 元素的瀏覽器顯示的:
<audio src="song.ogg" controls="controls"> Your browser does not support the audio tag. </audio>
常用的控制函數:
- load():加載音頻、視頻軟件
- play():加載並播放音頻、視頻文件或重新播放暫停的的音頻、視頻
- pause():暫停出於播放狀態的音頻、視頻文件
- canPlayType(obj):測試是否支持給定的Mini類型的文件
只讀的媒體屬性:
- duration獲取媒體文件的播放時長,以s為單位,如果無法獲取,則為NaN
- paused如果媒體文件被暫停,則返回true,否則返回false
- ended如果媒體文件播放完畢,則返回true
- startTime返回起始播放時間
- error返回錯誤代碼
- currentSrc以字符串形式返回正在播放或已加載的文件
可腳本控制的屬性值:
- autoplay:自動播放已經加載的的媒體文件
- loop為true:的時候則設定為自動播放
- currentTime:以s為單位返回從開始播放到目前所花的時間
- controls:顯示或者隱藏用戶控制界面
- volume:音量值,從0.0至1.0之間
- muted:設置是否靜音
- autobuffer:是否進行緩沖加載
"我"的成長獨白 ESTELLE'S AUDIO PLAYER
首先,先介紹一下"我"自己,和你一樣,我也是有生命的個體,但 (ke) 是 (xi) ,我比你更有靈性 [ 傲嬌 ]
audio.html
<body> <audio id='audio'>你的瀏覽器不支持喔!</audio> <div class='MusicPanel'> <div class='PanelLeft'><div class='circle'><span class='icon glyphicon-heart'></span></div></div> <!-- Like Button --> <div class='PanelRight'> <div class='Prev'><span class='icon glyphicon-step-backward'></span></div> <!-- Prev Song Button --> <div id='Play' class='Play'><span class='icon glyphicon-play'></span></div> <!-- Play & Pause Button --> <div class='Next'><span class='icon glyphicon-step-forward'></span></div> <!-- Next Song Button --> <div class="Song"><span class='SongAuthor'>Greyson Chance</span></br><span class='SongName'>Summertrain</span></div> <!-- Song Title --> <div class="Process"> <!-- Process --> <div class="ProcessAll" ></div> <!-- ProcessAll --> <div class="ProcessNow"></div> <!-- ProcessNow --> <div class="SongTime">00:00 | 00:00</div> <!-- Time --> </div> <!-- Process End --> </div> <!-- PanelRight End --> </div> <!-- MusicPanel End --> </body>
進度條邏輯
繪制兩條重疊的進度條,一條指示總進度 ProcessAll,另一條指示已播放的進度 ProcessNow
根據已播放的時間占總時間比,設置 ProcessNow 下 Width 的值,根據CSS的層疊規范,后寫的 ProcessNow 的顏色層在最高層
詳見 audio.JS 函數 TimeSpan()
其次,我不能不穿衣服呀,我需要一件合適的袈裟,人見人愛,花見花開,車見車那啥兒,嘿嘿
audio.css
.MusicPanel{ width: 400px; height: 100px; margin: 0 auto; border:1px solid #76dba3; } .MusicPanel .PanelLeft{ width: 100px; height: 100px; display: inline-block; text-align: center; background: #76dba3; } .MusicPanel .PanelRight{ width: 260px; height: 80px; display: inline-block; padding: 10px 20px; position: absolute; background: #fdfef6; } .Prev,.Play,.Next{ display: inline-block; margin-right: 5px; } .Prev,.Next{ filter:alpha(opacity=30); -moz-opacity:0.3; opacity:0.3; cursor: not-allowed; } .Prev:hover,.Next:hover{ filter:alpha(opacity=30); -moz-opacity:0.3; opacity:0.3; cursor: not-allowed; } .Song{ display: inline-block; padding-left: 15px; } .SongTime{ float: right; font-family: cursive,microsoft Yahei; font-size: 14px; color:#ee8a87; } .Song:hover{ cursor: default; } .SongAuthor{ font-family: cursive,microsoft Yahei; color:#ee8a87; font-size: 18px; } .SongName{ font-family: cursive,microsoft Yahei; color:#ee8a87; font-size: 13px; } .PanelRight .icon{ display: inline-block; color:#f06d6a; font-size:22px; transition:0.3s; } .PanelRight .Play .icon:hover { cursor: pointer; color: #dd2924; } .PanelLeft .circle{ width: 40px; height: 40px; display: inline-block; margin-top: 30%; line-height: 40px; border-radius: 50%; border:1px solid white;