-1

I have a code in PHP language hosted in webcindario.com (free hosting), that shows on click a video randomly. But it works ONLY in VLC Player for Windows.

<?php 

header('Content-type: video/mp4');

$videos  = array(
        'https://gdsit.cdn-immedia.net/video-repository/carini-0281175080.mp4',
        'https://gdsit.cdn-immedia.net/video-repository/funerali-gabriele-conigliaro-7222258160.mp4',
        'https://gdsit.cdn-immedia.net/video-repository/beneficenza-1865095120.mp4',
        'https://gdsit.cdn-immedia.net/video-repository/mercatino-dell-usato-solidale-7035608630.mp4'
    );

$total_video = count($videos);

$total_video--; //array index starting from 0 so decrease 1

$random_index = rand(0, $total_video); //array index 0 to 2

$video_to_play = $videos[$random_index];

echo $video_to_play;

?>

The result is that the Chrome browser or Android don´t show the video: The Chrome browser or Android don´t show the video

And I have these questions:

  1. Is there a way to get the code to work on Android devices and programs like Kodi or Tivimate, inside .m3u lists?

  2. Can it be done in such a way that you don't have to click every time, as if it were a 24/7 video in loop? (on Android and Windows, of course) Is that possible?

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 1
    (1) You may check the ended value of the video IP say by using addEventListener, then load and play another random mp4 (2) Instead of using header, please use tags as the standard way to do it (3) use the play() to trigger playing the video so that there is no need for user to click the play button to do it. For reference, please refer **here**: [link](https://stackoverflow.com/questions/20942900/playing-videos-one-after-another-in-html5) – Ken Lee Sep 12 '21 at 07:41

1 Answers1

0

To display video and several browsers you should use HTML5 so for example:

<?php

//header('Content-type: video/mp4');

$videos  = array(
        'https://gdsit.cdn-immedia.net/video-repository/carini-0281175080.mp4',
        'https://gdsit.cdn-immedia.net/video-repository/funerali-gabriele-conigliaro-7222258160.mp4',
        'https://gdsit.cdn-immedia.net/video-repository/beneficenza-1865095120.mp4',
        'https://gdsit.cdn-immedia.net/video-repository/mercatino-dell-usato-solidale-7035608630.mp4'
    );

$total_video = count($videos);

$total_video--; //array index starting from 0 so decrease 1

$random_index = rand(0, $total_video); //array index 0 to 2

$video_to_play = $videos[$random_index];
?>

<video width="400" controls autoplay>
    <source src="<?php echo $video_to_play;?>" type="video/mp4">
    Your browser does not support HTML video.
</video>

1 This will play on most browsers!

You can see I added "autoplay" to the top of the video element! This will run on most browsers and android / mac - you should know that autoplay on chrome android is usually blocked.

Use some css, to move it around!

2 Since you are using this in php to receive the next video name the page needs reloading so add to the top this :

$page = $_SERVER['PHP_SELF'];
$sec = "20";
header("Refresh: $sec; url=$page");

This will cause the page to reload every 20 second - change the time for 180 sec for 3 mins for example ... :)

Shlomtzion
  • 674
  • 5
  • 12
  • Thanks for your help. I will use your agregated code for another project in html. I have solved my php code in another way ... See below. I have edited it adding 'readfile($videos[array_rand($videos)]);' and adding 'AddType application/x-httpd-php .php .mp4' in the '.htaccess' file. But i have a problem, I can't use the video scroll bar, is there a way to scroll through the video? Greetings. – Jack Bender Sep 16 '21 at 05:49