Recommended videos can be defined when setting a player's video source.
The recommendations will be shown in the recommendation overlay when a video ends.
To automatically show recommendations:
var source1 = {
publicId: 'book',
info: {
title: 'Cloud Book Study',
subtitle: 'A short video with a nice book animation',
description: 'A description of the book movie.'
}
}
HTML:
<video
id="example-player"
controls
muted
autoplay
class="cld-video-player cld-video-player-skin-dark"
data-cld-transformation='{ "width": 500, "crop": "limit" }'>
</video>
<div id="source-data">
<span id="public-id-placeholder"></span><br>
<span id="source-url-placeholder"></span>
</div>
JavaScript:
var cld = cloudinary.Cloudinary.new({ cloud_name: 'miki-cloudinary' });
// Set video sources
var source1 = {
publicId: 'oceans',
info: {
title: 'Oceans',
subtitle: 'A movie about oceans'
description: 'A description of the oceans movie'
}
};
var source2 = {
publicId: 'book',
info: {
title: 'Cloud Book Study',
subtitle: 'A short video with a nice book animation',
description: 'A description of the book movie.'
}
};
// Recommendations can be as simple as an array of other video source objects
source1.recommendations = [source2];
// For async fetching of recommendations (e.g. fetching from database), promises can be used
source2.recommendations = new Promise((resolve, _) => {
console.log('Going to database...');
setTimeout(() => {
console.log('Fetched source from database.', source1)
resolve([source1]);
}, 3000);
});
// Initialize player
var player = cld.videoPlayer('example-player', { autoShowRecommendations: true });
player.source(source1);
function updateSource() {
var divElem = document.querySelector("div#source-data");
publicIdElem = divElem.querySelector("#public-id-placeholder");
sourceUrlElem = divElem.querySelector("#source-url-placeholder");
publicIdElem.innerText = "Public Id: " + player.currentPublicId();
sourceUrlElem.innerText = "Source URL: " + player.currentSourceUrl();
divElem.style.display = 'block';
};
player.on('sourcechanged', updateSource);