Looping Audio in Xcode

Im reproducing sound in my application that I would like to loop. My research on this has not completely sorted out my problem.

my .m code

CFBundleRef mainBundle = CFBundleGetMainBundle(); CFURLRef soundFileURLRef; soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"ar4", CFSTR ("wav"), NULL); UInt32 soundID; AudioServicesCreateSystemSoundID (soundFileURLRef, &soundID); AudioServicesPlaySystemSound (soundID); 

I think I need to add something like this to

 numberOfLoops = -1; 

But you don’t know how to implement in my code, as I get an undeclared error for numberOfLoops. Some tips will be much appreciated.

+4
source share
1 answer

Something like this should solve your problem:

  NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; resourcePath = [resourcePath stringByAppendingString:@"/YOURMUSICNAME.mp3"]; NSLog(@"Path to play: %@", resourcePath); NSError* err; //Initialize our player pointing to the path to our resource player = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:resourcePath] error:&err]; if( err ){ //bail! NSLog(@"Failed with reason: %@", [err localizedDescription]); } else{ //set our delegate and begin playback player.delegate = self; [player play]; player.numberOfLoops = -1; player.currentTime = 0; player.volume = 1.0; } 

Then, if you want to stop it:

 [player stop]; 

or pause it:

 [player pause]; 

and also import it into the header file:

 #import <AVFoundation/AVFoundation.h>. 

You should, of course, declare it in your title, and then synthesize it.

//. h and add the bold part:

@interface ViewController: UIViewController < AVAudioPlayerDelegate {

 AVAudioPlayer *player; } @property (nonatomic, retain) AVAudioPlayer *player; 

// m.

 @synthesize player; 
+11
source

All Articles