views:

270

answers:

2

We have a youtube player embedded in a plage in Mobile Safari and it works great. But we need to be able to launch the youtube player by a means other than user tapping the video itself, for various reasons.

So I am trying to figure out what event to trigger in javascript to make it happen with no luck. None of the following appear to work.

var vid = document.getElementById('vid');
vid.click();
vid.onclick();
vid.ontouchend();
vid.ontouchstart();
vid.focus();

I tried to find an event handler added to the embedded object with this snippet, but didn't find anything.

for (var key in vid) {
  if (typeof vid[key] == 'function') console.log(key +': '+ vid[key]);
}

Is this just so wrapped up in a custom plugin there is no way?

A: 

You'd have to edit the src for the video. This should work:

vid.setAttribute('src', vid.getAttribute('src') + '&autoplay=1');

This will set the src attribute to a version that will automatically play. This is assuming the vid element is an embed element, if it's an object one you'll have to set the value of the "movie" param element.

Source: Autoplay embedded videos

Arda Xi
Thanks. But, sadly, it would appear the Mobile Safari does not support autoplay on youtube videos.
Squeegy
A: 

In that case, you'd probably have to try embedding the video using the Javascript Player API and then calling playVideo() on the video.

Arda Xi