Commands and requests JS API
The JavaScript API allows you to get player events, request data and manage it from the outside.
Requests and commands Show events
The api operator is used to manage the player and get data.
var player = new Playerjs({...});
console.log(player.api("id"));
player.api("play");
For some commands you can specify a parameter
player.api("play","URL");
Command |
Parameter |
Action |
Return |
play |
- |
starting playback |
- |
play |
URL, playlist or id: |
starting playback of new source* |
false or true for id: |
pause |
- |
pause playback |
- |
toggle |
- |
toggle playback |
- |
file |
URL, playlist or id:
|
update a source (no play, no preload)* |
- |
preload |
URL or playlist |
update a source (no play, but preload)* |
- |
stop |
- |
stop playback |
- |
mute |
- |
mute sound |
- |
unmute |
- |
unmute sound** |
- |
seek |
time in seconds |
seek (after starting) |
- |
fullscreen |
- |
go to full screen mode** |
- |
exitfullscreen |
- |
exit from full screen mode |
- |
isfullscreen |
- |
- |
true or false |
playing |
- |
- |
true or false |
started |
- |
- |
true or false |
time |
- |
- |
current time of playback (time2 return formatted value) |
duration |
- |
- |
duration in seconds |
buffered |
- |
- |
buffered time in seconds |
muted |
- |
- |
true or false |
volume |
nothing or volume level (0.8) |
the command returns the volume level or sets the value (from 0 to 1). You can use the command volume,+0.1 to increase the volume with custom button.
|
volume level (from 0 to 1) or nothing |
quality |
nothing or the serial number of the quality option (0,1,2...) |
- |
current quality |
qualities |
- |
- |
available qualities |
audiotrack |
nothing or the serial number (0,1,2...) |
launches audiotrack, switching by number works from version 9.7
|
the name of the audio track or index number, if the index parameter is specified |
audiotracks |
- |
- |
available audiotracks |
speed |
nothing or the serial number of the speed option (0,1,2...) |
- |
current speed |
id |
- |
- |
id of the player container |
log |
- |
- |
debug log |
screenshot |
- |
takes a screenshot |
false or base64 |
subtitles |
- |
- |
available subtitles |
subtitle |
[Title]URL or the serial number (-1,0,1,2...) |
launching subtitle from the source or by number (-1 disables subtitles), use subtitle,0/1 for custom button |
name of the current subtitle option or number if you specify the index parameter |
+subtitle |
[Title]URL |
add a new subtitle to ету end of list |
- |
subtitle:size |
subtitle size (14 by default) |
formatting (you can also use color, bg, bgcolor, shadow) |
- |
togglesubs |
|
turns subtitles on/off |
- |
poster |
URL |
displays the poster if the player is paused |
true or false |
share |
- |
display share screen with social and embed buttons if this plugin enabled in the builder |
- |
title |
text or nothing |
displays the title if there is such element enabled player.api("title","text"), or return current value player.api("title") |
true or title value |
playlist |
object [ ], url or played |
updates playlist or return a progress |
true, false or number |
playlist_id |
- |
return id of the played track from playlist |
id |
playlist_title |
- |
return title of the played track from playlist |
title |
playlist_folders |
- |
return the array with data of the playlist root folders |
array |
playlist_length |
- |
return length of the current playlist |
number |
push |
JSON
|
adds new items to the playlist, for example [{"file":"URL","title":"TEXT"}, {"file":"URL","title":"TEXT"}] |
- |
invert |
- |
invert the playlist in reverse order |
- |
autonext |
nothing, 0 or 1 |
autoplay the next file from playlist (1) or not (0) |
0 or 1 with no parameter |
playlistloop |
nothing, 0 or 1 |
loop playlist (1) or not (0) |
0 or 1 with no parameter |
next |
- |
play next file in the playlist |
- |
prev |
- |
play previous file in the playlist |
- |
loop |
nothing, 0 or 1 |
set loop playback |
0 or 1 |
find |
file id |
open track from playlist by id and wait of the starting |
true or false |
cuid |
cuid value |
updates the cuid parameter to remember the time |
- |
showplaylist |
1 or nothing |
show or hide playlist |
- |
moveplaylist |
id |
- |
move playlist outside of the player |
scale |
transformation step or format or nothing |
changes the video scale (floating-point number) or aspect ratio for incorrectly encoded video (width:height, for example 4:3 or 16:9)
|
if no parameter is specified, it returns an array with a scale in width, height and coordinates if the video is dragged
|
ratio |
- |
- |
video aspect ratio |
native |
- |
- |
native controls (false or true) |
points |
object [ ] |
updates the markup |
- |
thumbnails |
link to the thumbnails file |
updates the thumbnails |
- |
visibility |
- |
- |
precent of the player visibility |
hlserror
dasherror |
- |
- |
object error for HLS / DASH |
hls
dash |
- |
- |
object hls.js or dash.js (direct access to the plugin) |
fix
unfix |
- |
manual switching of the player's fixed mode |
- |
adblock |
- |
- |
on or off AdBlock (false или true) |
live |
- |
- |
is a live stream or not (true or false) |
size |
- |
- |
player size (width/height) |
stretch |
nothing, 0 or 1 |
stretch the video to fill all screen |
0 or 1 |
flip |
|
flips the video horizontally |
|
geo |
- |
- |
JSON data of geolocation plugin |
pip |
- |
swaps the primary and secondary streams |
- |
cut |
1, 0, interval or nothing |
controls the interface for creating a segment. The request cut? returns a current value. |
- |
toolbar |
- |
show the toolbar if it is hidden |
- |
destroy |
- |
Prepares the player for removal before cleaning the container |
- |
color1-3 |
color |
Set a color variable. |
|
menu |
- |
open or close the settings window |
|
act |
- |
returns the fact of user interaction with the player |
true or false |
* you can add the option
seek player.api("play","url[seek:10]"); to start with the desired second or [seek:time] to start from the current time.
First launching on mobile with sound works only after the user gestures. If you want to prevent ads after updating the file link, you need to add [skipads] - player.api("play","url[skipads]");
** on all platforms only due to user actions
The API is in a constant development process, if you have something missing - write to us via feedback.
How to know that the player is ready to work
You can catch the init event that the player sends after initialization. Also, from version 9.25, you can use the ready parameter with the function name that the player will call when it is ready.
var player = new Playerjs({id:"player", ready:"PlayerReady", file:"..."});
function PlayerReady(id){
alert("ready");
}
How to get the players events
You can read about listening to player events here.
Custom options
You can transfer your variables to the player with object vars and then request with the same command.
var player = new Playerjs({id:"player", file:"video.mp4", vars:{my1:"a",my2:"b"}});
var vars = player.api("vars");
console.log(vars.my1); // a
In the same way, you can pass your variables in playlist.
How to send commands to the player inside the iframe
There is a special way for this postMessage. To make it work, you need to enable in the settings. Plugins / API / postMessage for iframe. For example, iframe looks like this:
<iframe id="player" src="https://site.com/video.html" type="text/html" width="640" height="360" frameborder="0" allowfullscreen=""></iframe>
To send a command you need to pass an object with the command api
document.getElementById("player").contentWindow.postMessage({"api":"play"}, "*");
If you need to pass a parameter, please add a set option.
document.getElementById("player").contentWindow.postMessage({"api":"volume","set":0.5}, "*");
The player will send a response to requests with a message event with an object {event, answer}
document.getElementById("player").contentWindow.postMessage({"api":"time"}, "*");
window.addEventListener("message", function (event) {
console.log(event.data.event, event.data.answer);
}
The symbol * at the end means that you trust this iframe. If not, then you need to pass URI.