Youtube Script API - player showing over the top of menu items - youtube-api

I am trying to embed a youtube video of mine onto my website, I am using the script API so I can have it autoplay and automuted.
The only problem im having is that the video is located just beneath my navigation menu and when a submenu drops out it is show beneath the player.
I originally had the player embedded with the iFrame script and used wmode=opaque to stop this problem which worked perfectly.
It doesnt seem to work with the script API embedding method.
Is it possible to stop this happeneing or mute using the iFrame method?
heres my YouTube script:
<script src="http://www.google.com/jsapi"></script>
<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.1/swfobject.js"></script>
<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div>
<script type="text/javascript">
google.load("swfobject", "2.1");
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
ytplayer.playVideo();
ytplayer.mute();
}
var params = { allowScriptAccess: "always" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/Du_GXK-lh4M?enablejsapi=1&playerapiid=ytplayer&allowFullScreen=true&version=3&rel=0&wmode=opaque",
"ytapiplayer", "440", "280", "8", null, null, params, atts);
</script>
and the video embedding can be seen on my site here: http://create.tuscorlloyds.com/
Hoping ive posted this in the right place.
Any help would be amazing!
Many Thanks
Nick # Tuscor Lloyds

I fixed this by adding 'wmode: "transparent"' after the 'allowScriptAccess: "always"' text.
Finished code:
<script src="http://www.google.com/jsapi"></script>
<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.1/swfobject.js"></script>
<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div>
<script type="text/javascript">
google.load("swfobject", "2.1");
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
ytplayer.playVideo();
ytplayer.mute();
}
var params = { allowScriptAccess: "always", wmode: "transparent" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/Du_GXK-lh4M?enablejsapi=1& playerapiid=ytplayer&allowFullScreen=true&version=3&rel=0",
"ytapiplayer", "440", "280", "8", null, null, params, atts);
</script>

Related

How to stop youtube video programmatically in jQuery

I'm developing a simple web application with jQueryMobile, I can swipe between different pages with Youtube videos,
the problem I have is quite simple, I want to stop a youtube video whine I swipe go to the next page,
otherwise the user will have two youtube videos playing in the same time.
here is the Code and the demo :
JSFiddle DEMO + FULL CODE
Here is my awesome full code :
var i =1;
$("body").on("swipeleft",function(){
if (i<2) {
i++;
$.mobile.changePage('#p'+i, {
transition: "slide"
});
}
});
$("body").on("swiperight",function(){
if (i>1) {
i--;
$.mobile.changePage('#p'+i, {
transition: "slide",
reverse: true
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div data-role="page" id="p1">
<iframe width="560" height="315" src="https://www.youtube.com/embed/X9_n8jakvWU" frameborder="0" allowfullscreen></iframe>
</div>
<div data-role="page" id="p2">
<iframe width="560" height="315" src="https://www.youtube.com/embed/UecPqm2Dbes" frameborder="0" allowfullscreen></iframe>
</div>
Given the older version of jQuery you are using, I assume you are using jQuery Mobile 1.3 not 1.4.
You could use the pagebeforehide event to get the iframe of the page you are leaving and then quickly set the src of the iframe to nothing and then back to the video url. This will stop the video from playing.
var TheFrame;
$(document).on( "pagebeforehide", function( event, data ) {
TheFrame = $(event.target).find("iframe");
setTimeout(StopVideo, 500);
});
function StopVideo(){
var vid = TheFrame.prop("src");
TheFrame.prop("src", "");
TheFrame.prop("src", vid);
}
The setTimeout allows the slide transition to complete before messing with the iframe src.
Working DEMO
If you are using jQM 1.4, you can use the pagecontainer widget events instead:
var TheFrame;
$(document).on( "pagecontainerbeforehide", function( event, ui ) {
TheFrame = ui.prevPage.find("iframe");
setTimeout(StopVideo, 500);
});
function StopVideo(){
var vid = TheFrame.prop("src");
TheFrame.prop("src", "");
TheFrame.prop("src", vid);
}

Unable to play youtube video in window 8.1 winjs library

I am trying to play youtube video in my window 8.1 app, with the following code.
var videoPlayer = document.getElementById("videoPlayer");
var content = '<iframe width="480" height="270" src="http://www.youtube.com/embed/XGSy3_Czz8k?rel=0" frameborder="0" allowfullscreen></iframe>';
WinJS.Utilities.setInnerHTMLUnsafe(videoPlayer, content);
where the videoplayer is name of div where video player appears, but I am not able to play the video.
I have also tried using sample code from
https://developers.google.com/youtube/iframe_api_reference#Examples
but it didn't work as well. So please can anyone help me with this.
Thanks in advance
I managed to get this scenario to work for me, but I had to jump through some hoops to make it work.
First, in order to load the external script they need to communicate with the iframe, I had to sandbox the player in its own outer iframe that could run in the web context:
<div class="playerContainer">
<iframe class="playerFrame" src="ms-appx-web:///pages/video/iframe.html" style="visibility: hidden" scrolling="no"></iframe>
</div>
Then I had to edit my APPXMANIFEST to include the YouTube domain as a ContentURI rule:
https://*.youtube.com
My iframe.html was pretty simple:
<!DOCTYPE html>
<html>
<head>
<title>Video Player Iframe</title>
<link href="/pages/video/iframe.css" rel="stylesheet" />
<script src="/pages/video/iframe.js"></script>
</head>
<body>
<iframe id="player"
src="https://www.youtube.com/embed/?enablejsapi=1"
frameborder="0"
style="visibility: hidden"></iframe>
</body>
</html>
Then inside of iframe.js I borrowed heavily from https://developers.google.com/youtube/iframe_api_reference#Examples
(function () {
"use strict";
window.addEventListener('message', function (e) {
if (e.data.op === 'load') {
loadVideo(e.data.args)
} else if (e.data.op === 'resize') {
resize(e.data.args);
} else if (e.data.op === 'destroy') {
destroy();
}
});
var player;
var videoId;
function loadVideo(args) {
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 playerEl = document.getElementById('player');
playerEl.height = window.innerHeight;
playerEl.width = window.innerWidth;
videoId = args.videoId;
window.onYouTubeIframeAPIReady = function onYouTubeIframeAPIReady() {
playerEl.style.visibility = 'visible';
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady
}
});
}
}
function resize(args) {
player && player.setSize(window.innerWidth, window.innerHeight);
}
function destroy() {
player && player.destroy();
player = null;
}
function onPlayerReady(event) {
event.target.loadVideoById(videoId);
}
})();
As you can see from the above, I am using iframe's postMessage to tell the wrapping iframe to load the videoId that I want:
iframe.addEventListener("load", function () {
iframe.style.visibility = 'visible';
if (iframe.contentWindow) {
iframe.contentWindow.postMessage({ op: "load", args: { videoId: video.id} }, "*");
}
});
Hope that helps some. The YouTube IFrame API is pretty finicky and didn't work right for me when loaded any other way.

Why is the video not loading?How should I load and play a video on a chromeless player?

<html>
<body>
<div id="ytplayer_div1">You need Flash player 8 or higher to view this content.</div>
<script type='text/javascript' src='swfobject.js'> </script>
<script type='text/javascript'>
swfobject.embedSWF
(
'http://www.youtube.com/apiplayer?enablejsapi=1',
'ytplayer_div1',
'425',
'344',
'8',
null,
null,
{
allowScriptAccess: 'always',
allowFullScreen: 'true'
},
{
id: 'ytplayer_object'
}
);
var ytplayer = document.getElementById("ytplayer_div1");
ytplayer.loadVideoById("bHQqvYy5KYo", 5, "large");
</script>
</body>
</html>
From YouTube Javascript Player API: Getting Started,
When the player is ready, the API will call the onYouTubePlayerReady callback function.
Wrap
var ytplayer = document.getElementById("ytplayer_div1");
ytplayer.loadVideoById("bHQqvYy5KYo", 5, "large");
with
onYouTubePlayerReady = function() {
var ytplayer = document.getElementById("ytplayer_object");
ytplayer.loadVideoById("bHQqvYy5KYo", 5, "large");
}
Take note of document.getElementById("ytplayer_object"), where the id must equal the id attribute passed into swfobject.embedSWF here:
{
id: 'ytplayer_object'
}

Init YouTube iframe player without video ID

Is there a way to init YouTube iframe player without actualy loading video itself?
If do not provide videoId video is not loaded (no errors is shown) and player instance remains in "ready" state.
ytPlayer = new YT.Player('video-player', {
height: '100%',
width: '100%',
videoId: 'video-id-goes-here',
playerVars: {
.....
.....
Thanks for answer!
Sure, if you want to, the following code does work:
<script>
function onYouTubePlayerAPIReady() {
var player = new YT.Player('player', {
events: {
onReady: function() {
player.loadVideoById('sOEAD-gfJ_M');
}
}
});
}
</script>
<script src="//www.youtube.com/player_api" type="text/javascript"></script>
Alternatively, if you know you're going to be loading a playlist, you can leave out videoId and put in
playerVars: {
list: 'PLAYLIST_ID'
}
But while both of those approaches do work, what are you trying to accomplish? There might be a better way of doing it than initializing a YT.Player without the videoId.

How make fancybox auto close when youtube video id done?

How i can make fancybox auto close when youtube video ended?
This isn't as simple as it sounds:
First determine if you are going to use the embed or iframe player.
Follow the embed player API or iframe player API examples to initialize the API.
Use the "onComplete" fancybox callback functon to set up the player once the popup is open.
In the callback, make sure you add an onStateChange event listener so you can determine when the video has completed (the value of zero means the video has ended)
Once you find that the video has ended, then use the $.fancybox.close method to close the popup
or, you could just let the user close the popup.
this is the full script with Fancybox-Youtube-Cookie-Autoclose-Autopopup just download the images that required in css put them in /fancybox folder in your root and replace with your video id. Really works fully tested...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/helpers/jquery.fancybox-media.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" media="screen" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="http://www.youtube.com/player_api"></script>
<script type="text/javascript">
// detect mobile devices features
var isTouchSupported = 'ontouchstart' in window,
isTouchSupportedIE10 = navigator.userAgent.match(/Touch/i) != null;
function onPlayerReady(event) {
if (!(isTouchSupported || isTouchSupportedIE10)) {
// this is NOT a mobile device so autoplay
event.target.playVideo();
}
}
function onPlayerStateChange(event) {
if (event.data === 0) {
$.fancybox.close();
}
}
function onYouTubePlayerAPIReady() {
$(function() {
if ($.cookie('welcome_video')) {
// it hasn't been three days yet
} else {
// set cookie to expire in 3 days
$.cookie('welcome_video', 'true', { expires: 3});
$(document).ready(function () {
$.fancybox.open({
href: "https://www.youtube.com/embed/qm1RjPM9E-g", /*YOUR VIDEO ID*/
helpers: {
media: {
youtube: {
params: {
autoplay: 1,
rel: 0,
// controls: 0,
showinfo: 0,
autohide: 1,
}
}
},
buttons: {}
},
beforeShow: function () {
var id = $.fancybox.inner.find('iframe').attr('id');
var player = new YT.Player(id, {
events: {
onReady: onPlayerReady,
onStateChange: onPlayerStateChange
}
});
}
}); // fancybox
}); // ready
} // cookie else ready
}); // function for cookie
} // youtube API ready
</script>
After looking around without any luck here is a solution I came up with
See what we do, watch a video here
<div style="display: none;">
<div id="player"></div>
</div>
<script src="http://www.youtube.com/player_api"></script>
<script>
$(document).ready(function () {
$("a.video_button").fancybox({
'titlePosition' : 'inside',
'transitionIn' : 'none',
'transitionOut' : 'none'
});
});
// create youtube player
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 's19V_6Ay4No',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
}
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
$(document).ready(function () { parent.$.fancybox.close();});
}
}
</script>
Yes this is the right way to handle to fancybox or colorbox. Youtube Video API provides different states to handle this like unstarted=-1, ended=0, playing=1, paused=2, buffering=3, video cued=5
Getting or traping this state in fancybox jquery code block one can achieve this easily. Just visit this article with proper demo also.Visit here

Resources