API
What you learn​
You're instantiating DIVA Player in your web front-end and relying on DIVA Back Office as the video streaming source.
The goal of this article is to build the simplest front-end to stream a video from the DIVA Back Office and call API endpoints to play and pause the video.
Before starting​
- Ensure the DIVA Back Office instance you rely on is up and running.
- Ask your video engineers team the
<video id>
and the related<settings URL>
.
Instantiation​
Use the BoAdapterWebComponentConfig
object to get the API reference:
const config: BoAdapterWebComponentConfig = {
...
setAPI: (apiRef: DivaAPI) => {
//track the API object
console.log("API Reference:", apiRef);
//get the API reference
divaAPI = apiRef
},
...
};
Optionally, subscribe to the onPlayerState
event to track DIVA Player changes when calling the API endpoints:
const config: BoAdapterWebComponentConfig = {
...
onPlayerState: (state: PlayerState) => {
//track the player state changes when calling API's endpoints
console.log("Player State:", state);
}
};
Note: Enclose API calls within functions. E.g., to call the API endpoint play()
, enclose it within a function like in the following example:
function play() {
divaAPI.play();
}
Working sample code (.tsx)​
The following working code adds to the page two buttons to play and pause the video outside the DIVA Player viewport:
import { BoAdapterWebComponentConfig, DivaWebBoAdapter } from "@deltatre-vxp/diva-sdk/diva-web-bo-adapter";
import { DivaAPI, PlayerState } from "@deltatre-vxp/diva-sdk/diva-web-bo-adapter/types/diva-configuration";
import "@deltatre-vxp/diva-sdk/diva-web-sdk/index.min.css";
const videoId = "<videoId>";
let divaAPI: DivaAPI
const libs = {
mux: "https://cdnjs.cloudflare.com/ajax/libs/mux.js/6.2.0/mux.min.js",
shaka: "https://cdnjs.cloudflare.com/ajax/libs/shaka-player/4.11.2/shaka-player.compiled.js",
hlsJs: "https://cdn.jsdelivr.net/npm/hls.js@1.5.7",
googleIMA: false,
googleDAI: false,
googleCast: false,
threeJs: false
};
const config: BoAdapterWebComponentConfig = {
videoId: videoId,
libs: libs,
setAPI: (apiRef: DivaAPI) => {
//track the API object
console.log("API Reference:", apiRef);
//get the API reference
divaAPI = apiRef
},
onPlayerState: (state: PlayerState) => {
//track the player state changes when calling API's endpoints
console.log("Player State:", state);
}
};
const props = {
settingsUrl: "<settings URL>",
languageCode: "en-US",
languageDictionary: "en-US",
onDivaBoAdapterError: console.error,
config: config
}
function play() {
divaAPI.play();
}
function pause() {
divaAPI.pause();
}
function App() {
return (
<>
<DivaWebBoAdapter
{
...props
}
/>
<button onClick={play} >Play</button>
<button onClick={pause} >Pause</button>
</>
);
}
export default App;