The HTML element is used to show a video on a web page.
The HTML Element
To show a video in HTML, use the element:
Example
Your browser does not support the video tag.
Try it Yourself »
How it Works
The controls
attribute adds video controls, like play, pause, and volume.
It is a good idea to always include width
and height
attributes. If height and width are not set, the page might flicker while the video loads.
The element allows you to specify alternative video files which the browser may choose from. The browser
will use the first recognized format.
The text between the and
tags will only be displayed in browsers that do not support the
element.
HTML Autoplay
To start a video automatically, use the autoplay
attribute:
Example
Your browser does not
support the video tag.
Try it Yourself »
Note: Chromium browsers do not allow autoplay in most cases. However, muted autoplay is always allowed.
Add muted
after
autoplay
to let your video start playing automatically [but muted]:
Example
Your browser does not support the video tag.
Try it Yourself »
Browser Support
The numbers in the table specify the first browser version that fully supports the element.
4.0 | 9.0 | 3.5 | 4.0 | 10.5 |
HTML Video Formats
There are three supported video formats: MP4, WebM, and Ogg. The browser support for the different formats is:
Edge | YES | YES | YES |
Chrome | YES | YES | YES |
Firefox | YES | YES | YES |
Safari | YES | YES | NO |
Opera | YES | YES | YES |
HTML Video - Media Types
MP4 | video/mp4 |
WebM | video/webm |
Ogg | video/ogg |
HTML Video - Methods, Properties, and Events
The HTML DOM defines methods, properties, and events for the element.
This allows you to load, play, and pause videos, as well as setting duration and volume.
There are also DOM events that can notify you when a video begins to play, is paused, etc.
For a full DOM reference, go to our HTML Audio/Video DOM Reference.
HTML Video Tags
Defines a video or movie | |
Defines multiple media resources for media elements, such as and | |
Defines text tracks in media players |
bởi Nguyễn Văn Hợi vào 10/07/2019. Lượt xem: 1,882
Phát video trong HTML
Trước HTML5, một video chỉ có thể được phát trong trình duyệt với một trình hỗ trợ mở rộng [như Flash Player].
Phần tử HTML5 chỉ ra cách thức tiêu chuẩn để nhúng video vào trang web.
Phần tử HTML
Để hiển thị một video trong HTML, ta dùng phần tử :
Ví dụ
Your browser does not support the video tag.
Cách thức hoạt động
Thuộc tính controls thêm trình điều khiển video như play, pause, và volume.
Bạn cũng nên thêm thuộc tính width và height, nếu không đặt kích thước cho video, trang có thể bị nhấp nháy [do chưa xác định được kích thước video] khi tải trang.
Phần tử cho phép bạn chỉ ra đường dẫn của video để trình duyệt lựa chọn. Trình duyệt sẽ lấy định dạng đầu tiên nó nhận ra.
Văn bản đặt giữa cặp thẻ và chỉ hiển thị nếu trình duyệt không hỗ trợ phần tử video.
HTML autoplay
Để phát tự động một video ta dùng thuộc tính autoplay.
Ví dụ
Your browser does not support the video tag.
Thuộc tính autoplay không hoạt động trên thiết bị di động như iPad và iPhone.
HTML video – Trình duyệt hỗ trợ
Trong HTML5, có 3 định dạng video được hỗ trợ : MP4, WebM, và Ogg.
Trình duyệt hỗ trợ cho các định dạng khác nhau là:
Browser | MP4 | WebM | Ogg |
Internet Explorer | YES | NO | NO |
Chrome | YES | YES | YES |
Firefox | YES | YES | YES |
Safari | YES | NO | NO |
Opera | YES [from Opera 25] | YES | YES |
Phân loại video
Định dạng File | Kiểu |
MP4 | video/mp4 |
WebM | video/webm |
Ogg | video/ogg |
HTML video – Phương thức, thuộc tính và sự kiện.
HTML5 xác định DOM phương thức, thuộc tính, và sự kiện cho phần tử .
Điều này cho phép bạn tải, phát và dừng video cũng như cài đặt độ dài và âm lượng.
Ngoài ra còn có các sự kiện DOM có thể thông báo cho bạn khi video bắt đầu phát, bị tạm dừng, v.v.
Ví dụ sử dụng JavaScript
Play/Pause
Big
Small
Normal
Your browser does not support HTML5 video.
var myVideo = document.getElementById["video1"];
function playPause[] {
if [myVideo.paused]
myVideo.play[];
else
myVideo.pause[];
}
function makeBig[] {
myVideo.width = 560;
}
function makeSmall[] {
myVideo.width = 320;
}
function makeNormal[] {
myVideo.width = 420;
}
Video courtesy of Big Buck Bunny.
Để tham khảo thêm về DOM các bạn có thể vào đường dẫn HTML5 Audio/Video DOM Reference.
Các thẻ video HTML5
Thẻ | Mô tả |
| Xác định một video hoặc phim |
| Xác định tài nguyên cho các phần tử media như và [dùng thuộc tính src để dẫn nguồn cho video và audio] |
| Xác định phụ đề trong trình phát media |
Trích nguồn từ: [//www.w3schools.com/html/html5_video.asp]