How to do unlimited repetition using AVAudioPlayer in ios

In my application, I use audio. I want to play audio for an unlimited time at the touch of a button. Is it possible?

+1
objective-c iphone avaudioplayer
Mar 14 '14 at 13:09
source share
2 answers

The easiest way is to replay 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 work forever!

0
Mar 14 '14 at 13:16
source share

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

+4
Mar 14 '14 at 20:18
source share



All Articles