I'm currently making an app that plays a song. I wanted to play a random song each a button is clicked. I currently have :
-(IBAction)currentMusic:(id)sender {
NSLog(@"Random Music");
int MusicRandom = arc4random_uniform(2);
switch (MusicRandom) {
case 0:
[audioPlayerN stop];
[audioPlayer play];
break;
case 1:
[audioPlayer stop];
[audioPlayerN play];
break;
but I already tried :
- (IBAction)randomMusic:(id)sender {
NSLog(@"Random Music");
NSMutableArray * numberWithSet = [[NSMutableArray alloc]initWithCapacity:3];
int randomnumber = (arc4random() % 2)+1;
while ([numberWithSet containsObject:[NSNumber numberWithInt:randomnumber]])
{
NSLog(@"Yes, they are the same");
randomnumber = (arc4random() % 2)+1;
}
[numberWithSet addObject:[NSNumber numberWithInt:randomnumber]];
NSLog(@"numberWithSet : %@ \n\n",numberWithSet);
switch (randomnumber) {
case 1:
[audioPlayerN stop];
[audioPlayer play];
NSLog(@"1");
break;
case 2:
[audioPlayer stop];
[audioPlayerN play];
NSLog(@"2");
break;
default:
break;
}
}
And all of them work, the thing is, even though I will add more songs, they repeat. I wanted a random code that would not repeat. Like plays song 1, song 2, song 3, song 4 and song 5 randomly, and when plays all of them restarts. Like a Loop. But my current code is like song 1, song 1, song 2, song 1, song 2, and so on...Is there any way to not repeat the songs except for when all of them have been played? Thank you very much.