0

Currently, I'm using this jquery which gets youtube video and plays them in element's background.

I was provided with this code that will play single youtube video. But I would like to make a list of videos and play them randomly... I am not so good with Javascript at the moment, so I would really appreciated some help on this...

right now I have this....

I wanna make a list of videos and play them randomly like a shuffled playlist..

$(function() 
{ 
    var videos = ['xJvt2SDV7ck', 'x6DD1k4BAUg', 'xJvt2SDV7ck'];
    var index = Math.floor(Math.random() * videos.length);

    var Video_back = new video_background($("#bgvideo"), 
    {
        "position": "absolute", //Stick within the div
        "z-index": "-1",        //Behind everything
        "loop": true,           //Loop when it reaches the end
        "autoplay": true,       //Autoplay at start
        "muted": true,          //Muted at start
        "youtube": "videos[index]",   //Youtube video id
        "start": 0,                 //Start with 6 seconds offset (to pass the introduction in this case for example)
        "video_ratio": 1.333,      // width/height -> If none provided sizing of the video is set to adjust
        "fallback_image": "videos/main.jpg",    //Fallback image path
    });
});

Currently It only plays 1 video randomly selected from the list(single loop). I want to make it so that it will move on to another video when the first video finishes..

HELP WILL BE MUCH MUCH APPRECIATED! THANK YOU FOR YOUR TIME!

Hirohito Yamada
  • 387
  • 1
  • 4
  • 17
  • Please define what "isn't working means". Do you have console errors? – Lee Taylor Oct 11 '14 at 21:16
  • You'll probably need to do : `"youtube": videos[index],`. As you have it, youtube is looking for a video with id of "[index]" – Lee Taylor Oct 11 '14 at 21:17
  • thanks for the feedback! It started playing videos randomly! But I also wanted to play those videos I listed above as a playlist so that it won't just play single video but multiple videos on loop. – Hirohito Yamada Oct 11 '14 at 21:27
  • OK. So what is this "video_background"? Is it a plugin? Maybe there's a way of creating a playlist with that, OR, you will need to determine when a video has finished. Perhaps there is a callback to say "video finished"? At the point you could then call your code again. Of course with such a small array of videos the same one may be picked again. So, you may want to prevent that from happening... – Lee Taylor Oct 11 '14 at 21:32
  • thank you sooo much for your help.. I need to finish off this project today... :( Currently im using "Easy Video Background" plugin that I got from Codecanyon. I looked at some of the documents and there seems to be no call back function for "video finished"... – Hirohito Yamada Oct 11 '14 at 21:36
  • when everything is ready, I would have at least 10 videos ready to be on the list, so playing same video won't be too much of a problem I hope...! – Hirohito Yamada Oct 11 '14 at 21:41
  • Well if there is NO WAY of knowing if the video has finished then you would need to store the length of each video and time how long it plays in your code, then set up the next video... – Lee Taylor Oct 11 '14 at 21:42
  • Great. How could I do that...? Would you be kind enough to show me a sample code..? – Hirohito Yamada Oct 11 '14 at 21:52
  • Also, another video background plugin that I tried, worked almost on every browser but IE9. I was wondering if theres any way to figure out why exactly it is not working... http://1ne-studio.com/test/index.html id okite pass whatsup if you could also help me with this that will be much much appreciated...! Ive been trying to get this one work on all browser all night but it didnt so I tried using "easy video background" but it seems like that might take a while... so I thought it might be easier to find out why this one is not working... :( – Hirohito Yamada Oct 11 '14 at 21:57

1 Answers1

1

The following answer is based on the fact that there is no way of determining when a video has finished. The lengths are in milliseconds:

$(function() 
{ 
    var videos = 
    [ 
        { id : 'xJvt2SDV7ck', length : 60000 },
        { id : 'x6DD1k4BAUg', length : 125000 },
        { id : 'xJvt2SDV7ck', length : 166000 }
    ];

    function playNext()
    {
        var index = Math.floor(Math.random() * videos.length);

        alert( "Playing next movie => " + videos[index].id );

        var Video_back = new video_background($("#bgvideo"), 
        {
            "position": "absolute", //Stick within the div
            "z-index": "-1",        //Behind everything
            "loop": true,           //Loop when it reaches the end
            "autoplay": true,       //Autoplay at start
            "muted": true,          //Muted at start
            "youtube": videos[index].id,   //Youtube video id
            "start": 0,                 //Start with 6 seconds offset (to pass the introduction in this case for example)
            "video_ratio": 1.333,      // width/height -> If none provided sizing of the video is set to adjust
            "fallback_image": "videos/main.jpg",    //Fallback image path
        });

        setTimeout(function() 
        {
            playNext();
        }, videos[index].length);
    }

    playNext();
});
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
  • The above is untested but hopefully should be something like the above – Lee Taylor Oct 11 '14 at 22:41
  • 1
    Thank you so much... I owe you big time... I tried the code you provided above. It is still playing just single youtube video on loop. I also tried putting "false" on loop function but it just ended up not stopping the video play at the end of one video... – Hirohito Yamada Oct 11 '14 at 22:52
  • btw, http://1ne-studio.com/test/index2.html id okite pass whatsup is the one that im working on. – Hirohito Yamada Oct 11 '14 at 22:56
  • It does try to play another movie. However, as stated it's quite likely that it would pick the same movie again. Also, if you press F12 in your browser, then load your page you'll see lots of console errors. There are quite a few (other) things that are "broken" which would likely have an adverse affect on the above code... – Lee Taylor Oct 11 '14 at 23:06
  • I've added an `alert` statement in the above code. Place it in your live site and you will see it is attempting to play another movie. – Lee Taylor Oct 11 '14 at 23:08
  • There are few errors that I dont get still... http://1ne-studio.com/content/topics/nishio/okitegami/errors.png 1: i dont use lightbox.min.map, i use lightbox.min.js 2: measureIt.js is not mentioned anywhere but its been called... – Hirohito Yamada Oct 12 '14 at 00:49
  • The following explains the lightbox "issue" (It's not really an issue in your case though) http://stackoverflow.com/questions/18499930/missing-map-resource – Lee Taylor Oct 12 '14 at 02:12
  • measureIt is a browser extension, so again this probably isn't to do with your code. – Lee Taylor Oct 12 '14 at 02:16
  • okay, thanks. So it basically worked..! All I need now is to get that loading spinning image to be hidden when loading and some ratio issues(black border will apprea when I resize windows...) Is there a way to deal with that?? I want to have the same kind of look as this(http://1ne-studio.com/test/index.html id:okite pass:whatsup) so that transition onload and between videos are smooth. So thankful for putting up with me...! I really appreciate it! – Hirohito Yamada Oct 12 '14 at 02:25
  • No worries. You'd be better asking (or searching SO) additional questions to help with those points! :) – Lee Taylor Oct 12 '14 at 02:28
  • Thanks! Yeah, I've been looking for that in SO and Google but I can't find it... and I have to wait 2more days to ask another question...:( – Hirohito Yamada Oct 12 '14 at 03:01