The issue is that you don't have control of events within the iframe, so can't tell when the user swipes over that area. The work around I suggest is to replace the iframe with a image placeholder while not watching. In order to do this use YouTube's Iframe API to keep track of video events. When the video goes from play to pause we will hide the video and show the image. Here is a demo.
HTML
<div ng-app="app">
<div ng-controller="ctrl" ng-swipe-right="msg($event, 'right')" ng-swipe-left="msg($event, 'left')">
<div id="player"></div>
<img id="player-cover" src="http://img.youtube.com/vi/M7lc1UVf-VE/hqdefault.jpg" />
</div>
</div>
JS
On initiation it hides the video. When ever the image is clicked it shows and plays the video. When the video goes from paused to play onPlayerStateChange
handles toggling the image and video. Every time the swiping event gets called on the image it also triggers the click event handler for the image. The variable swiping
keeps track of if the event was just a click or also a swipe.
var app = angular.module('app', ['ngTouch']);
app.controller('ctrl', function($scope) {
$scope.msg = function(event, msg) {
swiping = true;
alert(msg);
}
});
// keep track of the user swiping. onYouTubeIframeAPIReady needs to occur outside of Angular.
var swiping = false;
// Youtube related.
document.getElementById('player').style.display = 'none';
document.getElementById('player-cover').addEventListener("click", function() {
if (swiping) {
swiping = false;
return;
}
document.getElementById('player').style.display = 'block';
player.playVideo();
});
// This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// This function creates an <iframe> (and YouTube player) after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: { 'onStateChange': onPlayerStateChange }
});
}
function onPlayerStateChange(event) {
if (event.data === YT.PlayerState.PAUSED || event.data === YT.PlayerState.ENDED) {
document.getElementById('player-cover').style.display = 'block';
document.getElementById('player').style.display = 'none';
} else {
document.getElementById('player-cover').style.display = 'none';
document.getElementById('player').display = 'block';
}
}