Using jfugue, how do I generate random music strings (using player.play) from an array?

user11471046 :

I'm using Jfugue in Eclipse, and I have a list of music strings in the code. When I run the code, it plays back all of them, but I want to play back one at a time in random order.

So far I'm using:

Pattern pattern = new Pattern ("A");
Player.play(A);
Pattern pattern = new Pattern ("B");
Player.play(B);

I've tried using "Random random = new Random();" But this has not been working, and I don't know how to implement it.

I've also tried re-suing a random word generator:

  for(int i = 0; i < numberOfTest; i++) {
    int index = (int)(Math.random() * 10);
    System.out.println(strings[index]);

But I don't know how to replace the word strings with music strings:

In general most of my problems stem from a lack of familiarity with the correct syntax, especially Jfugue.

GhostCat salutes Monica C. :

Put them into a list, and then shuffle that:

List<Pattern> allPatterns = Arrays.asList(new Pattern ("A"), new Pattern ("B"), ... more patterns);
Collections.shuffle(allPatterns);

And please note: the above is plain and simple java, it works independently of any specific framework such as jfugue.

For playing, you simply have to tell the player to play the patterns using the order of your shuffled list:

// by using the for-each loop
for (Pattern onePattern : allPatterns) {
   player.play(onePattern);
}

// alternatively, turn the list back into an array and play that
player.play(allPatterns.toArray(new Pattern[0]);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=100486&siteId=1