Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

how to display YouTube video to all users on the site?

$
0
0

Website on asp.net core mvc, signalR. The site has a textBox where the user enters a link to a YouTube video, and a button, after clicking on which the video appears and plays.The problem is that the video is displayed normally only for the user who entered the link to this video. The rest show an error.If you enter videoId into the code itself, then everything is displayed normally on all connected devices. It turns out that the problem is in videoId...please help me, I will be very grateful!

//videoSrc - a button that, when clicked, starts a video.document.getElementById("videoSrc").addEventListener("click", function () {//userUrl - textBox with url    var userVideoUrl = document.getElementById("userUrl").value;    userVideoId = getVideoId(userVideoUrl);    if (userVideoId == "") {        alert('Please, enter correct url!');    } else {//SendVideoData just calls "ReceiveVideoData" method.        connection.invoke("SendVideoData").catch(function (err) {            return console.error(err.toString());        });    }});connection.on("ReceiveVideoData", function () {    GetVideo();});
//function to "get" videoId from urlfunction getVideoId(url) {    var videoId = "";    if (url.indexOf("youtube.com") !== -1) {        videoId = url.split("v=")[1];    } else if (url.indexOf("youtu.be") !== -1) {        videoId = url.split(".be/")[1];    }    return videoId;}    // 3. This function creates an <iframe> (and YouTube player)    //    after the API code downloads.function GetVideo() {    if (typeof (YT) == 'undefined' || typeof (YT.Player) == 'undefined') {        var tag = document.createElement('script');        tag.src = "https://www.youtube.com/iframe_api";        var firstScriptTag = document.getElementsByTagName('script')[0];        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);    }}function onYouTubeIframeAPIReady() {        var player = new YT.Player('player', {            height: '360',            width: '640',            videoId: userVideoId,            events: {'onReady': onPlayerReady,'onStateChange': onPlayerStateChange,            }        });    }    // 4. The API will call this function when the video player is ready.    function onPlayerReady(event) {        event.target.playVideo();    }    // 5. The API calls this function when the player's state changes.    //    The function indicates that when playing a video (state=1),    //    the player should play for six seconds and then stop.    var done = false;    function onPlayerStateChange(event) {        if (event.data == YT.PlayerState.PLAYING && !done) {            done = true;        }    }    function stopVideo() {        player.stopVideo();    }

Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>