Ok,
Step 1:
I don't know what player you are using to play the vidoes, in the script I wrote below I am using
JW Player. It plays files that are in the following formats:
FLV, MP4, MP3
If your videos are in a different format use a free converter like
WINFF to convert your videos to any of the above formats (except MP3 that's only for streaming audio).
So go ahead and download JW player from the above link, unzip the file and upload the folder called "mediaplayer" to the root of your server.
Step 2:
In the database you should only have the url path of the video files. For example:
http://www.mysite.com/path/to/videofile
That's it, nothing else - just the
absolute url path to the file. In the script below I am assuming this is how your database structure looks:
Code:
id | video_title | video_path
----------------------------
1 | Title One | http://www.mysite.com/path/to/videofile1
2 | Title Two | http://www.mysite.com/path/to/videofile2
etc..
I am also assuming your database is called "videos" and the table inside that database is called "my_videos".
If you want to throw away that database you were using and just go with the example one I made then create a new database called "videos", go inside it and just copy and paste the below code in the sql tab of PHPmyadmin or type it in mysql monitor (if that's what you are using):
Code:
CREATE TABLE IF NOT EXISTS `my_videos` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`video_title` varchar(1000) NOT NULL,
`video_path` varchar(1000) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
Next enter your video path urls under the "video_path" column, you can also add video titles under the "video_title" column, you don't have to enter anything under the "id" column - that will populate automatically.
Step 3:
Here's the PHP script to play videos at random inside JW player:
PHP Code:
<?php
// connect to db
mysql_connect("localhost", "username", "password") or die ("Could not establish database connection.");
mysql_select_db("videos") or die ("Could not select database.");
// grab a random video to play
$query = mysql_query("SELECT * FROM my_videos ORDER BY rand() LIMIT 1");
while ($row = mysql_fetch_assoc($query)) {
$videoTitle = $row['video_title'];
$video = $row['video_path'];
}
?>
<?php echo "Now Playing: $videoTitle <hr />"; ?>
<script type="text/javascript" src="mediaplayer/swfobject.js"></script>
<div id="1">If you can read this either you have JavaScript turned off or you do not have <a href="http://get.adobe.com/flashplayer/">Flash Player</a> installed.</div>
<script type="text/javascript">
var so = new SWFObject('mediaplayer/player.swf','mpl','610','330','9');
so.addParam('allowscriptaccess','always');
so.addParam('allowfullscreen','true');
so.addParam('flashvars','&file=<?php echo $video; ?>');
so.write('1');
</script>
Notes:
I know you are streaming your own videos, but if you want you can also stream youtube videos inside JW player. Just enter the youtube url of the video you want to stream like you did with the video file paths in the database. As long as that video doesn't have embedding disabled it will work like any other stream. Just remember to use youtube urls like these:
http://www.youtube.com/watch?v=x4c_wI6kQyE
and not like:
http://www.youtube.com/watch?v=x4c_w...ture=topvideos
- Good luck