Answer the question
In order to leave comments, you need to log in
How to properly upload a video using the YouTube API?
Good afternoon. Faced a set of problems accompanying the YouTube API. First of all, the task is to connect this very API by clicking on a third-party button and start the video. Those. The API cannot be directly connected when the page is loaded. I did everything according to the documentation .
I do the following:
$(document).on('click', "#playVideo", function() {
// Ссылка на ролик зашита в атрибут data-link кнопки
videoLink = $(this).attr("data-link").split("v=")[1] || $(this).attr("data-link").split("embed/")[1];
// Вызываем функцию, подключающую API
getYouTubeAPI(onYouTubeIframeAPIReady);
});
function getYouTubeAPI(callback) {
var doc = document,
script = doc.createElement('script'),
lastScript = $("script")[$("script").length - 1];
script.src = 'https://www.youtube.com/iframe_api';
lastScript.before(script);
// В конце вызываем функцию, которая создает объект плеера и запускает видео
callback();
}
var videoLink,
player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('videoInner', {
videoId: videoLink,
playerVars: {
'autoplay': 1
},
events: {
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question