0

The website here has a music player http://www.numberonedesigns.com/ubermusic.com/ ... When hitting the next button with random highlighted it wont randomize correctly. It always goes back to the first song on the play-list then randomizes on the next.. Here's the variable code. I tried messing with it for a while but decided I needed some help

$next.click(function(){
 var num = 0;
 $.cookie('plate_time', 1);
 if($random.hasClass('active')){
 while(num == $audio.currentTrack){num = Math.floor(Math.random() * (opt.playlist.length));}
 }else{
 num = $audio.currentTrack - 1;
 }
 $audio.loadTrack(num);
 });
 $playlist.on('click', '.track', function(){
 $audio.loadTrack($(this).attr('rel'));
 });
 $prev.click(function(){
 var num = 0;
 $.cookie('plate_time', 0);
 if($random.hasClass('active')){
 while(num == $audio.currentTrack){num = Math.floor(Math.random() * (opt.playlist.length));}
 }else{
 num = $audio.currentTrack - 1;
 }
 $audio.loadTrack(num);
 });
asked Apr 28, 2015 at 18:47

2 Answers 2

1

Since you asked so nicely:

$next.click(function(){
 var num = Math.floor(Math.random() * (opt.playlist.length)); 
 //Set the random number here.
 $.cookie('plate_time', 1);
 if($random.hasClass('active')){
 while(num == $audio.currentTrack){num = Math.floor(Math.random() * (opt.playlist.length));} 
 // leaving it here JIC you get the same track.
 }else{
 num = $audio.currentTrack - 1; 
 // cos the ordering still works here if not on random. 
 }
 $audio.loadTrack(num);
});
$playlist.on('click', '.track', function(){
 $audio.loadTrack($(this).attr('rel'));
});
$prev.click(function(){
 var num = Math.floor(Math.random() * (opt.playlist.length));
 $.cookie('plate_time', 0);
 if($random.hasClass('active')){
 while(num == $audio.currentTrack){num = Math.floor(Math.random() * (opt.playlist.length));}
 }else{
 num = $audio.currentTrack - 1;
 }
 $audio.loadTrack(num);
});
answered Apr 28, 2015 at 19:03
Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick! I guess it's time to really start learning some JS and not relying on everyone else's plugins. You the man! Thanks Tech Chaos
1

You're setting num to 0 at the start of the function. This means that when the current track is playlist[0] the function is triggered, but when the current track isn't playlist[0] but num gets set to 0, the random isn't called and playlist[0] is played again. The while check is failing you with the num set to 0 at the start.

answered Apr 28, 2015 at 18:54

1 Comment

That's what I thought. This is a purchased plugin... What do I do with the 0? Or that line of string? I tried removing it, defining 0 as unlimited and a whole slue of other things. I'm stumped lol

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.