I want to manipulate a youtube video to autoplay and loop using js/jquery. It seems my issues stem from the from the fact that my site is on a CMS (Drupal), and it already spits out the iframe. So it's not an empty div that gets replaced as per the documentation.
The first thing I did was to change some settings on my site to ensure that "enablejsapi=1" and an ID was included in the output for whatever spits out the iframe.
I thought that would suffice and I'd only need to reference that ID in the onYouTubeIframeAPIReady();
function. But it wasn't working.
I looked at these solutions as well:
- Listening for Youtube Event with this http://jsfiddle.net/YzvXa/197 - It didn't quite work for me. The state change did seem to be working in the fiddle, but when I tried to add parameters it didn't. (I'm assuming parameters only work when applied on load/ready?)
- Already embedded iframe not working with api with this http://jsfiddle.net/y89je0k8/ - I feel like this got me closer, as I was able to atleast autoplay and mute using
event.target.playVideo().mute();
for the ready event. How ever setting parameters likecontrols: 0
etc didn't.
What confuses me is in the second solution, it only works when the js is "inline". In the fiddle it's actually written in the html box in a script tag, but if you move the script in the js box, it doesn't.
Still I do feel like the points stated in the second solution might point me in the right direction, but I'm stuck in how to move forward with it.
- onPlayerReady will not fire the ready check on localhost.
- Also when linking your youtube.js file it has to come after the iframe.
- add ?enablejsapi=1
- sometimes double linking in both player_api and iframe_api will also help
- //< before www. not https://
- placment is key.
Here's what I've tried:
- I've addressed #1 by actually adding my js in the server
- My script is linked just before the closing body tag (so it is after the iframe)
- I have adjusted my cms' youtube handler (Media: Youtube for Drupal) to add this parameter on output
- I have tried this but it didn't seem to make a difference
- I am limited by this as I am bound to using https. Is this the deal breaker?
- I do notice that when I add the standard code to load the the iframe api asynchronously in my js, the
<script src="https://www.youtube.com/iframe_api"></script>
and<script type="text/javascript" id="www-widgetapi-script" src="https://s.ytimg.com/yts/jsbin/www-widgetapi-vflC6bXIS/www-widgetapi.js" async=""></script>
appears as the first items in<head>
, instead of just directly after the iframe as it normally would. I'm unsure why this is happening, but what can I do to ensure it's in the correct place? Perhaps this is the source of the issue, if placement is the key?
What I want to achieve is this behaviour: https://codepen.io/cee-r/pen/PMaRJR, where the video accepts parameters set in js when the html markup is already:
<iframe id="player" title="YouTube video player" width="640" height="360" src="https://www.youtube.com/embed/ApXoWvfEYVU?enablejsapi=1" frameborder="0" allowfullscreen="1" ></iframe>
instead of:
<div id="player"></div>