PDA

View Full Version : play audio/video files randomly


mrjameer
09-11-2007, 08:23 PM
hi,



i want to play some audio and video files(separately) randomly.any tutorials or links or any help will be surely appreciated.i search in google.but i dont get any useful links..


thanks
mrjameer

madmatter23
09-11-2007, 09:14 PM
I needed to design something similar for one of my sites a while back. I needed a n image/video to play randomly when the page loaded. Each image/video needed to have it's own specific rollover effect and hyperlink. I ended up, with help, making a javascript code that did this. I know this is a php forum and my script is going to be slightly different that yours, but I think this can help you move in the right direction. I'll throw a few comments in to point out the parts that you'll need to understand.


//Create an Array for holding new objects
var theImage = new Array()


//Create my Image Object and assign its properties.
theImage[0] = new objImage()
theImage[0].url = 'image1.gif';
theImage[0].hyperlink = 'image1.html';
theImage[0].over = 'image1r.gif';

theImage[1] = new objImage()
theImage[1].url = 'image2.jpg';
theImage[1].hyperlink = 'image2.html';
theImage[1].over = 'image2r.jpg';

theImage[2] = new objImage()
theImage[2].url = 'image3.jpg';
theImage[2].hyperlink = 'image3.html';
theImage[2].over = 'image3r.jpg';

//================================
//end definitions
//================================

//assign a variable to the length of theImage Array (total images added)
var p = theImage.length;

//Calculate a random number between 0 and the total amount of theImage
var whichImage = Math.round(Math.random()*(p-1));

//Write the HTML onto the page
function showImage(){

document.write('<a href="javascript:void(0)" onClick="openWindow(\''+theImage[whichImage].hyperlink+'\')">');
document.write('<img src="'+theImage[whichImage].url+'"');
document.write(' onmouseover="this.src=\''+ theImage[whichImage].over +'\'"');
document.write(' onmouseout="this.src=\''+theImage[whichImage].url +'\'">');
document.write('</a>')

}

// define openWindow variables
function openWindow(hyperLink) {
window.open (hyperLink,"Answer","scrollbars=0,status=0,toolbar=0,location=0,width=630,height=335");
}


As you can see, this general method could easily be translated to php.

Basically you need to:
1) Create an array.
2) Store info in array
a) a number for each possible video/image/audio file
b) information about video/image/audio file location, etc
eg: so one array value could equal $randomArray[$itemNumber]["picture.jpg"];
4) Generate random number between 0 and sizeof($randomArray)
5) take that number and use it to draw on the corresponding numbered image/audio/whatever in $randomArray
6) display that image/audio/etc

Hope that's clear enough. I've got to run, let me know if I was confusing.

mlseim
09-11-2007, 10:18 PM
How many do you have?

Perhaps a simple flat-file database might be good if you have a lot of them.
The PHP script would open that file and randomly pick a line, that line would
have all of the information needed (filename, title, length, etc).