Search code examples
iosobjective-caudioplayback

Make a button play a sound in iOS


I am trying to make an app that when you touch a button (actually a .png image that I imported) a sound plays. Does anybody know how to do this? I am using xcode 4.5.2


Solution

  • NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"your audio file name" ofType:@"wav"]];
    AVAudioPlayer *click  = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile  error:nil];
    [click play];
    

    Write this code on action of your button click.Also Add AudioToolbox/AudioToolbox.h and AVFoundation/AVFoundation.h frameworks to your class.