user1343858
user1343858

Reputation: 25

breaking out to multiple loops

in my project I have multiple recordings taken from the iPhones microphone which I loop with this code

-(IBAction)loop1{ 
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile error:&error];
    audioPlayer.numberOfLoops = 100; 

[audioPlayer play];

}

and stop with this code

-(IBAction)loopstop1{ 

audioPlayer.numberOfLoops = 0; 

[audioPlayer stop];

}

When more than one recording is looping however this stop method only stops the last recording that was set to loop, I'm wondering if there is anyway to break out of all audio loops, or break these specific loops, as I am only managing to stop the last recording I have looped.

thanks for any help or advice

Upvotes: 1

Views: 106

Answers (1)

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

What I would do :

  1. Create an NSMutableArray to hold the audioPlayers
  2. Add your AVAudioPlayer objects to your array

    AVAudioPlayer* newLoop = [[AVAudioPlayer alloc] initWithContentsOfURL:temporaryRecFile 
                                                                    error:&error];
    newLoop.numberOfLoops = 100; 
    
    [loopsArray addObject:newLoop];
    
  3. Pass an index to each of the start / stop functions, to know which audioPlayer to start/stop.


Hint : To stop all of them, just call the stop method on each of your loops store in the loopsArray

Upvotes: 1

Related Questions