Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thực thi script khi đang seek (onseeking) video/audio trong HTML?

Sự kiện onseeking kích hoạt khi người dùng bắt đầu di chuyển vị trí phát (seeking) trên video/audio (kéo thanh tiến trình, nhấn phím tắt). Phát lại khi seeking kết thúc: onseeked.

Ví dụ

<!DOCTYPE html>
<html>
  <body>
    <video controls onseeking="display()">
      <source src="/html5/foo.ogg" type="video/ogg" />
      <source src="/html5/foo.mp4" type="video/mp4" />
      Trình duyệt không hỗ trợ video.
    </video>
    <script>
      function display() {
        alert("Đang seek...");
      }
    </script>
  </body>
</html>

Các sự kiện seek liên quan

Sự kiện Kích hoạt
seekingBắt đầu seek (đang di chuyển)
seekedKết thúc seek (đã đến vị trí mới)
timeupdateVị trí phát thay đổi (phát bình thường hoặc seek)

Thuộc tính media liên quan

  • currentTime: Vị trí hiện tại (giây) — đọc/ghi để seek bằng JS.
  • duration: Tổng thời lượng.
  • seeking (property): true nếu đang seek.

Ví dụ JS seek tùy chỉnh

<video id="v" src="video.mp4" controls></video>
<button onclick="document.getElementById('v').currentTime = 30">Nhảy đến 30s</button>
<script>
  document.getElementById('v').addEventListener('seeking', () => console.log('Đang seek...'));
  document.getElementById('v').addEventListener('seeked', () => console.log('Đã seek xong'));
</script>