Embed randomly named MP3

亡梦爱人 提交于 2019-12-23 23:57:23

问题


Here's my code:

<embed src="/sound/lowyourchicken.mp3" 
width="140" height="40" autostart="true" loop="TRUE"> 
</embed> 

I would like the src for the .mp3 to take in to account that there are many randomly named .mp3 files in the /sound/ directory, and to choose one at random each time the page is opened. Any clues for me?

My server is PHP enabled but I'd like to keep this as simple as possible.


回答1:


This should do it:

$files = glob("/path/to/directory/*.mp3");
$random = array_rand($files)

Then do this:

<embed src="<?php echo $random ?>" 
width="140" height="40" autostart="true" loop="TRUE"> 
</embed> 



回答2:


array_rand returns what random index it chose, so you'll need to do this:

<embed src="<?php $files[ $random ] ?>"



回答3:


Try This: It will Work, I used original code found in answers and did some tweaking by adding array($files) in the $random = array_rand(); variable statement

You will first need to put the PHP code in the body like this

<?php
$files = glob("assets/songs/SayYesToLove/*.mp3");
$random = array_rand(array($files));
?>

next add this just outside that php code in the body

<embed src="<?php echo $files[$random]; ?>" width="140" height="40" autostart="true" loop="TRUE">
</embed>

Please Notice the echo output in the src file. This will ensure it gets outputted to your page. Also don't forget to use the ; at the end of every php variable statement as this can through some errors.



来源:https://stackoverflow.com/questions/5979639/embed-randomly-named-mp3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!