Show YouTube video in Twig from the URL - html

I'm trying to show Youtube videos in Symfony (in the Twig template). I can't use the iframe method because I need to use the general URL (https://www.youtube.com/watch?v=jUjeps1hxIw&t=671s instead of https://www.youtube.com/embed/jUjeps1hxIw).
I have found examples with the HTML tag but they doesn't work.

Finally I have found a solution. I had to change the url, what is that I didn't want.
<div id="youtube-ficha">
{% for artistYoutube in group.youtube %}
<iframe class="myIframe" width="560" height="315" frameborder="0" allowfullscreen></iframe>
<script type="text/javascript">
var url = "{{ artistYoutube}}";
var id = url.split("?v=")[1]; //sGbxmsDFVnE
var embedlink = "http://www.youtube.com/embed/" + id;
var ytube1 = document.getElementsByClassName("myIframe");
ytube1[{{loop.index0}}].src = embedlink;
</script>
{% endfor %}
</div>

If you don't want to use tag you can use
<object width="420" height="315"
data="https://www.youtube.com/embed/jUjeps1hxIw">
</object>
<embed width="420" height="315"
src="https://www.youtube.com/embed/jUjeps1hxIw">
but you have to give the embed link of your you tube video which you can find by following below steps
play your video on you tube
right click on your video
select copy embed code
past it in notepad and copy that link and use it in above tag

Related

Automatically Displaying the latest 5 videos for youtube in a html

I am trying to display the lastest five videos from my youtube channel on one HTML page. I have managed to successfully embed one youtube video with this code but I am unable to upload any other one. I have tried to embed the second latest video as well but no matter what I try, I get a playback error with invalid ID as seen above. Does anyone have a clue how I could embed the latest 5 videos from my YouTube Chanel in JSON/JavaScript?
Below is my code
var channelID = "MY-ID";
var reqURL = "https://www.youtube.com/feeds/videos.xml?channel_id=";
$.getJSON("https://api.rss2json.com/v1/api.json?rss_url=" + encodeURIComponent(reqURL)+channelID, function(data) {
var link = data.items[0].link;
var id = link.substr(link.indexOf("=")+1);
$("#youtube_video").attr("src","https://youtube.com/embed/"+ id + "?controls=0&showinfo=0&rel=0");
});
<iframe id="youtube_video" width="600" height="340" frameborder="0" allowfullscreen></iframe>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
I Found It you have to change the number in the []'s.
var channelID = "MY-ID";
var reqURL = "https://www.youtube.com/feeds/videos.xml?channel_id=";
$.getJSON("https://api.rss2json.com/v1/api.json?rss_url=" + encodeURIComponent(reqURL)+channelID, function(data) {
var link = data.items[0].link;
var id = link.substr(link.indexOf("=")+1);
$("#youtube_video").attr("src","https://youtube.com/embed/"+ id + "?controls=0&showinfo=0&rel=0");
var link = data.items[1].link;
var id = link.substr(link.indexOf("=")+1);
$("#youtube_video2").attr("src","https://youtube.com/embed/"+ id + "?controls=0&showinfo=0&rel=0");
var link = data.items[2].link;
var id = link.substr(link.indexOf("=")+1);
$("#youtube_video3").attr("src","https://youtube.com/embed/"+ id + "?controls=0&showinfo=0&rel=0");
});
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<iframe id="youtube_video" width="600" height="340" frameborder="0" allowfullscreen></iframe>
<iframe id="youtube_video2" width="600" height="340" frameborder="0" allowfullscreen></iframe>
<iframe id="youtube_video3" width="600" height="340" frameborder="0" allowfullscreen></iframe>

Vimeo api play failed because the user didn't interact with the document first

Vimeo player will now play even with allow="autoplay" attribute, check jsfiddle console error, click small icon play button:
https://jsfiddle.net/0vfLtdm8/
var player = document.getElementById('video-player');
var vimeoPlayer = new Vimeo.Player(player);
playbtn.onclick = function() {
vimeoPlayer.play();
}
vimeoPlayer.on('error', function(error) {
console.log(error);
});
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<script src="https://player.vimeo.com/api/player.js"></script>
<div id="video-outer-full">
<div id="video-inner">
<i class="far fa-play-circle" id="playbtn"></i>
<iframe id="video-player" class="video" width="560" height="315" src="https://player.vimeo.com/video/309741585" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen allow="autoplay"></iframe>
</div>
</div>
#Toniq this is an issue specific to jsfiddle and codepen like services.
On those test pages they wrap the user defined test content in an iframe which doesn't have the allow="autoplay" which prevents the play() action from happening.
Also the Vimeo player iframe has to have the allow="autoplay" attribute.
You can see on the api demo page that this problem doesn't occur if you press the custom play button on the top right.
https://player.vimeo.com/api/demo

Vimeo player pause another video when you play any video

I have 2 Vimeo players in my page when I play the first video then play second video first one gets paused.
var vid1=new Vimeo.Player($('.one'));
var vid2=new Vimeo.Player($('.two'));
$('.btnonePlay').on('click',function(){
vid1.play();
})
$('.btnonePause').on('click',function(){
vid1.pause();
})
$('.btntwoPlay').on('click',function(){
vid2.play();
})
$('.btntwoPause').on('click',function(){
vid2.pause();
})
Steps on how to reproduce the issue:
Click on play video one
Click on play video two
Player one will pause when the 'play video two' button is clicked.
Working example on jsFiddle.
The entire code is also in the snippet below.
var vid1 = new Vimeo.Player($('.one'));
var vid2 = new Vimeo.Player($('.two'));
$('.btnonePlay').on('click', function() {
vid1.play();
})
$('.btnonePause').on('click', function() {
vid1.pause();
})
$('.btntwoPlay').on('click', function() {
vid2.play();
})
$('.btntwoPause').on('click', function() {
vid2.pause();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="one">
<iframe src="https://player.vimeo.com/video/286183716" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<p>Annihilation Song | Rubblebucket (Official Music Video) from Amanda Bonaiuto on Vimeo.</p>
</div>
<button class="btnonePlay"> Play video one</button>
<button class="btnonePause"> Pause video one</button>
<div class="two">
<iframe src="https://player.vimeo.com/video/286183716" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<p>Annihilation Song | Rubblebucket (Official Music Video) from Amanda Bonaiuto on Vimeo.</p>
</div>
<button class="btntwoPlay"> Play video two</button>
<button class="btntwoPause"> Pause video two</button>
By default on our Vimeo videos we enable autopause. Autopause is when one video automatically pauses when another starts playing. Most people do not want two videos playing at the same time.
However, if you do want that you can turn off autopause by using the embed parameter attached to the url ?autopause=0. I have adjusted your jsfiddle to work accordingly. Working JSFiddle!
You can learn more about the available embed parameters from our API readme or this help article.

Muting an embedded vimeo video

On a website I am building I have a vimeo video embedded. The client needs to keep the sound on the video obviously for people that find it on vimeo. However for her website the sound is just plain annoying. So I need to find a way to mute the audio within the code for the embed. I have googled it but can't seem to find anything legible. As you can see from my code below, I have used the autoplay command within the link I was hoping I could do a similar thing to mute the sound.
<iframe src="http://player.vimeo.com/video/4415083? title=0&byline=0&portrait=0&color=d01e2f&autoplay=1" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
Thanks
In case it helps anyone, Vimeo have added a 'background' parameter for embedding videos, that autoplays videos silently. In some cases that will be useful where people want to mute videos - this is their example:
<iframe src="https://player.vimeo.com/video/76979871?background=1"
width="500" height="281" frameborder="0" webkitallowfullscreen
mozallowfullscreen allowfullscreen></iframe>
For non-paying members
You just need to add the muted parameter. E.g.:
<iframe src="https://vimeo.com/48400332?autoplay=1&loop=1&muted=1" ></iframe>
For paid members
According to Vimeo, the background parameter is only supported for videos hosted by paid members.
Source: https://help.vimeo.com/hc/en-us/articles/115004485728-Autoplaying-and-looping-embedded-videos
you will be using setVolume api in your vimeo.. player.api('setVolume', 0);
it will be like this...
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//f.vimeocdn.com/js/froogaloop2.min.js"></script>
<iframe id="vimeo_player" src="http://player.vimeo.com/video/4415083?title=0&byline=0&portrait=0&color=d01e2f&autoplay=1&player_id=vimeo_player" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<script>
var iframe = $('#vimeo_player')[0],
player = $f(iframe),
status = $('.status');
player.addEvent('ready', function() {
player.api('setVolume', 0);
});
</script>
I tried the examples in the answers with no luck. After looking into the documentation I noticed there is missing the parameter &player_id=IFRAME_ID. Maybe something changed in the Vimeo API, anyway the following example works for me:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://f.vimeocdn.com/js/froogaloop2.min.js"></script>
<iframe id="vimeo_player" src="//player.vimeo.com/video/4415083?api=1&player_id=vimeo_player&autoplay=1&loop=1&color=ffffff" width="1920" height="1080" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<script>
$(function() {
var vimeo_iframe = $('#vimeo_player')[0];
var player = $f(vimeo_iframe);
player.addEvent('ready', function() {
player.api('setVolume', 0);
});
});
</script>
Seems like Vimeo is providing an updated library, and the API syntax is a bit different from the old one (Froogaloop). Here's how to mute an embedded video with JS:
<!--Add the id attr to the iframe tag to use as a selector-->
<iframe id="embeddedVideo" src="http://player.vimeo.com/video/4415083? title=0&byline=0&portrait=0&color=d01e2f&autoplay=1" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<!--Include the Vimeo API Library-->
<script src="https://player.vimeo.com/api/player.js"></script>
<!--Select and manipulate your video-->
<script type="text/javascript">
//Select the #embeddedVideo element
var video = document.getElementById('embeddedVideo');
//Create a new Vimeo.Player object
var player = new Vimeo.Player(video);
//When the player is ready, set the volume to 0
player.ready().then(function() {
player.setVolume(0);
});
</script>
Hope this helps out anyone who's using the new library. Documentation is at vimeo/player.js
Since most of the answers here are referring to Vimeo's old api. Here is the simplest way with the new api. You can include vimeo player.js from their CDN or you can download it or you can include it from npm.
<script src="https://player.vimeo.com/api/player.js"></script>
or
npm install #vimeo/player
then you can do the following.
<script>
var iframe = document.querySelector('iframe');
var player = new Vimeo.Player(iframe);
player.setVolume(0);
</script>
that's it. And if you are using angular 2+,
import * as Vimeo from '#vimeo/player';
const iframe = e.target;
const player = new Vimeo(iframe);
player.setVolume(0);
here e.target is $event which would be passed from the template. Probably it could be iframe (load) event. Or may be you can use jquery to select iframe.
#Gadss answer works great but I found that you need update the iframe src to include the activation of the Vimeo api. Just include api=1 after the vimeo id.
I've also found that this sometimes doens't work on Safari for some reason.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//f.vimeocdn.com/js/froogaloop2.min.js"></script>
<iframe id="vimeo_player" src="http://player.vimeo.com/video/4415083?api=1&title=0&byline=0&portrait=0&color=d01e2f&autoplay=1" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<script>
var iframe = $('#vimeo_player')[0],
player = $f(iframe),
status = $('.status');
player.addEvent('ready', function() {
player.api('setVolume', 0);
});
</script>
**Here is my solution:
http://jsfiddle.net/jakeoblivion/phytdt9L/5/
(You will need your own play/pause mute/unmute icons)
//load video muted
var video = $("#myvideo");
video.vimeo("play");
video.vimeo("setVolume", 0);
//toggle play/pause
$('#play-pause').click(function() {
$(this).toggleClass('play');
if ($(this).hasClass('play')) {
//pause video
video.vimeo("pause");
$(this).css('background', 'pink');
} else {
//unpause video
video.vimeo("play");
$(this).css('background', 'blue');
}
});
//toggle mute/unmute
$('#mute-unmute').click(function() {
$(this).toggleClass('mute');
if ($(this).hasClass('mute')) {
//unmute video
video.vimeo("setVolume", 1);
$(this).css('background', 'green');
} else {
//mute video
video.vimeo("setVolume", 0);
$(this).css('background', 'red');
}
});
Spent ages trying and nothing seemed to work to.
I just wanted to have a Vimeo autoplay muted (volume 0) with simple Play/Pause Mute/Unmute controls, instead of their default ones. (feel free to use icons instead of the temporary colours I put).
(if you want to add the default controls back but keep muted, remove "?background=1". By default background=1 will set video.vimeo("setVolume", 0) and hide controls, so I also added the mute on load without the background=1 set).
Also note:
"You’ll need to be running on a web server instead of opening the file directly in your browser. JS security restrictions will prevent the API from working when run locally."
I've found a way to do it. You start the video muted so it autoplays, then on the first timeupdate you set the volume to 1.
var options = {
id: 'video_id_here',
width: 640,
loop: false,
muted: true,
autoplay: true
};
var player = new Vimeo.Player('vimeo', options);
player.setVolume(0);
player.on('timeupdate', set_autoplay_volume );
function set_autoplay_volume(){
player.setVolume(1);
player.off('timeupdate', set_autoplay_volume );
}
You try insert ?muted=1 after link in attribute src
For example
<iframe id="vimeo_player" src="https://player.vimeo.com/video/257992348?muted=1">
This is the only way it worked for me: http://jsfiddle.net/87dsjL8q/108/
var iframe = document.getElementsByTagName('iframe')[0];
var player = $f( iframe );
player.addEvent('ready', function() {
player.api('setVolume', 20);
});
<iframe src="http://player.vimeo.com/video/4415083?muted=1; title=0;byline=0;portrait=0;color=d01e2f;autoplay=1" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
you can just give "muted=1" so the video will be muted...
chrome allow the videos autoplay that are muted

Embedding Youtube Video into HTML for iOS

I am loading an html file to my webview using [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"faq" ofType:#"html"] isDirectory:NO]]]; Now I want to embed a youtube video into the html file. I have the file but don't know what to add in order for it to work. I have found code that let's you load it as a string but what I need is to load it inside the html file. I have tried adding, with a few fixes, the same code that I have found on other posts to the html file, but it always comes up as an external link, instead of embedded inside the html file. Here is what I have:
<script type="text/javascript" src="http://www.youtube.com/iframe_api"></script>
<script type="text/javascript">
function onYouTubeIframeAPIReady()
{
ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})
}
function onPlayerReady(a)
{
a.target.playVideo();
}</script>
<iframe id="playerId" type="text/html" width="250" height="180" src="http://www.youtube.com/embed/9qkXs768JKY?enablejsapi=1&rel=0&playsinline=1&autoplay=1" frameborder="0"></iframe>
And my ViewController has :
self.webView.allowsInlineMediaPlayback = YES;
self.webView.mediaPlaybackRequiresUserAction = NO;
Any help and guidance is appreciated. Thanks in advance.
Other attempt:
I have tried the code below inside the HTML file but now it shows a play button with a line through it. Any idea how to fix it?
<video width="280" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
<object data="movie.mp4" width="220" height="240">
<script type="text/javascript" src="http://www.youtube.com/iframe_api"></script>
<script type="text/javascript">
function onYouTubeIframeAPIReady()
{
ytplayer=new YT.Player("playerId",{events:{onReady:onPlayerReady}})
}
function onPlayerReady(a)
{
a.target.playVideo();
}
</script>
<iframe id="playerId" type="text/html" width="250" height="180" src="http://www.youtube.com/embed/9qkXs768JKY?enablejsapi=1&rel=0&playsinline=0&autoplay=0" frameborder="0"></iframe>
<!-- <embed src="http://www.youtube.com/embed/9qkXs768JKY" width="220" height="200">
</embed> -->
</object>
</source>
</source>
</video>
I am using this to play youtube videos in web views:
<iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/XDJxoVIqdW0" frameborder="0">
For your UIWebView, you need to set allowsInlineMediaPlayback to YES in order to show the media in your app. It defaults to NO on the iPhone.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/Reference/Reference.html#//apple_ref/occ/instp/UIWebView/allowsInlineMediaPlayback