You can use JavaScript to set a cookie that video has been played once. Read that cookie, it you get it, do not autoplay. Here is a sample. The cookie expires after number of days I have set days = 7. So after 7 days the video will autoplay again. Change var days = 7; if you want.
<script language="Javascript">
function played(){
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf("played=") == 0) return 1;
}
var date = new Date();
var days = 7;
date.setTime(date.getTime()+(days*24*60*60*1000));
document.cookie = "played=1"+"; expires="+date.toGMTString()+"; path=/";
return 0;
}
if(played()==0){
document.write("<embed src=\"http://www.youtube.com/v/z435EriT9eY&hl&autoplay=1\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"460\" height=\"340\"></embed>");
}
else{
document.write("<embed src=\"http://www.youtube.com/v/z435EriT9eY&hl\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"460\" height=\"340\"></embed>");
}
</script>
Hi there. Thanks for this script, its really helpful. I've just tried to have a video play on my website just once and not play or show up anywhere on the website after the visitor has seen it. This code works on firefox only and not on chrome or IE9. Is there a reason why it should not work? I've uploaded it onto a server so its not a local page.
<div id="player"></div>
<script language="Javascript">
function played(){
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf("played=") == 0) return 1;
}
var date = new Date();
var days = 7;
date.setTime(date.getTime()+(days*24*60*60*1000));
document.cookie = "played=1"+"; expires="+date.toGMTString()+"; path=/";
return 0;
}
var tag = document.createElement('script');
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '1067',
width: '1900',
videoId: 'NR9bBqZCeqU',
playerVars: {'controls': 0 },
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
if(played()==0)
{
function onPlayerReady(event) {
event.target.playVideo();
}
}
else{
stopVideo();
}
var done = false;
function onPlayerStateChange(event) {
if(event.data === YT.PlayerState.ENDED) {
stopVideo();
}
}
function stopVideo() {
player.stopVideo();
document.getElementById("player").style.height = "0px";
}
</script>