When an embedded video is paused, YouTube will display a menu with "More Videos". I tried "$('.ytp-expand-pause-overlay .ytp-pause-overlay').css('display','none');" but it doesn't work. May I know how to disable the "More Videos" feature?
Here is the source code:
<iframe id="videoplayer"
width="640" height="360"
src="https://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&color=white&showinfo=0&ecver=2&rel=0&modestbranding=1&origin="
frameborder="0"
style="border: solid 4px #37474F">
</iframe>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript">
var tag = document.createElement('script');
tag.id = 'videoplayer';
tag.src = 'https://www.youtube.com/iframe_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('videoplayer',
{
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
document.getElementById('videoplayer').style.borderColor = '#FF6D00';
$('.ytp-expand-pause-overlay .ytp-pause-overlay').css('display','none');
}
function onPlayerStateChange(event) {
document.getElementById('videoplayer').style.borderColor = '#37474F';
}
</script>
Could you use playerVars for this? Documentation
<script type="text/javascript">
const tag = document.createElement("script");
tag.id = "videoplayer";
tag.src = "https://www.youtube.com/iframe_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
let player;
function onYouTubeIframeAPIReady() {
player = new YT.Player("videoplayer", {
height: "360",
width: "640",
playerVars: {
autoplay: 1,
controls: 0,
disablekb: 1,
fs: 0,
iv_load_policy: 3,
modestbranding: 1,
rel: 0, // <--- I think this is the option you're looking for
showinfo: 0,
ecver: 2,
origin: "",
},
events: {
onReady: onPlayerReady(),
onStateChange: onPlayerStateChange(),
},
});
}
function onPlayerReady(event) {
document.getElementById("videoplayer").style.borderColor = "#FF6D00";
}
function onPlayerStateChange(event) {
document.getElementById("videoplayer").style.borderColor = "#37474F";
}
</script>
Related
Recently, YouTube Player API for iframe Embeds doesn't work seekTo method on Microsoft Edge (Windows 10).
Example, simple HTML5 player as follows:
<!DOCTYPE html>
<html>
<body>
<div class="video-container">
<div id="player">
</div>
</div>
<script type="text/javascript">
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
var isPlayerReady = false;
function onYouTubeIframeAPIReady() {
isPlayerReady = true;
window.external.notify("ready");
}
function startVideo(videoId) {
if (!isPlayerReady) return;
player = new YT.Player('player', {
videoId: videoId,
playerVars: {
'autoplay': 1,
'autohide': 2,
'fs': 0,
'cc_load_policy': 0,
'enablejsapi': 1,
'iv_load_policy': 3,
'loop': 0,
'modestbranding': 1,
'rel': 0,
'showinfo': 1,
},
events: {
}
});
}
function onSeek(sec) {
if (player == null) return;
player.seekTo(sec, true);
}
</script>
<a onclick="startVideo('XefNxZ0pAR0');">Start</a>
<br />
<a onclick="onSeek(60);">Seek</a>
</body>
</html>
If click the 'seek' link, error occurred "Object doesn't support property or method 'seekTo'".
And after start Video, error always occurred 'invalid argument' in www-embed-player.js (565,52)
What's happened? Before few weeks, this code could run on Edge.
I'm trying the following code on iOS in a UIWebView to embed a YouTube video:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
body, html {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<iframe id="player" type="text/html" width="100%" height="100%" src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1" frameborder="0"></iframe>
<script>
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
</script>
</body>
</html>
I'm using the following Swift code to accomplish it:
let html = "<!DOCTYPE html><html><head><style type=\"text/css\"> body { margin: 0; padding: 0; } body, html { height: 100%; width: 100%; } </style> </head> <body> <iframe id=\"player\" type=\"text/html\" width=\"100%\" height=\"100%\" src=\"http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1\" frameborder=\"0\"></iframe> <script> var tag = document.createElement('script'); tag.src = \"https://www.youtube.com/iframe_api\"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubeIframeAPIReady() { player = new YT.Player('player', { events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body></html>"
let width = UIScreen.mainScreen().bounds.width
let height = UIScreen.mainScreen().bounds.height
youTubeWebView = UIWebView(frame: CGRectMake(20, 120, width - 40, height-500))
youTubeWebView.mediaPlaybackRequiresUserAction = false
view.addSubview(youTubeWebView)
youTubeWebView.loadHTMLString(html, baseURL: NSBundle.mainBundle().resourceURL)
But though the video loads, it doesn't actually autoplay like it's supposed to.
Try to put ?rel=0&autoplay=1
<iframe id="player" type="text/html" width="100%" height="100%" src="http://www.youtube.com/embed/M7lc1UVf-VE?rel=0&autoplay=1" frameborder="0"></iframe>
Hopes that fix your problem.
BTW: can't be done in IOS/Mobile
"Warning: To prevent unsolicited downloads over cellular networks at the user’s expense, embedded media cannot be played automatically in Safari on iOS — the user always initiates playback."
https://developers.google.com/youtube/iframe_api_reference#Mobile_considerations
I am using the youtube video api and ahve 2 seperate lightboxes that open the same video. One for mobile and one for desktop.
The problem I am having is that once I place seperate code for the mobile version the desktop version stops working. What I mean is that the lightbox still opens but the video does not dsiplay.
Desktop (video-home-popup) and mobile (video-homepopup-mobile) code:
<div id="video-home-popup" style="display:none; padding:0px;">
<div id="player"></div>
<script type="text/javascript">
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
playerVars: {
modestbranding: true,
theme: 'light',
rel: 0,
wmode: "opaque",
autoplay: '0'
},
height: '480',
width: '640',
videoId: '4IXAxJ8oPFg',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
/// event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
_gaq.push(['_trackEvent', 'Videos', 'Play',
player.getVideoUrl()]);
}
if (event.data == YT.PlayerState.PAUSED) {
_gaq.push(['_trackEvent', 'Videos', 'Paused',
player.getVideoUrl()]);
}
if (event.data == YT.PlayerState.ENDED) {
_gaq.push(['_trackEvent', 'Videos', 'Watch to End',
player.getVideoUrl()]);
}
}
// ]]>
</script>
</div>
<div id="video-home-popup-mobile" style="display:none; padding:0px;">
<div id="player1"></div>
<script type="text/javascript">
var player1;
function onYouTubePlayerAPIReady() {
player1 = new YT.Player('player1', {
player1Vars: {
modestbranding: true,
theme: 'light',
rel: 0,
wmode: "opaque",
autoplay: '0'
},
height: 'auto',
width: 'auto',
videoId: '4IXAxJ8oPFg',
});
}
// ]]>
</script>
</div>
Don't use two function : onYouTubePlayerAPIReady()
Live demo
<div id="player"></div>
<div id="player1"></div>
And the JS
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
player1 = new YT.Player('player1', {
player1Vars: {
modestbranding: true,
theme: 'light',
rel: 0,
wmode: "opaque",
autoplay: '0'
},
height: 'auto',
width: 'auto',
videoId: '4IXAxJ8oPFg',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
} else {
}
}
function stopVideo() {
player.stopVideo();
}
i have the following code
<script>
window.onload = function(){
var a = document.getElementsByTagName("a");
for(var i=0; i<a.length; i++){
if(!/#ytplayer/.test(a[i].href)) continue;
var link = a[i].innerHTML.match(/\/vi\/([^\/]+)/);
if(link) (function(vidId){
a[i].onclick = function(){
player.loadVideoById(vidId);
}
})(link[1]);
}
}
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '480',
width: '853',
videoId: '963puDdR0bY',
wmode: 'transparent',
rel: '0',
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) {
setTimeout(stopVideo, 60000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
<script>
$(document).ready(function() {
$.ajax({
type: "POST",
url: "/publicPortal/restservices/tracking/click",
data: { docId: "enPubIntentionalInvesting", docType: "tvSpots" },
contentType: "text/plain"
});
});
</script>
and im calling the images here
<div id="player" style="padding-left:40px; z-index:-1;"></div>
</div>
<div id="nav" style="height:200px; padding-top:20px; padding-left:40px !important;">
<a href="#ytplayer" >
<img src="image.png" id="vid1" class="vidThumb" rel="http://www.youtube.com/watch?v=963puDdR0bY" />
<img style="display:none;" src="http://img.youtube.com/vi/963puDdR0bY/default.jpg" /></a>
<a href="#ytplayer" >
<img src="image.png" id="vid1" class="vidThumb" rel="http://www.youtube.com/watch?v=963puDdR0bY" />
<img style="display:none;" src="http://img.youtube.com/vi/963puDdR0bY/default.jpg" /></a>
<a href="#ytplayer" >
<img src="image.png" id="vid1" class="vidThumb" rel="http://www.youtube.com/watch?v=963puDdR0bY" />
<img style="display:none;" src="http://img.youtube.com/vi/963puDdR0bY/default.jpg" /></a>
</div>
you may notice i have two images there, but it seems the code needs to pull from you tube, but i wanted a custom image.
My issue is in ie the menu area goes UNDER the youtube player, ive looked around for fixes, but most are geared towards separate iframes.
ive tried adding wmode into the api call but its doesnt seem to be working! any help would be great!
so i figured it out!
you need to add playerVars to the function, like so;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '480',
width: '853',
videoId: '5hewAwxIy9k',
playerVars: {
controls: 1,
showinfo: 0 ,
modestbranding: 0,
rel: 0,
wmode: "opaque"
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
I have a jCarousel (jquery) running for several youtube videos. The carousel works great, and even pauses when you hover on a slide- but it will resume if you move your cursor off the slide, even if the video is playing.
Videos are embedded using the YouTube Player API.
Plugin: http://sorgalla.com/jcarousel/
This is the Carousel code I'm using:
<script type="text/javascript">
function mycarousel_initCallback(carousel, item, idx, state){
// Disable autoscrolling if the user clicks the prev or next button.
carousel.buttonNext.bind('click', function() {
carousel.startAuto(0);
});
carousel.buttonPrev.bind('click', function() {
carousel.startAuto(0);
});
// Pause autoscrolling if the user moves with the cursor over the clip.
$('#mycarousel').hover(function() {
carousel.stopAuto();
}, function() {
carousel.startAuto();
});
};
$(function() {
$('#mycarousel').jcarousel({
auto: 2,
wrap: 'both',
initCallback: mycarousel_initCallback
});
});
</script>
The YouTube script:
<script>
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var done = false;
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '505', // 535
width: '900',
videoId: 'DxZve0UV6UM',
playerVars: {
'autoplay': 0,
'controls': 0,
'modestbranding': 1,
'wmode': 'opaque'
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(evt) {
player.setPlaybackQuality('hd720');
}
function onPlayerStateChange(evt) {
if (evt.data == YT.PlayerState.PLAYING && !done) {
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
And the HTML...
<ul id="mycarousel" class="jcarousel-skin-tango">
<li>
<div id="player"></div>
</li>
<li>
<div id="player"></div>
</li>
</ul>
Any suggestions?
Simply call carousel.startAuto() and carousel.stopAuto() when the player triggers an event:
function onPlayerStateChange(evt) {
if(evt.data == YT.PlayerState.PLAYING) {
global_carousel.stopAuto(); // Stop the carousel, if video is playing
} else {
global_carousel.startAuto(); // Otherwise, start it
}
}
Edit:
Make the variable carousel global inside mycarousel_initCallback:
var global_carousel;
function mycarousel_initCallback(carousel, item, idx, state) {
global_carousel = carousel;
//Other code
Then use that instead in the code before (as I've edited).