Skip to content
Advertisement

PHP: How to play random mp4 videos in static url .php

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?

Advertisement

Answer

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 … 🙂

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement