title: AVAudioPlayer Help: Playing multiple sounds simultaneously, stopping them all at once, and working around Automatic Reference Counting
body: <p>I am trying to create buttons that <code>play</code> single sound files and one <code>button</code> that <code>stops</code> all of the sounds that are currently playing.  If the user clicks multiple buttons or the same <code>button</code> in a short amount of time, the app should be playing all of the sounds simultaneously.  I have accomplished this without much difficulty using the System Sound Services of iOS.  However, the System Sound Services play the sounds through the <code>volume</code> that the <code>iPhone's</code> ringer is set to.  I am now trying to use the <code>AVAudioPlayer</code> so that users can <code>play</code> the sounds through the media volume.  Here is the code that I am currently (yet unsuccessfully) using the play the sounds:</p>
  
  <pre><code>-(IBAction)playSound:(id)sender
  {
     AVAudioPlayer *audioPlayer;
     NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Hello" ofType:@"wav"];
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile] error:nil];
     [audioPlayer prepareToPlay];
     [audioPlayer play];
  }
  </code></pre>
  
  <p>Whenever I run this code in the iPhone Simulator, it does not play the sound but does display a ton of output.  When I run this on my iPhone, the sound simply does not play.  After doing some research and testing, I found that the <code>audioPlayer</code> variable is being released by Automatic Reference Counting.  Also, this code works when the audioPlayer variable is defined as an instance variable and a property in my interface file, but it does not allow me to play multiple sounds at once.</p>
  
  <p>First thing's first: How can I play an infinite number of sounds at once using the <code>AVAudioPlayer</code> and sticking with Automatic Reference Counting?  Also: When these sounds are playing, how can I implement a second <code>IBAction</code> method to stop playing all of them?</p>