I’ve got a problem with my audio player (http://www.vegapohl.com/audio). I’m trying to have the player play a random track from a mysql database, but I don’t want it to play a previous song. Only once all songs have been listened to or skipped, it should “reset” and play all tracks again.
Here is my PHP:
<?php
mysql_connect("localhost", "db57276_11", "*****")or die("cannot connect");
mysql_select_db(db57276_11)or die("cannot select DB");
$sql = "SELECT * FROM audio ORDER BY RAND()";
$result = mysql_query($sql);
if (false === $result) {
echo mysql_error();
}
$row = mysql_fetch_row($result);
$url = $row[0];
$title = $row[1];
$artist = $row[2];
$albumart = $row[3];
?>
I found some answers to similar questions on stackoverflow, but none of them worked for me. To give you a better idea of the general functionality, here is the body markup:
<body>
<script type="text/javascript">
function playPause(soundId,getImg) {
setTimeout(swapImg,30000,getImg);
soundId.volume=.5;
if (soundId.paused){
soundId.play();
getImg.src='pause.png';
}
else {
soundId.pause();
getImg.src='play.png';
}
}
function swapImg(getImg){
getImg.src='play.png';
}
</script>
<div class="wrapper">
<div class="wrapmiddle">
<div class="middle">
<div class="track">
<h1><?php echo $title; ?></h1>
<h2><?php echo $artist; ?></h2>
<div id="buttons">
<a href="javascript:window.location.reload()" class="next">Skip</a>
<a href="#"class="like">Share</a><a href="#" class="view">Visit Site</a>
</div>
</div>
</div>
</div>
<div class="left">
<div>
<audio id="track1" onended = "window.location.reload()">
<source src="<?php echo $url; ?>" type="audio/mp3"></source>
Your browser does not support this content.</audio>
<a href="#" onClick="playPause(track1,playImg1);">
<img alt="" class="playImg" src="play.png" style="cursor: pointer;" name="playImg1" />
</a><br />
</div>
</div>
<div class="right">
<div class="overlay"><img src="icon.png" width="35" height="34" /> </div>
</div>
</div>
</body>
Maybe someone knows of a better way of playing the next track (instead of refreshing the page/iframe). If it could play each track in the table in order and then start from the top again that would also work. Hope someone can help me with this.
Add an additional smallint field to your SQL table, something like
status. In that store a 1 or 0 (or something else you devise) to mean unplayed/unskipped, or played/skipped. In your SQL query, add a where clause along the lines ofSELECT songname,artist,filelocation,status FROM audio WHERE status=0 ORDER BY rand()