Using the Embedded Video Player API you can control the embedded video player as well as observe events triggered by the player. For example, you may listen to the event when a video is paused or start the video playback using a custom button.
To get started follow the sections below:
Read the embedded video player documentation to learn how to setup the plugin.
To get the embedded video player API instance Listen to xfbml.ready
. If the message type
is video
, the ready
event was fired by the embedded video player.
var my_video_player; FB.Event.subscribe('xfbml.ready', function(msg) { if (msg.type === 'video') { my_video_player = msg.instance; } });
In the code sample below we assume you are using an app ID when initalizing the Facebook SDK for JavaScript. If you don't have an app ID yet, read the docs for creating an app ID.
In the code sample below replace {your-app-id}
by your app ID and data-href
with your video URL. Make sure that you check for the xfbml.ready
event after calling the function FB.init()
.
<html> <head> <title>Your Website Title</title> </head> <body> <!-- Load Facebook SDK for JavaScript --> <script> window.fbAsyncInit = function() { FB.init({ appId : '{your-app-id}', xfbml : true, version : 'v3.2' }); // Get Embedded Video Player API Instance var my_video_player; FB.Event.subscribe('xfbml.ready', function(msg) { if (msg.type === 'video') { my_video_player = msg.instance; } }); }; </script> <div id="fb-root"></div> <script async defer src="https://connect.facebook.net/en_US/sdk.js"></script> <!-- Your embedded video player code --> <div class="fb-video" data-href="https://www.facebook.com/facebook/videos/10153231379946729/" data-width="500" data-allowfullscreen="true"></div> </body> </html>
If you are using multiple players on one page you can identify a video player by adding an id
attribute to the video player tag and checking for its id
in the msg
object:
<div id="my-video-player-id" ... /> <script> FB.Event.subscribe('xfbml.ready', function(msg) { if (msg.type === 'video' && msg.id === 'my-video-player-id') { // True for <div id="my-video-player-id" ... my_video_player = msg.instance; } }); </script>
You can call a set of functions to control your video player or get its current status, e.g. the current playback position.
// Start video playback my_video_player.play(); // Check whether video is muted if (my_video_player.isMuted()) { // Video is muted } // Jump to second 5 of the video my_video_player.seek(5);Player Controls Reference
The function subscribe()
adds a listener function for a specified event, e.g. startedPlaying
.
var myEventHandler = my_video_player.subscribe('startedPlaying', function(e) { // Video started playing ... });
The function subscribe()
returns a token with a release
method that when called, will remove the listener again from the event.
myEventHandler.release('startedPlaying');Event Reference
You can call a set of functions to control your video player or get its current status, e.g. the current playback position.
Function | Description | Arguments (Type) |
---|---|---|
| Plays the video. | |
| Pauses the video. | |
| Seeks to specified position. |
|
| Mute the video. | |
| Unmute the video. | |
|
| |
| Sets the volume to specified number ( |
|
| Returns the video's current volume ( | |
| Returns current video time position in seconds. | |
| Returns the video duration in seconds. | |
| Adds a listener function for the specified event. For details about events, refer to the section Subscribing to Events.
Returns a token with a |
|
Event | Description |
---|---|
| Fired when video starts to play. |
| Fired when video pauses. |
| Fired when video finishes playing. |
| Fired when video starts to buffer. |
| Fired when video recovers from buffering. |
| Fired when an error occurs on the video. |