Player methods
You can manage Cloud Video Player using the following JavaScript SDK methods:
setSource
Switches content.
Example of simple content switching:
player.setSource('https://runtime.video.cloud.yandex.net/player/...');
Where https://runtime.video.cloud.yandex.net/player/...
is a link to playable content, e.g., https://runtime.video.cloud.yandex.net/player/video/vplvmyqsxi7dlwndvb4y
. For more information, see Getting an embed code or link to a video and Getting an embed code or link to a broadcast.
Example of content switching with a specified starting position and disabled autoplay:
player.setSource({
source: 'https://runtime.video.cloud.yandex.net/player/...',
startPosition: 10,
autoplay: false
});
This example uses an object of the following format as the parameter:
{
/** @type {string} link to playable content */
source,
/**
* @type {boolean} (optional) autoplay when switching to content.
* By default, preserves the playback state used at time the method is called.
* Autoplay may not work. See https://developer.chrome.com/blog/autoplay/
* autoplay
*/
autoplay,
/** @type {number} (optional, the default value is 0) starting position in seconds */
startPosition,
}
The method returns a promise which:
- Gets the
fulfilled
status if switching is successful. - Gets the
rejected
status if switching fails. For example, if the content with a specifiedid
was not found.
getState
Returns the player state as an object in the format described in Player state.
Usage example:
var state = player.getState();
play
Starts playback.
The method returns a promise which:
- Gets the
fulfilled
status if playback starts. - Gets the
rejected
status if playback fails .
pause
Pauses playback.
seek
Rewinds or fast forwards a video to a specific position, which is provided as the parameter.
Example of jumping to the 10th second:
player.seek(10);
setMuted
Turns sound on/off.
The boolean
type value is provided as the parameter.
Example of turning sound on:
player.setMuted(false);
setVolume
Sets video volume level in the range from 0
(muted) to 1
(maximum volume).
Example:
player.setVolume(0.7);
on/once
Allows you to subscribe to player events.
If once
is called, the subscription works only for the first triggering of the event; if on
is called, for all triggerings.
These methods have the on(eventName, handler)
and once(eventName, handler)
signatures, respectively. The first parameter communicates the event name, the second communicates the handler.
The handler receives an object with the appropriate field from the player state.
Example of subscribing to all triggerings of the playback StatusChange event:
player.on('StatusChange', ({ status }) => {
console.log(status);
});
off
Allows you to unsubscribe from the player events you subscribed to using the on
or once
methods.
The method has the off(eventName, handler)
signature. The first parameter communicates the event name, the second communicates the handler you had used for subscription.
Example of unsubscribing from a handler function named handler
for the playback StatusChange event:
player.off('StatusChange', handler);
destroy
Destroys the player and frees up the resources.
Returns a promise, which enters the fulfilled
state as soon as the operation is completed.
Usage example:
player.destroy();
See also
- Interface: PlayerSdkApi API reference