HTMLVideoElement、HTMLAudioElement 繼承自 HTMLMediaElement,統一 API 覆蓋獲取、控制、狀態、事件全生命週期。
| 分類 | 屬性/方法 | 說明 |
|---|---|---|
| 錯誤 | error | null=正常;code: 1用戶終止 2網絡錯誤 3解碼錯誤 4URL無效 |
| 網絡 | currentSrc | 當前資源 URL |
| src | 獲取/設置資源 URL | |
| canPlayType(type) | 檢測編碼支援度("probably"/"maybe"/"") | |
| networkState | 0未初始化 1閒置 2下載中 3無資源 | |
| load() | 重新載入 src | |
| buffered | 已緩衝區域 TimeRanges | |
| preload | none/metadata/auto | |
| 就緒態 | readyState | 1無 2有元數據 3有當前幀 4有未來幀 5可流暢播放 |
| seeking | 是否正在 seek | |
| 播放控制 | currentTime | 當前播放位置(可寫入跳轉) |
| duration | 總時長(流媒體為 Infinity) | |
| paused | 是否暫停 | |
| playbackRate / defaultPlaybackRate | 當前/默認播放速率 | |
| played / seekable | 已播放/可 seek 區域 TimeRanges | |
| ended | 是否結束 | |
| autoplay / loop | 自動播放/循環 | |
| play() / pause() | 播放/暫停 | |
| 音量/控制條 | controls | 是否顯示原生控制條 |
| volume (0-1) | 音量 | |
| muted | 靜音 |
buffered.length // 緩衝段數
buffered.start(index) // 第 index 段起始時間
buffered.end(index) // 第 index 段結束時間
loadstart, progress, suspend, abort, error, stalled, play, pause, loadedmetadata, loadeddata, waiting, playing, canplay, canplaythrough, seeking, seeked, timeupdate, ended, ratechange, durationchange, volumechange 等。可用 addEventListener 統一監聽。
function Player() {}
Player.prototype = {
box: null,
read(id) { this.box = document.getElementById(id); },
play() { this.box.play(); },
pause() { this.box.pause(); },
src(url) { this.box.src = url; },
currentTime() { return (this.box.currentTime/60).toFixed(2); },
duration() { return (this.box.duration/60).toFixed(2); }
};
配合 jQuery 綁定播放/暫停/顯示時長/實時進度條更新。