0

Here is my code under the AppDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        NSLog(@"PLAY SOUND CLIP WHILE LOADING APP");
        NSURL *clip = [[NSBundle mainBundle] URLForResource: @"project" withExtension:@"m4a"];
        self.startipPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
        [self.startipPlayer play] 
}

This only plays the audio once and not repeat. How do I make this code repeat itself or repeat the song at least?

Or any alternatives to create play background music than can loop would be helpful. Thank you.

Raptor
  • 53,206
  • 45
  • 230
  • 366
Cyril
  • 2,783
  • 1
  • 24
  • 35
  • p.s. missed a trailing semi-colon at the last line ? – Raptor Mar 03 '15 at 02:03
  • @Raptor, yes I know but you get the idea right? – Cyril Mar 03 '15 at 02:03
  • possible duplicate of [iphone app allow background music to continue to play](http://stackoverflow.com/questions/14122363/iphone-app-allow-background-music-to-continue-to-play) – Raptor Mar 03 '15 at 02:05
  • Yes, I do. Instead of using `AVAudioPlayer`, it's better to use `AVAudioSession` whenever possible, see the quoted duplicate question's answer. – Raptor Mar 03 '15 at 02:06

1 Answers1

1

Looking at the documentation for AVAudioPlayer, it appears to have property numberOfLoops, which, if set to negative number, repeats the song indefinitely until you call stop.

Probably something like this (untested):

NSURL *clip = [[NSBundle mainBundle] URLForResource: @"project" withExtension:@"m4a"];
self.startipPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
startipPlayer.numberOfLoops = -1
[self.startipPlayer play] 
MirekE
  • 11,515
  • 5
  • 35
  • 28
  • How exactly do I write this? I've also looked in the documentation but I'm not sure on how to write it. – Cyril Mar 03 '15 at 02:10