As of THEOplayer 6.0, THEOplayer can also be used as
a JavaScript module. Instead of loading the
library through a <script>
tag in your HTML, you import
it directly from your JavaScript code.
Using JavaScript modules has several advantages:
<script>
tags and keeping them
in the right order.import
the same module, the module is only loaded once. (On the other hand,
multiple <script>
tags with the same src
attribute will load and execute the script multiple times.)Additionally, since JavaScript modules are only supported in modern web browsers, THEOplayer can use more modern JavaScript syntax in its JavaScript module (such as async/await). This results in smaller files, which load and execute faster on your viewer's devices.
Note If you need to support older browsers (such as older smart TVs), you should stick with a classic
<script>
tag. See the getting started guide.
In this article, we will set up a simple web page using JavaScript modules with the THEOplayer SDK on Web. The outcome of this article is the template below.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>THEOplayer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="/path/to/ui.css" />
</head>
<body>
<div class="theoplayer-container video-js theoplayer-skin vjs-16-9"></div>
<script type="module">
import * as THEOplayer from "/path/to/THEOplayer.esm.js"
let element = document.querySelector(".theoplayer-container")
let player = new THEOplayer.Player(element, {
libraryLocation: "/path/to/your-theoplayer-folder/",
license: "your_license_string",
})
player.source = {
sources: [
{
src: "//cdn.theoplayer.com/video/star_wars_episode_vii-the_force_awakens_official_comic-con_2015_reel_(2015)/index.m3u8",
type: "application/x-mpegurl",
},
],
}
</script>
</body>
</html>
See the prerequisites in our getting started guide.
Now, we're ready to set up THEOplayer on your website. This guide explains how you implement THEOplayer in four steps:
When THEOplayer is used as a web video player, we will need a web page (i.e. HTML file) to embed THEOplayer and the code.
Note that a THEOplayer SDK license can only be used on whitelisted domains. You configure these domains when you create a THEOplayer Web SDK at https://portal.theoplayer.com.
Let's start with the following HTML file:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>THEOplayer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<script type="module"></script>
</body>
</html>
Pay attention to the type="module"
attribute on the <script>
tag. This informs the browser to execute it as a
module, rather than as a classic script. This allows the code inside it to use import
and export
statements.
Next, we’ll include the THEOplayer SDK.
To access the THEOplayer API and default UI, you must first include the THEOplayer library files. The JavaScript library exposes the THEOplayer API and the CSS library contains the default UI.
The following line imports the THEOplayer JavaScript library, and gives you access to the THEOplayer API.
<script type="module">
import * as THEOplayer from "/path/to/THEOplayer.esm.js"
</script>
The path of the import
statement is the URI of the THEOplayer library that you wish to use. This could be something
like: /path/to/THEOplayer.esm.js
, where the URL is a link to your THEOplayer.esm.js
file.
You must swap /path/to/
with your CDN URL.
For example, if your SDK is hosted on https://example.com/vendor/theoplayer/
, then you'd write the following import:
import * as THEOplayer from "https://example.com/vendor/theoplayer/THEOplayer.esm.js"
To use the default THEOplayer UI, you need to reference the THEOplayer CSS library:
<link rel="stylesheet" type="text/css" href="/path/to/ui.css" />
The <link>
tag includes a CSS file on a web page.
Similar to the JavaScript library, you must swap /path/to/
with your CDN URL.
Next, we’ll create a THEOplayer instance and configure a video stream.
To play a video through the THEOplayer Web SDK, you must:
The snippet below implements these 4 steps.
<div class="theoplayer-container video-js theoplayer-skin vjs-16-9"></div>
<script type="module">
import * as THEOplayer from "/path/to/THEOplayer.esm.js"
let element = document.querySelector(".theoplayer-container") // fetch THEOplayer container div
let player = new THEOplayer.Player(element, {
// instantiates video player
libraryLocation: "/path/to/your-theoplayer-folder/", // references folder containing your THEOplayer library files (theoplayer.p.js, THEOplayer.js, ...)
license: "your_license_string", // references your THEOplayer SDK license
})
// HLS
player.source = {
sources: [
{
src: "//cdn.theoplayer.com/video/star_wars_episode_vii-the_force_awakens_official_comic-con_2015_reel_(2015)/index.m3u8", // sets HLS source
type: "application/x-mpegurl", // sets type to HLS
},
],
}
// DASH
// player.source = {
// sources : [{
// src : '//amssamples.streaming.mediaservices.windows.net/634cd01c-6822-4630-8444-8dd6279f94c6/CaminandesLlamaDrama4K.ism/manifest(format=mpd-time-csf)', // sets DASH source
// type : 'application/dash+xml' // sets type to MPEG-DASH
// }]
// };
</script>
Let's break down the above snippet.
This <div>
element creates a video player container.
<div class="theoplayer-container video-js theoplayer-skin vjs-16-9"></div>
theoplayer-container
class is used to fetch the container through JavaScript, as demonstrated by the snippet below.video-js theoplayer-skin vjs-16-9
classes load the default THEOplayer UI. You may omit vjs-16-9
if you don't want a 16:9 container.let element = document.querySelector(".theoplayer-container")
The snippet below creates a THEOplayer instance for a container with a specific Player Configuration.
let player = new THEOplayer.Player(element, {
// instantiates video player
libraryLocation: "/path/to/your-theoplayer-folder/", // references folder containing your THEOplayer library files (theoplayer.p.js, THEOplayer.js, ...)
license: "your_license_string", // references your THEOplayer SDK license
})
Similar to the JavaScript and CSS library, you must swap out '/path/to/your-theoplayer-folder/'
and 'your_license_string'
.
For example, if your SDK is hosted on https://example.com/vendor/theoplayer/
and your license string is ABCD1234
, then you would instead configure the snippet below:
var player = new THEOplayer.Player(element, {
libraryLocation: "https://example.com/vendor/theoplayer/",
license: "ABCD1234",
})
You can read more about configuring the license
at
the License section.
The snippet below configures a HLS video stream for the THEOplayer instance.
player.source = {
sources: [
{
src: "//cdn.theoplayer.com/video/star_wars_episode_vii-the_force_awakens_official_comic-con_2015_reel_(2015)/index.m3u8", // sets HLS source
type: "application/x-mpegurl", // sets type to HLS
},
],
}
To configure a DASH stream instead,
you must set type
to 'application/dash+xml'
instead of 'application/x-mpegurl'
,
as demonstrated by commented out code in one of the earlier snippets.
In the next step we'll put everything together.
We're done! Here's the complete code again, which you can put on your web server:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>THEOplayer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="/path/to/ui.css" />
</head>
<body>
<div class="theoplayer-container video-js theoplayer-skin vjs-16-9"></div>
<script type="module">
import * as THEOplayer from "/path/to/THEOplayer.esm.js"
let element = document.querySelector(".theoplayer-container")
let player = new THEOplayer.Player(element, {
libraryLocation: "/path/to/your-theoplayer-folder/",
license: "your_license_string",
})
player.source = {
sources: [
{
src: "//cdn.theoplayer.com/video/star_wars_episode_vii-the_force_awakens_official_comic-con_2015_reel_(2015)/index.m3u8",
type: "application/x-mpegurl",
},
],
}
</script>
</body>
</html>
Ready to learn more?