Im trying to play a random music file that loops in SpriteKit. I have achieved this but theres a delay in-between the loop using SKAction like this. Regardless of the simulator or device.
self.backgroundMusicArray = [[NSMutableArray alloc] initWithObjects:@"BackgroundMusic1.mp3",@"BackgroundMusic2.mp3",@"BackgroundMusic3.mp3",@"BackgroundMusic4.mp3",@"BackgroundMusic5.mp3",@"BackgroundMusic6.mp3", nil];
int randomNumber = arc4random() % self.backgroundMusicArray.count;
SKAction* play = [SKAction playSoundFileNamed:[self.backgroundMusicArray objectAtIndex:randomNumber] waitForCompletion:YES];
SKAction *loop = [SKAction repeatActionForever:play];
[self runAction:loop];
I have also achieved this with NSURL & AVAudioPlayer without the delay problem, but I'm lost on how to make it random. So I guess my question is "how can I do this randomly with AVAuidoPlayer". Im fairly new to objc so any help is greatly appreciated.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"BackGroundMusic1"
ofType:@"mp3"]];
self.backGroundplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
self.backGroundplayer.numberOfLoops = -1;
[self.backGroundplayer play];