Play video links via php?

For everything that's not in any way related to PureBasic. General chat etc...
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Play video links via php?

Post by Dude »

Hi, I want to put a bunch of video links on my website so that the user can click a link and then have that video open in a new tab and start playing. Currently I have to have 1 x HTM page for each video to make this work, but I'd rather have one target HTM page (called "player.htm") which knows the filename of the video link that was clicked, and then plays that video.

I can't work out how to do this, but apparently it can be done with PHP instead? Anyone know how? I'm looking for something like this:

Code: Select all

<a href="/video/player.php?file=video1.mp4">First video</a>
<a href="/video/player.php?file=video2.mp4">Second video</a>
...
Thanks.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Play video links via php?

Post by TI-994A »

Dude wrote:...I'd rather have one target HTM page (called "player.htm") which knows the filename of the video link that was clicked, and then plays that video.

I can't work out how to do this, but apparently it can be done with PHP instead?
Even JavaScript is able to extract the URL query, but it's much easier with PHP.

player.php

Code: Select all

<?php 
  if (isset($_GET['file'])) {
    $file = $_GET['file'];
  }
?>

<!DOCTYPE html>
<html lang="en">  
  <body>
    <video id="player" width="640" height="480" controls>
    </video>
  </body>  
  <footer>
    <script> 
      var file = "<?= $file ?>";
      if (file.trim() != "") {
        var player = document.getElementById("player");
        var source = document.createElement("source");
        source.setAttribute("src", file);
        player.appendChild(source);
      } else {
        alert("Video source not found.");
      }
    </script>    
  </footer>  
</html>
Then, simply load the page as you would, with the video file name in the URL query:

Code: Select all

<a href="/video/player.php?file=video1.mp4">First video</a>
<a href="/video/player.php?file=video2.mp4">Second video</a>
...
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Play video links via php?

Post by Dude »

Thanks a lot, TI-994A! :) Works nice as a start, so now I'll tweak the HTML to match my site. Appreciate it.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: Play video links via php?

Post by VB6_to_PBx »

HTML Example : ( only checked out in FireFox Browser )

Code: Select all

<!DOCTYPE html> 
<html> 
<body> 
<button onclick="playPause()">Play/Pause</button>
<button onclick="enableLoop()" type="button">Enable loop</button>
<button onclick="disableLoop()" type="button">Disable loop</button><br>
<button onclick="setQuarterVolume()" type="button">Set volume to 25 %</button>
<button onclick="setHalfVolume()" type="button">Set volume to 50 %</button>
<button onclick="setFullVolume()" type="button">Set volume to 100 %</button>
<button onclick="VidSize640x480()" type="button">Video Size 640x480</button>
<button onclick="VidSize940x705()" type="button">Video Size 940x705</button>
<button onclick="VidSize1160x870()" type="button">Video Size 1160x870</button><br> 

<video id="myVideo" controls>
 /<source src="Black Night by Joe Bonamassa - Bonatube2013-1Qcexr6CZ5Y.mp4" type="video/mp4">
  Your browser does not support HTML5 video.
</video>

<script>
var vid = document.getElementById("myVideo");
vid.volume = 0.5;
vid.width = 940;
//vid.height = 705;

function playPause() { 
    if (myVideo.paused) 
        myVideo.play(); 
    else 
        myVideo.pause(); 
} 


function enableLoop() {
     if (myVideo.loop = true)
        disableLoop();
        //alert("Loop Disabled");
     else
        vid.loop = true;
        alert("Loop Enabled");
} 


function disableLoop() { 
  vid.loop = false;
  alert("Loop Disabled");
  //vid.load();
} 

function getVolume() { 
    alert(vid.volume);
} 

function setQuarterVolume() { 
    vid.volume = 0.25;
} 
  
function setHalfVolume() { 
    vid.volume = 0.5;
} 
  
function setFullVolume() { 
    vid.volume = 1.0;
} 

function VidSize640x480() { 
    vid.width = 640;
    //vid.height = 480;
} 

function VidSize940x705() { 
    vid.width = 940;
    //vid.height = 705;
} 

function VidSize1160x870() { 
    vid.width = 1160;
    //vid.height = 870;
} 

</script> 

</body> 
</html>



or real simple Example :

Code: Select all

<!DOCTYPE html> 
<html> 
<body> 
<iframe width="940" height="705" 
src="https://www.youtube.com/embed/MMB7Xf_7JDs?&vq=hd720&controls=1&autoplay=1&showinfo=1&rel=0"
frameborder="0" allowfullscreen></iframe>
</body> 
</html>
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Play video links via php?

Post by Dude »

Thanks VB6_to_PBx, but it was the "<?= $file ?>" bit of code that I was missing and needed.
Post Reply