LAA
LAA

Reputation: 13

Looping a sound in Cocoa Touch

I have a rectangle button, when the user presses it once. the sound plays But I want it so when the user double taps the button, the sound continously loops till the user decided to press to stop the sound. This is the code to play the sound.

- (IBAction)oneSound:(id)sender; {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"wav"];
    if (theAudio) [theAudio release];
    NSError *error = nil;
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
    if (error)
        NSLog(@"%@",[error localizedDescription]);
    theAudio.delegate = self;
    [theAudio play]; 
}

Thanks!

Upvotes: 0

Views: 518

Answers (1)

Anne
Anne

Reputation: 27073

Add this line:

theAudio.numberOfLoops = -1;

The negative value makes the player loop forever until it is stopped.

Upvotes: 1

Related Questions