Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 39

How to do unlimited repetition by using AVAudioPlayer in ios

In my app i am using audio. I want to play audio for unlimited time on tapping button. Is it possible?

Upvotes: 1

Views: 759

Answers (2)

swilliams
swilliams

Reputation: 48940

AVAudioPlayer has a numberOfLoops method. Set that to -1 for unlimited repetition.

Upvotes: 5

rdurand
rdurand

Reputation: 7410

Easiest way is to re-play the player when it reaches the end :

-(void)startPlayer {

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"]];
    NSError *error;
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    player.delegate = self;
    if (error) {
        NSLog(@"Error in audioPlayer: %@", [error localizedDescription]);
    } else {
        [aPlayer play];
    }
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)aPlayer successfully:(BOOL)flag {

    aPlayer.currentTime = 0;
    [aPlayer play];
}

Call [self startPlayer]; once, and it will loop forever !

Upvotes: 0

Related Questions