This topic describes how your application can play video content using the HTML5 "video" element.
video"
HTMLMediaElement Interface
HTML video element
HTML5 Basic VideoPlayer
HTML5 VideoPlayer with Subtitles
The HTML5 video element is a standard way to embed video content in Web applications. You can use the video element to stream video files on a Samsung device. The video element inherits all the properties and methods of the HTMLMediaElement interface.
video
Samsung devices support a variety of standard video formats. For more information on the supported formats, see the Media Specifications.
To create a video player using HTML5:
Create a video element, and add attributes and properties. You can create the element in HTML or in JavaScript:
In HTML:
<body> <video id='video' width='700' height='400' poster='yourPosterURI' src='yourVideoURI' controls style='background:black'> </video> </body>
The following are some basic attributes for the HTML video element. For more information, see the HTML <video> element.
autoplay
controls
height
loop
muted
poster
preload
src
width
In JavaScript:
var videoElement = document.createElement('video'); videoElement.id = 'video'; videoElement.width = '700'; videoElement.height = '400'; videoElement.poster = 'yourPosterURI'; videoElement.src = 'yourVideoURI' ; videoElement.controls = true; videoElement.style.background='black'; videoElement.load(); document.body.appendChild(videoElement);
The following are some basic properties for the JavaScript video element. You can also use the HTML attributes as properties. For more information, see the HTMLMediaElement Properties.
audioTracks
buffered
currentTime
duration
ended
paused
volume
Note Setting a black background for the player is recommended.
Setting a black background for the player is recommended.
Define event listeners. For more information on the available events, see Media events.
var videoElement = document.getElementById('video'); videoElement.addEventListener('loadeddata', function() { console.log('Video loaded.'); }); videoElement.addEventListener('timeupdate', function() { console.log('Current time: ' + videoElement.currentTime); }); videoElement.addEventListener('seeked', function() { console.log('Seek operation completed.'); videoElement.play(); }); videoElement.addEventListener('stalled', function() { console.log('Video stalled.'); }); videoElement.addEventListener('ended', function() { console.log('Video ended.'); });
Control the playback:
Use the HTMLMediaElement Methods to control the video player:
addTextTrack()
canPlayType()
load()
source
pause()
play()
To move to a specific time in the media, set the currentTime property. The currentTime property sets or returns the current playback position, in seconds. When you set this property, the playback jumps to the specified position in the video.
var videoElement = document.getElementById('video'); /* Move 10 seconds forward */ var seekTime = videoElement.currentTime + 10; if (seekTime >= 0 && seekTime <= videoElement.duration) { videoElement.currentTime = seekTime; } /* Move 10 seconds back */ var seekTime = videoElement.currentTime - 10; if (seekTime >= 0 && seekTime <= videoElement.duration) { videoElement.currentTime = seekTime; } /* Move currentTime to 30 seconds */ videoElement.currentTime = 30;
Note If the meta data of the media file is loaded, you can move to the specified time position even when the media file is not playing. For more information, see Retrieving Media Information.
If the meta data of the media file is loaded, you can move to the specified time position even when the media file is not playing. For more information, see Retrieving Media Information.
To control the playback rate (trick play) of the media, set the playbackRate property as a multiplier for the playback rate. Positive values for the playbackRate property play the media forwards, while negative values cause the media to play in reverse.
playbackRate
var videoElement = document.getElementById('video'); /* Set to half speed */ var speed = 0.5; videoElement.playbackRate = speed; /* Set to double speed */ var speed = 2.0; videoElement.playbackRate = speed;
To add multiple videos to a video player, for playing in sequence:
Create a source element for each video file you want to add.
<video id='video' width='700' height='400'> <source class='active' src='yourVideoURI_1' ></source> <source src='yourVideoURI_2' ></source> </video>
Note To improve application performance, you can optionally add the type attribute to each source element. By indicating the MIME types of the media files using the type attribute, you can immediately skip media in formats that are not supported by the device.
To improve application performance, you can optionally add the type attribute to each source element. By indicating the MIME types of the media files using the type attribute, you can immediately skip media in formats that are not supported by the device.
type
When the first video has finished playing, start playback for the next video:
var videoElement = document.getElementById('video'); videoElement.addEventListener('ended', function(e) { var activeSource = document.querySelector('#video source.active'); var nextSource = document.querySelector('#video source.active+source') || document.querySelector('#video source:first-child'); // deactivate current source, and activate the next one activeSource.className = ''; nextSource.className = 'active'; // update the video source and start playback videoElement.src = nextSource.src; videoElement.play(); })
You can use the track element to add text captions or subtitles to the video. The caption or subtitle track must be in WebVTT (".vtt") format.
track
In the HTML code, set the attributes for each text track element:
kind
label
srclang
default
<video id='video'> <source src='yourVideoURI' type='yourVideoType' ></source> <track label='English' kind='subtitles' srclang='en' src='yourSubtitleURI_en' default></track> <track label='Deutsch' kind='subtitles' srclang='de' src='yourSubtitleURI_de'></track> </video>
Show the selected track and hide the others:
var videoElement = document.getElementById('video'); // show English subtitles videoElement.textTracks[0].mode = 'showing'; videoElement.textTracks[1].mode = 'hidden'; // show Deutsch subtitles videoElement.textTracks[0].mode = 'hidden'; videoElement.textTracks[1].mode = 'showing';
Style the displayed text using CSS. WebVTT supports a limited number of CSS properties for text styling, such as color, opacity, and font. For more information, see Adding captions and subtitles to HTML5 video.
color
opacity
font
::cue { color:#ccc; }
The error property returns an instance of the MediaError interface, which returns a numerical code value representing the error state of the media element.
error
var videoElement = document.getElementById('video'); videoElement.addEventListener('error', function() { /* Video playback failed: show an error message */ switch (videoElement.error.code) { case 1: // 'MEDIA_ERR_ABORTED : 1, Media data download is stopped by the user' break; case 2: // 'MEDIA_ERR_NETWORK : 2, Download stopped due to network error' break; case 3: // 'MEDIA_ERR_DECODE : 3, Media data decoding failure' break; case 4: // 'MEDIA_ERR_SRC_NOT_SUPPORTED : 4, Format not supported' break; } }, false);