0

I'm trying to use this code that I thought would work but I can't seem to get it to work

(function($){
var video = $('video'),
    v = video[0]
    control = $('.control');
// $(window).load(function(){
//  $('.control').click();
//  $('.control').trigger('touchstart');
// });
// when can play, play
video.bind('canplay', function(){
    v.play();
});
$(window).blur(function(e) {
    // Left the tab
    v.pause();
});
$(window).focus(function(e) {
    // Back to the tab
    v.play();
});
$('.control').on('click touchstart', function(){
    v.play();
});
})(jQuery);

I've also tried the window.load as you can see the commented section there, and I even tried sending both events just in case the mobile OS is ignoring clicks and listening for touch events.

I am trying to get a video to play automatically, I know there are tons of questions but I've tried so many of the code snippets in them with no luck, and through some other google searches I've found this bit but nothing works. Is there really no solution to this at all?

Jordan
  • 2,393
  • 4
  • 30
  • 60
  • have you try `$(document).ready(function() {...});` ? – MamaWalter Feb 26 '14 at 19:25
  • Wrapping it in that doesn't help, still doesn't work, what's strange is the click event on the invisible div I have overtop of the video plays the video programmatically like that no problem, it's just that I can get the on load or canplay event to fire that play method. – Jordan Feb 26 '14 at 19:34

1 Answers1

0

You can try with $(document).ready:

(function($){

$(document).ready(function() {  

    var v = $('video').get(0);

    v.play();

    ... 

});

})(jQuery);

an alternative could be to use attribute <video autoplay="autoplay">...</video>:

MamaWalter
  • 2,073
  • 1
  • 18
  • 27
  • but not on a phone or tablet device – Jordan Feb 26 '14 at 20:20
  • 1
    finding this: http://stackoverflow.com/questions/12496144/can-you-autoplay-html5-videos-on-the-ipad – MamaWalter Feb 26 '14 at 20:28
  • Yes I've found this question before as well but the problem is that it shows a workaround for having the user click something, I don't want to force the user to click something. I want it to start playing when the user hits the page. And everything I've found so far has not lead me to a solution. – Jordan Feb 26 '14 at 21:15
  • That's what I'm doing right now but it still doesn't work on a mobile device – Jordan Feb 27 '14 at 12:41