Welcome! This documentation will guide you to embedding the WebTvPlayer in your website.
The WebTvPlayer provides the same functionalities you would expect from any other video player.
The WebTvPlayer offers different skins which can be selected with a specific configuration option. If you want to customize one of our skins, please take a look at the overrides section, otherwise you can build your own skin from scratch as explained in the custom skins section.
Pick a color theme:
Each color can be customized by providing skin-specific options.
First, you'll need a free API Key from the Halleymedia staff, so contact us at dev@halleymedia.com.
After you got your API Key, initializing the WebTvPlayer user interface in your website is super easy. We provide two different initialization options to suit your needs.
By using our JavaScript SDK, you can inject the WebTvPlayer user interface right into a container element, such as a div
located in your HTML page. The container element can be sized to your liking, since the WebTvPlayer has a responsive layout. However, if the element has no defined height
, the WebTvPlayer will adjust it as necessary.
Add this initialization code right before the closing of the body
tag. The container element can be placed anywhere on your page.
<div id="player-container" style="width: 640px; height: 360px;"></div> <script src="https://video.cirfood.com/v1/embed/webtvplayer.js?apiKey=YOUR_API_KEY"></script> <script> var playerContainer = document.getElementById("player-container"); var videoId = 1; var webTvPlayer = new WebTvPlayer(playerContainer, videoId); </script>
Or, if you're using jQuery, you might prefer a more concise syntax.
<div id="player-container" style="width: 640px; height: 360px;"></div> <script src="https://video.cirfood.com/v1/embed/webtvplayer.js?apiKey=YOUR_API_KEY"></script> <script> var videoId = 1; var webTvPlayer = $("#player-container").webTvPlayer(videoId); </script>
In both these examples, an object of type WebTvPlayer
is returned. Subscribe to the ready
event and then you'll be able interact with its public API that's documented in the properties and methods sections. Here's an example.
webTvPlayer.on('ready', function() { //Here it's safe to use the WebTvPlayer public API, e.g.: this.speed = 1.5; this.volume = 0.8; });
This option is expecially needed when executing JavaScript is not allowed for some reason. Just add this iframe
element anywhere in the page. Set a proper width and height for the iframe
tag.
<iframe src="https://video.cirfood.com/v1/embed/webtvplayer.html?apiKey=YOUR_API_KEY&videoId=videoId" style="width: 640px; height: 360px; border: 0"></iframe>
The WebTvPlayer provides plenty of customization for both initialization options. Jump to the configuration and samples sections for more in-depth customization examples. For a deeper level of customization, take a look at the overrides section.
If you want to protect your video content from anonymous access, read how to properly initialize the WebTvPlayer in the secure playback section.
When initializing the WebTvPlayer you can provide some configuration options to control its final appearance.
If using the JavaScript SDK, provide a configuration object as an additional argument to the initialization method, like in this sample.
<script> var playerOptions = { skin: null, segment: 4.0 }; //Using the global function var webTvPlayer1 = new WebTvPlayer(playerContainer1, videoIdOrJwt, playerOptions); //Or, using jQuery var webTvPlayer2 = $(selector).webTvPlayer(videoIdOrJwt, playerOptions); </script>
Instead, if you're using the iframe, you can pass configuration options as query string parameters.
<iframe src="https://video.cirfood.com/v1/embed/webtvplayer.html?apiKey=YOUR_API_KEY&videoId=videoId&skin=skinName&segment=4.0" style="width: 640px; height: 360px; border: 0"></iframe>
All available options are listed in the following paragraph.
Option name | Description | Default |
---|---|---|
skin:StringOrFunction | Name or constructor function of the UI skin to use. Set to null for a chromeless player. | 'WebTvPlayerDefaultSkin' |
skinOptions:Object | A skin-specific configuration object (see next paragraph for each skin-specific options). | null |
segment:Number | Duration of a segment of a video, expressed in seconds. Has a minimum value of 0.5. When a segment has been completely watched, the progress event is raised. |
5.0 |
from:Number | Display a time-clipped version of the original video starting from this position, expressed in seconds. | null |
to:Number | Display a time-clipped version of the original video ending with this position, expressed in seconds. | null |
metadata:Array | A collection of video-related metadata that must be fetched. The array can contain any combination of these string values: markers , transcription .See the properties and events sections to learn how to get these metadata values once they've been fetched. |
null |
overrides:Object | An object containing your JavaScript overrides to the default behavior and aspect of the WebTvPlayer. See the overrides section to learn how to start your customization! |
{} |
Option name | Description | Default |
---|---|---|
barColor:String | Background color of the control bar. Any color format you would use in CSS is valid. | 'black' |
accentColor:String | A color used for the progress bar and volume. | '#5fa9ff' |
foregroundColor:String | A color used for text and icons. | 'white' |
backgroundColor:String | A color used for the background, which will be revealed in fullscreen, if the display is not the same aspect ratio of the video. | 'black' |
trayColor:String | Background color of the progress bar. | 'black' |
hoverColor:String | A color used as a control background color, when the user hovers it with the mouse cursor. | 'rgba(255, 255, 255, 0.2)' |
If you want to protect content that's only available to paying users, then you need to prevent anonymous access. The WebTvPlayer won't play protected videos if you just provide their ID: instead, you should provide a signed JSON Web Token (JWT) like in the following example:
var jwt = "eyJhbGciOiJIUzI1..."; //JSON Web Token, generated server-side var webTvPlayer = $("#player-container").webTvPlayer(jwt);
You should generate a JWT with a payload containing the following claims:
Here's an example of a valid payload.
{ "iss": "https://example.org", "sub": "1", "exp": 1516239022, "name": "3415" }
You should then sign this payload on the server-side of your application using the HS256
algorithm. The secret key used to sign the payload must be shared with the Halleymedia staff. Contact us via e-mail at dev@halleymedia.com.
The WebTvPlayer will then securely load the video by using Pre-Signed Cookies with the provided expiration, thus granting access for a limited time and exclusively from the IP address of the user. This security mechanism severly discourages any attempt of unauthorized access by malicious users.
If you're using the JavaScript SDK, then you can track a user's progress on the current video. Just subscribe to the progress
event to be notified each time the user has watched a short segment of the video.
webTvPlayer.on('progress', function(index, start, end, percentage) { console.log("The user has watched the segment with index " + index + " starting at " + start + "s and ending at " + end + "s. He watched " + Math.round(percentage) + "% of the video."); //TODO: send an ajax request to your backend in order to save the user progress, or just push an event to Google Tag Manager dataLayer. });
Each segment has a default duration of 5 seconds, so the progress
event will trigger every 5 seconds. You can change the segment duration by providing a segment
property in the configuration options. Here's a comple example.
<div id="player-container" style="width: 640px; height: 360px;"></div> <script> var videoId = 1; var playerOptions = { segment: 1.0 //Each segment will have a duration of 1 second }; var playerContainer = document.getElementById("player-container"); var webTvPlayer = new WebTvPlayer(playerContainer, videoId, playerOptions); webTvPlayer.on('progress', function(index, start, end) { //This will now trigger every second, as long as the video is playing console.log("The user has watched the segment with index " + index + " starting at " + start + "s and ending at " + end + "s."); });
If you're just interested in tracking a percentage value, you could subscribe the percentage
event instead which will fire only for interger values.
webTvPlayer.on('percentage', function(percentage) { console.log("The user has watched " + percentage + "% of the video."); //percentage being an integer value //TODO: send an ajax request to your backend in order to save the user progress, or just push an event to Google Tag Manager dataLayer. });
If you wish to use a purely unobtrusive tracking solution, you can rely on Google Tag Manager instead of subscribing the progress
or percentage
events in the page.
Create a new Custom JavaScript Variable and write code which will do the following:
webtvplayer.create
DOM Event in order to be notified of newly created instances, or iterate through the WebTvPlayer.instances
property;progress
, percentage
or any of the other events of the WebTvPlayer;window.dataLayer
;We prepared a ready-made recipe you can import into your Google Tag Manager Container. All pushed events will be relayed to Google Analytics so just be sure to amend the Google Analytics Settings Variable with your Account ID.
Download the recipe Download just the codeWhen a WebTvPlayer is initialized with the JavaScript API, an object of type WebTvPlayer
is returned. Here's a list of its public properties.
These properties can be used after the ready
event has occurred.
Property name | Description | Accessors |
---|---|---|
id:String | Returns the currently selected video id or JWT. | Read only |
options:Object | Returns the flattened configuration options. | Read only |
container:HTMLElement | Returns a reference to the container element of the current WebTvPlayer. | Read only |
status:String | Returns the current status of the player. Note: Possible values are: ready , loading , buffering , playing , paused , stopped or error . |
Read only |
segments:Number | Returns the number of total segments in this video. Each one has a duration of segment seconds, except the last one which might be shorter. |
Read only |
qualities:Array | Returns the available quality levels for this video. Each object has a label, bitrate, width and height properties. | Read only |
quality:Number | Returns or sets the index of the current quality level. Note: use the index of the array returned by the qualities property. The index 0 is always the 'Auto' quality. |
Read, Write |
resolution:Object | Returns the currently selected video resolution. The object has a width and height property. Note: you could also use the quality and qualities properties for more details about the currently selected quality level. |
Read only |
metadata:Object | Returns the currently loaded metadata. Only the metadata set via the metadata configuration option will be fetched. |
Read only |
speed:Number | Returns or sets the current playback speed. Note: set a value in the range 0.5 (inclusive) to 4.0 (inclusive) with increments of 0.25. |
Read, Write |
volume:Number | Returns or sets the current volume. Note: set a value in the range from 0.0 (mute) to 1.0 (maximum volume). |
Read, Write |
mute:Boolean | Returns or sets whether the video is muted or not. | Read, Write |
fullscreen:Boolean | Returns or sets whether the video is displayed in fullscreen or not. | Read, Write |
These properties can be used after the load
event has occurred.
Property name | Description | Accessors |
---|---|---|
duration:Number | Returns the total duration of the video, expressed in seconds. | Read only |
position:Number | Returns or sets the actual position in the video, expressed in seconds. | Read, Write |
These areproperties that are available right away on the WebTvPlayer
constructor function.
Property name | Description | Accessors |
---|---|---|
instances:Array | A list of the currently active instances of WebTvPlayer. | Read only |
version:String | Version of WebTvPlayer. | Read only |
When a WebTvPlayer is initialized with the JavaScript API, an object of type WebTvPlayer
is returned. Here's a list of its public methods.
These methods can be used after the ready
event has occurred.
Method name | Description |
---|---|
load(videoIdOrJwt:String[, from:Number[, to:Number]]):void | Unloads the currently loaded video (if any) and starts loading a new one with the provided video ID or JWT. The from and to parameters are optional. You should provide them only if you want to display a time-clipped version of the original video. |
on(eventName:String, callback:function):void | Subscribes to an event by providing the event name and a callback See the events section for details. |
off(eventName:String[, callback:function]):void | Removes a previously subscribed callback to an event by providing the event name and the callback itself. Note: The callback argument is optional. If it's not provided, all subscribed callbacks are removed for that event name. |
update():void | Forces the update of the UI. You might need to call this method after the container size is changed. |
destroy():void | Removes all resources created by the WebTvPlayer, including HTML elements from the container. |
These methods can be used after the load
event has occurred.
Method name | Description |
---|---|
play():void | Plays the currently loaded video. |
pause():void | Pauses the currently loaded video. |
stop():void | Stops the currently loaded video, resetting its position to zero. |
The WebTvPlayer
object can raise a few events. Subscribe them by name, using the on
method.
webTvPlayer.on('eventName', function() { /* your code here */ });
Conversely, you can stop being notified by invoking the off
method. The callback argument is optional: if it's not provided, all subscribed callbacks are removed for that event.
webTvPlayer.off('eventName');
When a callback is invoked, the this
keyword references the current instance of WebTvPlayer
.
Event name | Description | Callback arguments |
---|---|---|
ready | The WebTvPlayer intialization has completed and it's ready to load a video. | None |
loading | Loading of a video just started. Methods play(), pause() and other video-related methods and properties are not available during this phase. | None |
load | A video has been loaded, its duration has been determined and it's going to be played. After this event has occurred, it's safe to use the video-related metods and properties. | None |
error | The load or playback of the current video raised an error. | String: reason for the error |
buffer | The buffer has changed. | Boolean: whether the buffer is empty or not. If true, the buffer is empty and the player was forced to stop playing the video and wait for more data to be available. If false, the buffer is not empty and the video is playing just fine. Number: the buffered percentage of the total duration. |
play | The user has requested the playback of the video either from the start or from an arbitrary position. | None |
pause | The user has paused the video. | None |
stop | The user has stopped the video. | None |
end | The video stopped because it reached the end. | None |
mute | The user has muted or unmuted the video. | Boolean: whether the user has muted the video (true) or unmuted it (false). |
volume | The user has changed the volume. | Number: The new volume level, a value between 0.0 (mute) and 1.0 (max volume). |
qualities | The quality levels for this video have been determined. | Array: The collection of quality level. Each object has a label, bitrate, width and height properties. |
quality | The user has changed the quality level. | Number: The index of the selected quality level. |
speed | The user has changed the playback speed. | Number: The selected playback speed. |
progress | The user has watched a segment of the video. | Number: the zero-based index of the segment watched; Number: the start offset of the segment, expressed in seconds; Number: the end offset of the segment, expressed in seconds; Number: the percentage watched so far. |
percentage | The user has watched a certain percentage of the video. Guaranteed to report exactly once each 1% increment. | Number: The watched percentage, always an integer value from 1 to 100. |
position | Position of the video has been updated. | Number: The current position. |
fullscreen | The user has entered or exited fullscreen mode. | Boolean: whether the user has entered fullscreen mode (true) or exited it (false). |
metadata | A piece of video-related metadata has been fetched. | String: name of the metadata. See the metadata configuration option to see which ones are available.Object: the fetched metadata object. |
metadata.name | A specific metadata object has been fetched. Replace the placeholder name with one of the names supported by the metadata configuration option. |
Object: the fetched metadata object. |
resize | The WebTvPlayer container has changed size. | Object: the new size object, containing width and height properties. |
destroy | The WebTvPlayer instance is going to be destroyed: the video will stop playing and the container element is going to be emptied. | None |
Event name | Description | Callback arguments |
---|---|---|
WebTvPlayer.Create | A new instance of WebTvPlayer has been created. | Object: the event, containing a detail property which will give you the newly created instance of WebTvPlayer. |
When you want to make small changes to the behavior or the appearance of the WebTvPlayer beyond what's allowed by configuration options, you provide overrides. Basically, an override is a javascript function which will replace or add your custom logic to the original function. Suppose you want to change just the play button icon of the default skin: you override the getSkinResource function to do so.
var playerContainer = document.getElementById('player-container'); var videoId = 1; var playerOptions = { overrides: { getSkinResource: function(originalFunction, resourceName) { switch(resourceName) { //We want to replace just the play button, so let's return our own url in this case case "play": return "https://example.com/images/my-own-play-icon.svg"; //In all other cases, let's invoke the original function to return its result untouched default: return originalFunction(resourceName); } } }; var webTvPlayer = new WebTvPlayer(playerContainer, videoId, playerOptions);
Of course, if you wanted to perform more important changes, you might as well code your own custom skin.
The WebTvPlayer keeps its logic separated from the UI, so you can provide a completely custom skin, if you want. Creating a custom skin is an advanced task so, before actually embarking on this journey, think if the desired result might be achieved simply by providing overrides or by defining CSS rules.
If you're determined in creating your own skin, then use the code of the WebTvPlayerDefaultSkin as a reference.
At minimum, you'll have to create an object constructor exposing a render
function, which the WebTvPlayer will invoke. We suggest using the Object.defineProperty to make it read-only.
function MySkin() { function render() { var playerInstance = this; //The this keyword references the WebTvPlayer instance //You can use the WebTvPlayer instance to access its public API //Here we get the containing element and change its style... playerInstance.container.style.border = "2px solid red"; //...then we append a play button to it var button = document.createElement('button'); button.setAttribute('type', 'button'); button.innerText = 'Play'; button.addEventListener('click', function() { playerInstance.play(); }, false); playerInstance.container.appendChild(button); } //Let's expose the render function Object.defineProperty(this, 'render', { get: function() { return render; }, configurable: false }); }
Inside your skin constructor you can, of course, organize the code as your prefer. In this example, the play button was added inside the render function but you're absolutely free (and encouraged) to create smaller methods to improve code readability.
When you're done, pass a reference to the constructor to the skin property of the configuration object.
var playerContainer = document.getElementById('player-container'); var videoId = 1; var playerOptions = { skin: MySkin //pass a reference to the constructor, not to a constructed object }; var webTvPlayer = new WebTvPlayer(playerContainer, videoId, playerOptions);
Here's a brief gallery of code samples.
JavaScript SDK | iframe tag | Result |
---|
TODO
TODO