Take the 2-minute tour ×

What is the simplest way to play a looping sound in an iPhone app?

share|improve this question
@Federico Thanks for that insight. :-) –  middaparka Mar 8 '11 at 19:21
From now on call me Captain Obvious. I hope it's obvious I was joking :-) –  Federico Culloca Mar 8 '11 at 20:00
@Federico (Or should that be @Captain?) Yup, I'd say it was pretty obvious you were jesting. :-) –  middaparka Mar 8 '11 at 20:03
add comment

2 Answers

up vote 10 down vote accepted

Probably the easiest solution would be to use an AVAudioPlayer with the numberOfLoops: set to a negative integer.

// *** In your interface... ***
#import <AVFoundation/AVFoundation.h>


AVAudioPlayer *testAudioPlayer;

// *** Implementation... ***

// Load the audio data
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"sample_name" ofType:@"wav"];
NSData *sampleData = [[NSData alloc] initWithContentsOfFile:soundFilePath];
NSError *audioError = nil;

// Set up the audio player
testAudioPlayer = [[AVAudioPlayer alloc] initWithData:sampleData error:&audioError];
[sampleData release];

if(audioError != nil) {
    NSLog(@"An audio error occurred: \"%@\"", audioError);
else {
    [testAudioPlayer setNumberOfLoops: -1];
    [testAudioPlayer play];

// *** In your dealloc... ***
[testAudioPlayer release];

You should also remember to set the appropriate audio category. (See the AVAudioSession setCategory:error: method.)

Finally, you'll need to add the AVFoundation library to your project. To do this, alt click on your project's target in the Groups & Files column in Xcode, and select "Get Info". Then select the General tab, click the + in the bottom "Linked Libraries" pane and select "AVFoundation.framework".

share|improve this answer
add comment

The easiest way would be to use an AVAudioPlayer set to an infinite number of loops (or finite if that's what you need).

Something like:

NSString *path = [[NSBundle mainBundle] pathForResource:@"yourAudioFileName" ofType:@"mp3"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:path];

AVAudioPlayer *_player = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[file release];

_player.numberOfLoops = -1; 
[_player prepareToPlay];
[_player play]; 

This will simply loop whatever audio file you have specified indefinitely. Set the number of loops to any positive integer if you have a finite number of times you want the audio file to loop.

Hope this helps.


share|improve this answer
Im getting the error: "Use of undeclared identifier 'AVAudioPlayer'" –  Linuxmint Mar 8 '11 at 19:12
Make sure you have #import <MediaPlayer/MediaPlayer.h> at the top of your file or in your header file. –  Brenton Morse Mar 8 '11 at 19:13
@Brenton Isn't it in #import <AVFoundation/AVFoundation.h> ? (Quite possibly in both, I suppose.) –  middaparka Mar 8 '11 at 19:15
@middaparka I thought it was just MediaPlayer that you needed to import, but you may be right. If you're still getting the error Linuxmint try importing AVFoundation too as midda suggested. Also make sure you have correct frameworks added to your project. I'd add CoreMedia, AVFoundation and MediaPlayer just be safe, –  Brenton Morse Mar 8 '11 at 19:41
middaparka should be right... but don't forget to import the framwork too in your xCode project... –  meronix Mar 8 '11 at 19:48
show 1 more comment

Your Answer


