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
https://forums.phpfreaks.com/topic/176268-random-sound/
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
https://forums.phpfreaks.com/topic/176268-random-sound/#findComment-928968
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
https://forums.phpfreaks.com/topic/176268-random-sound/#findComment-928982
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
https://forums.phpfreaks.com/topic/176268-random-sound/#findComment-928992
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
https://forums.phpfreaks.com/topic/176268-random-sound/#findComment-929006
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
https://forums.phpfreaks.com/topic/176268-random-sound/#findComment-929051
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
https://forums.phpfreaks.com/topic/176268-random-sound/#findComment-929150
Share on other sites

Hi Traveller 29,

 

To get the loop code in, just change your code to read:

 

<BGSOUND SRC="<?php echo $sounds[$rand_sound]; ?>" loop=1>

 

With regard to TeamViewer, have a look here for a workaround http://tech.nabtron.com/run-teamviewer-with-localhost-webserver-xampp-wamp-etc/310/

 

Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/176268-random-sound/#findComment-929155
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
https://forums.phpfreaks.com/topic/176268-random-sound/#findComment-929251
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.