title: Play a sound based on the button pressed on the IPhone/xcode
body: <p>I'm trying to play a sound based on which button is pressed using AVAudioPlayer. 
  <em>(This is not a soundboard or fart app.)</em></p>
  
  <p>I have linked all buttons using this code in the header file:</p>
  
  <pre><code>@interface appViewController : UIViewController &lt;AVAudioPlayerDelegate&gt; {
  
  AVAudioPlayer *player;
  
  UIButton *C4;
  UIButton *Bb4;
  UIButton *B4;
  UIButton *A4;
  UIButton *Ab4;
  UIButton *As4;
  UIButton *G3;
  UIButton *Gb3;
  UIButton *Gs3;
  UIButton *F3;
  UIButton *Fs3;
  UIButton *E3;
  UIButton *Eb3;
  UIButton *D3;
  UIButton *Db3;
  UIButton *Ds3;
  UIButton *C3;
  UIButton *Cs3;
  }
  
  @property (nonatomic, retain) AVAudioPlayer *player;
  
  @property (nonatomic, retain) IBOutlet UIButton *C4;
  @property (nonatomic, retain) IBOutlet UIButton *B4;
  @property (nonatomic, retain) IBOutlet UIButton *Bb4;
  @property (nonatomic, retain) IBOutlet UIButton *A4;
  @property (nonatomic, retain) IBOutlet UIButton *Ab4;
  @property (nonatomic, retain) IBOutlet UIButton *As4;
  @property (nonatomic, retain) IBOutlet UIButton *G3;
  @property (nonatomic, retain) IBOutlet UIButton *Gb3;
  @property (nonatomic, retain) IBOutlet UIButton *Gs3;
  @property (nonatomic, retain) IBOutlet UIButton *F3;
  @property (nonatomic, retain) IBOutlet UIButton *Fs3;
  @property (nonatomic, retain) IBOutlet UIButton *E3;
  @property (nonatomic, retain) IBOutlet UIButton *Eb3;
  @property (nonatomic, retain) IBOutlet UIButton *D3;
  @property (nonatomic, retain) IBOutlet UIButton *Db3;
  @property (nonatomic, retain) IBOutlet UIButton *Ds3;
  @property (nonatomic, retain) IBOutlet UIButton *C3;
  @property (nonatomic, retain) IBOutlet UIButton *Cs3;
  
  - (IBAction) playNote;
  
  @end
  </code></pre>
  
  <p>Buttons are all linked to the event "playNote" in interfaceBuilder and each note is linked to the proper referencing outlet according to note name.</p>
  
  <p>All *.mp3 sound files are named after the UIButton name (IE- C3 == C3.mp3).</p>
  
  <p>In my implementation file, I have this to play a only one note when the C3 button is pressed:</p>
  
  <pre><code>#import "sonicfitViewController.h"
  
  @implementation appViewController
  @synthesize C3, Cs3, D3, Ds3, Db3, E3, Eb3, F3, Fs3, G3, Gs3, A4, Ab4, As4, B4, Bb4, C4;
  
  // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  - (void)viewDidLoad {
  
      NSString *path = [[NSBundle mainBundle] pathForResource:@"3C" ofType:@"mp3"];
      NSLog(@"path: %@", path);
      NSURL *file = [[NSURL alloc] initFileURLWithPath:path];
  
      AVAudioPlayer *p = [[AVAudioPlayer alloc]
                          initWithContentsOfURL:file error:nil];
      [file release];
  
      self.player = p;
      [p release];
  
      [player prepareToPlay];
      [player setDelegate:self];
  
      [super viewDidLoad];
  }
  
  - (IBAction) playNote {
      [self.player play];
  }
  </code></pre>
  
  <p>Now, with the above I have two issues:</p>
  
  <ul>
  <li>First, the NSLog reports NULL and crashes when trying to play the file. I have added the mp3's to the resources folder and they have been copied and not just linked. They are not in an subfolder under the resources folder.</li>
  <li><p>Secondly, how can I set it up so that when say button C3 is pressed, it plays C3.mp3 and F3 plays F3.mp3 without writing duplicate lines of code for each different button? playNote should be like </p>
  
  <p>NSString *path = [[NSBundle mainBundle] pathForResource:nameOfButton ofType:@"mp3"];</p></li>
  </ul>
  
  <p>instead of defining it specifically (@"C3").</p>
  
  <p><strong>Is there a better way of doing this and why does the *path report NULL and crash when I load the app?</strong></p>
  
  <p>I'm pretty sure it's something as simple as adding additional variable inputs to - (IBAction) playNote:buttonName and putting all the code to call AVAudioPlayer in the playNote function but I'm unsure of the code to do this.</p>