user2175495
user2175495

Reputation:

Random / Shuffle playlist

I have a Filtered Table of music Titles and would like to shuffle-play them. I tried using Random, but often the same Title got played before all of the others had been played once.

I tried using a shuffled array (I found the method here) that then called the Record's matching AutoInc value...

procedure Shuffle(anArr : TStringList; aMax : Integer);
var
  i: Integer;
begin
  Randomize;
  for i:=0 to aMax do
    anArr.Add(IntToStr(i));
  for i := anArr.Count-1 downto 1 do
    anArr.Exchange(i,Random(i+1));
end;

But, because the Table is filtered, the AutoInc was not contiguous. I don't want to have to scan the Filtered Table for all available AutoInc values and then shuffle that unless I really have to as the Table could be quite large.

Is there any clever way to random shuffle the playing and not repeat until every Title has been played once? I tried using a Boolean Field in the record to indicate "Played," but then I had to scan the entire Table to clear them all before the next round of shuffled play.

It is possible that that the Filtered Table may be played many times over and I'd like a new shuffled order for each full iteration.

Upvotes: 1

Views: 508

Answers (2)

SilverWarior
SilverWarior

Reputation: 8341

Why not simply assign incremental values for non filtered table and then create a shuffled array of those values? All you have to do later is before playing next song to check if that song fits the set filter. If not you skip to the next.

Possible advantages:

  1. No need to scan through filtered table to assign incremental values for its records
  2. Changing of filter won't require you to reshuffle your list. Instead songs that were added by filter will become available. That is if they haven't been skipped already. And the songs that are removed by the updated filter simply become unavailable.

Possible disadvantages:

  1. If filter skips huge number of songs with incremental values finding the next song to play might cause small lag before the next song that suits the filter criteria is found. But I doubt this is much likely to happen. And it can be hidden from the user by finding the next suitable song while the current song is still playing.
  2. Shuffling process would always take the same amount of time regardless the active filter since all songs are shuffled. Which means if you have huge amount of songs total it can take some time. But even that can be solved by providing a multithreaded shuffling where each tread would only shuffle part of the list. Now it won't return same results but still the list will be shuffled enough I guess.

Upvotes: 1

MBo
MBo

Reputation: 80197

Just fill anArr with AutoInc field values from your filtered dataset (instead of continuous indexes), then shuffle the list.

Upvotes: 0

Related Questions