Free javascript Hosting


brightcovescript.js

Uploaded on Oct 05 2021 23:44 by adityat

function handlePlaybackEvent_(event) {
var state = event.type;
var player = this;

var message = {
currentTime: player.currentTime(),
duration: player.duration(),
name: player.mediainfo.name,
state: state,
};

// IMPORTANT!
// Replace '*' with the actual origin that should receive the message.
// E.g. 'https://www.mysite.com:80'.
var targetOrigin = '*';

// Post the message to the target window.
// The message is sent as a JSON string for compatibility.
var targetWindow = window.frameElement ? window.frameElement : window.parent;
targetWindow.postMessage(JSON.stringify(message), targetOrigin);

// Handle the message in the target window appropriately.
}

/**
* @public
* Post messages to the window frame for specific media playback events.
* @param {int} numTries Counter of tries to check that players are valid.
* @throws any error from Brightcove's `videojs` object.
*/
function handleBrightcovePlayers(numTries) {
var players = videojs.getPlayers();
var playerIds = Object.keys(players);
if (playerIds.length === 0) {
// players are not ready, try again
// give up after a total of about 7.5 seconds
if (numTries < 10) {
// use exponential backoff to delay
setTimeout(function() {
var numTries = numTries * 2;
handleBrightcovePlayers(numTries);
}, numTries * 500);
}
} else {
// a Brightcove IFRAME can only include one player
var playerId = playerIds[0];
try {
videojs.getPlayer(playerId).ready(function() {
var player = this;
var playerEvents = [
'ended',
'loadeddata',
'pause',
'play',
'stalled',
'timeupdate',
'volumechange',
];
playerEvents.forEach(function(playerEvent) {
player.on(playerEvent, handlePlaybackEvent_);
});
});
} catch (e) {
// use a warning, not an error, so that other scripts can run normally
console.warn(e);
}
}
}

handleBrightcovePlayers(1);

Back to list