views:

53

answers:

1

I am trying to play a short sound whenever clicks a button on the iPhone; however, for some reason whenever I click the button (which calls the playClickSound method below), no sound plays. I have the project linked with AudioFoundation.framework and the sound file (clickSound.mp3) in the Resources folder of my application bundle. Can someone please help me figure out what I am missing, and why the AVAudioPlayer won't play my sound file?

- (void) playClickSound {

    // Get path of sound file in bundle.
    NSString *path = [[NSBundle mainBundle] pathForResource: @"clickSound" ofType: @"mp3"];

    // Create url from path.
    NSURL *url = [NSURL URLWithString: path];

    // Create error.
    NSError *error;

    // Create audio player, and play file.
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: &error];

    if (![audioPlayer play]) {
        [error localizedDescription];
    }

    [audioPlayer release];



} // playClickSound
A: 

Your method never actually tells the audioPlayer to play. Try this after you allocate the audio player:

[audioPlayer play];

Also - you may want to look into the delegate method to release the player after the sound has played. I don't think your sound will play if you immediately release it after creating it like you are doing here.

crgt