Jump to content

Random Sound


Traveller29

Recommended Posts

Morning all - I am very new to PHP so please be gentle!

 

I currently use the <bgsound ...> in an HTML file which plays a .wav sound byte on opening. I want to provide a random sound (from a list of six) but can't seem to find a PHP script anywhere that will provide this for me. I can find random images and have this working just fine!

 

Could some kind soul point me in the right direction please?

 

MTIA,

Link to comment
Share on other sites

Hi Traveller29,

 

You could place the sounds in an array and then use the array_rand() function to choose a random sound.  e.g.:

 

$sounds = array("sound1.mp3", "sound2.mp3", "sound3.mp3", "sound4.mp3", "sound5.mp3");
$rand_sound = array_rand($sounds);

echo '<BGSOUND SRC="'.$rand_sound.'">';

 

 

array_rand() manual: http://us2.php.net/manual/en/function.array-rand.php

 

Hope this helps.

Link to comment
Share on other sites

Thanks for that but no luck so far?!

 

As I said, I am new to PHP so can you confirm what exactly I do?

 

I put the array into a file (changing the .mp3 to .wav) and made a file called random_sound.php.

 

I then included in my HTML file a calling line <?php include("random_sound.php"); ?> and put the six sounds in my root directory.

 

one thing just crossed my mind - the index file I am using is .HTML on the server; does this have to be .PHP also??

 

 

Link to comment
Share on other sites

Hi Traveller29,

 

Don't bother about putting the code into a separate file.  Rename your index.html to index.php and stick the code into the top of your page exactly like:

 

<?php
$sounds = array("sound1.wav", "sound2.wav", "sound3.wav", "sound4.wav", "sound5.wav");
$rand_sound = array_rand($sounds);
?>

 

Then, scroll down your page until you come across the php code which deals with the background sound and change it to read:

 

<BGSOUND SRC="<?php echo $rand_sound; ?>">

 

Hope this helps.

Link to comment
Share on other sites

Thank you gents - BINGO!!

 

Works a treat now - very greatful for your input.

 

Just one other question - now I have my index named as a .PHP what is the best way to be able to view locally?

 

I use CodeLobster for the PHP editing but the viewer doesn't view the files in the entriety.

Link to comment
Share on other sites

Installed both (one at a time!) without a lot of success?

 

When I click on the index.php it still opens in CodeLobster but if I open it in a server then it says 'localhost' - Viewer is running TeamViewer (I know I am but only when doing some remote work which I am not and the proggy is 'dormant'!?).

 

Will persist over the weekend ... onwards!

 

Thanks again for your patience.

Link to comment
Share on other sites

Well, I seem to have WAMPserver working as I can now open my PHP project site in Firefox!

 

Changed all the HTML links to PHP and they all seem to call OK - with the exception of the sound as previously sorted!? btw, in my original <bgsound src= ...> code line there was 'loop="1" /' afetr the wav file. How do I get that into the array please?

 

Other than that not really a problem other than I like to have things working entirely for test purposes :-/

 

I mentioned the 'localhost' and 'TeamViewer' irregulatory. Well the only way I could get the above to work was to remove the TeamViewer files running in the background; now that can't be right can it?

 

Ho hum ....

 

 

 

 

Link to comment
Share on other sites

Taa for that - my code is now complete!

 

TeamViewer: I will have a look at that but I would have preferred a fix for the WAMPserver port listening rather than mess with TeamViewers!

 

Firefox sound: Seems there is a 'bug' in firefox that stops the <bgsound> statement from working?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.