3

How can you make the video playback very random? (right now it's playing after French, Nederlands and after Nederlands, French, but I want randomly shuffle out of this two)

var playing ='FR';
function video_idle() {
    $('#mediaplayer').attr('src',playing + '.mp4' ).show(); 
    mediaplay_video= document.getElementById('mediaplayer');  
    mediaplay_video.play();  
    mediaplay_video.onended = function(e) {
      console.log('>>> Playing finished: ', e);
      if(playing ==='FR') {
        playing='NL';
        video_idle();
      } else {
        playing='FR';
        video_idle();
      }      
    };   
}
weston
  • 54,145
  • 21
  • 145
  • 203

2 Answers2

1

Assuming you only want to shuffle between two possible languages, you can use Math.round(Math.random()) which will give you a value of 0 and 1 (which in JavaScript will test as either true or false):

var playing ='FR';
function video_idle() {
    $('#mediaplayer').attr('src',playing + '.mp4' ).show(); 
    mediaplay_video= document.getElementById('mediaplayer');  
    mediaplay_video.play();  
    mediaplay_video.onended = function(e) {
      console.log('>>> Playing finished: ', e);
      if(Math.round(Math.random())) {
        playing='NL';
        video_idle();
      } else {
        playing='FR';
        video_idle();
      }      
    };   
}

If you have more languages, or you expect the number of languages to increase, you will want to consider the solution proposed by @Lars Ebert.

net.uk.sweet
  • 12,444
  • 2
  • 24
  • 42
  • 1
    `Math.round(Math.random())` will be zero 50% of the time, that's why this will work. – vdwijngaert Apr 22 '15 at 08:15
  • Is there anyway to stop flickering when playing nl.mp4 to fr.mp4? like playing seamlessly ? right now when the video change to the next one i see a black screen instead of seamless –  Apr 22 '15 at 08:20
  • 1
    Have a look at some of the solutions proposed here :- http://stackoverflow.com/questions/9776490/gapless-transition-from-video-to-video-using-html5 – net.uk.sweet Apr 22 '15 at 08:25
0

To choose a random video, simply place all possible languages into an array and choose one at random:

var languages = ['FR', 'NL'];
var playing = 'FR';
function video_idle() {
    $('#mediaplayer').attr('src',playing + '.mp4' ).show();
    mediaplay_video= document.getElementById('mediaplayer');
    mediaplay_video.play();
    mediaplay_video.onended = function(e) {
        console.log('>>> Playing finished: ', e);

        playing = languages[Math.floor(languages.length * Math.rand())];
        video_idle();
    };
}

languages.length * Math.rand() gives you a random float between 0 and the length of your array, then Math.floor() rounds it down, so that you get a random integer from 0 to length-1, which are your array-keys.

Lars Ebert
  • 3,487
  • 2
  • 24
  • 46
  • When i switch video1 to video2 (Google Chrome). I notice a black screen and then playing next video. How to play seamlessly? So that you do not notice the flicker of switching video1 to next one? –  Apr 22 '15 at 08:23
  • $('#mediaplayer').prop('loop', true); when i use true its playing one file seamlessly without flickering. when i use false then i have flickering. i have to use false because its multiple languages from the array. –  Apr 22 '15 at 08:24
  • 1
    As suggested, that is a separate problem and you should look at this question: http://stackoverflow.com/questions/9776490/gapless-transition-from-video-to-video-using-html5 – Lars Ebert Apr 22 '15 at 08:28