Player API tutorial
The API allows you to manage the player settings using JavaScript and collect data about its use for further analysis.
Player initialization via iFrame
Copy the iFrame code from the VOD/broadcast settings in your account:
- For videos: the All videos section, Settings, Export, Code
- For live streaming: the Live streaming section, Settings, Links for the export
Add the id='gplayer'
parameter to the iFrame and paste it into the site code.
Example of the iFrame code from the Gcore Customer Portal with the id= ‘gplayer’ parameter:
Parameters:
- width - the width of the player,
- height - the height of the player,
- src - content URL (link to VOD or live stream),
- allowfullscreen - if the parameter is added to the iframe, a button for switching to full-screen mode is added to the controls of the player,
- frameborder - border of the player. The default value is “0” (the border shouldn’t be visible),
- id - the player ID. Required for further initialization of the player when calling API methods.
After the iframe add the following code to initialize the player:
The onload event on window is triggered when the entire page, including the iframe, is loaded. Then there is an indication of the player to which the API call will be applied.
Example of the full player initialization code and iframe for HTML page:
Events
Event | Returns | Description |
---|---|---|
ready | Fired when playback is ready | |
play | Fired when player starts to play | |
pause | Fired when player is paused | |
seek | the current time in seconds | Fired when player seeks the video |
resize | the object with the current size | Fired when player is resized |
fullscreen | true/false | Fired when the player is switched to the full screen mode |
timeupdate | data progress object | Fired when the time is updated on player |
volumeupdate | current volume level | Fired when player’s volume is updated |
error | error | Fired when player receives an error |
ended | Fired when player ends the video | |
stop | Fired when player stops |
Calling player methods
You can start calling methods once you receive the ready event:
Available methods:
Method name | Data type | Description |
---|---|---|
getCurrentTime | Number | The current time in seconds |
getDuration | Number | The duration time in seconds |
getVolume | Number | Volume. Value from 0 (sound off, mute) to 100 (maximum volume) |
isDvrEnabled | Boolean | Shows DVR service status: true - DVR is enabled, false - DVR is disabled |
isPlaying | Boolean | Checks if the player is playing: true - the current source is playing, false - playback is off/stopped/not started |
mute | Turns off the sound | |
unmute | Turns on the sound | |
pause | Pauses playback | |
play | Starts playback | |
resize | Object | Modifies the canvas of the player |
seek | Number | Rewinds playback (seconds). For example, player.seek(120) rewinds the video and starts at 2 minutes |
seekPercentage | Number | Rewinds playback (percent). For example, player.seek(50) rewinds the video and starts playing from the middle of the video |
setVolume | Number | Sets the volume for the current video |
stop | Stops playback of the current video |
Method call pattern:
- name - the name of the function,
- params - the parameters of the function,
- callback - the callback function.
Examples: