iPhone Button Click Sound


 

iPhone Button Click Sound

iPhone Button Click Sound, In this series of iPhone button tutorial, we are going to create a small iphone application that is going to play a sound on clicking the button in iPhone SDK

iPhone Button Click Sound, In this series of iPhone button tutorial, we are going to create a small iphone application that is going to play a sound on clicking the button in iPhone SDK

iPhone Button Click Sound

In this series of iPhone button tutorial, we are going to create a small iphone application that is going to play a sound on clicking the button in iPhone SDK.

For the audio we are going to use AVAudioplayer in iPhone SDK tutorial. First of all... create a new ViewBased application and in Frameworks add the "AVFoundation.frameWork" by right clicking on the Framworks.

Command: Framworks->Reveal with Finder->Choose AVFoundation.foundation and ADD it to you list.

Now add your .wav sound file into your project. To add the sound file just copy and paste it into you project. You can also do it by right clicking on the project name (on the top in Group & files(left menu) in Xcode), choose add->Existing File.

After adding the sound file into project and AVFoundation framework.. now we can do some coding to play the sound. In my application i am using a button to give the action to play a sound. You can add and switch the button with action in Interface builder. If you don't know how to create the button then please check the previous button examples in this website.

In the ViewController.h file just replace the code with the below given code...

buttonSoundViewController.h

#import <UIKit/UIKit.h>
#import<AVFoundation/AVAudioPlayer.h>

@interface buttonSoundViewController : UIViewController <AVAudioPlayerDelegate> {
    }
-(IBAction) playSound:(id)sender;

@end

Here as you can see that we are importing one more file that is required to pay the sound.

#import<AVFoundation/AVAudioPlayer.h>

This will work only if you have AVFoundation.framework added into Frameworks. We also need to create a delegate of it otherwise it will give a warning that AVAudioPlayerDelegate is not declared.

In ViewController.m file, we need to write the button action as given below...

-(IBAction) playSound:(id)sender{
   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"awh_man" ofType:@"wav"];
    AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
   
    theAudio.delegate=self;
    [theAudio play];
     
    }

Now save your file and run the application to listen the sound on button click. The sound file that i have used is freely available on the internet. Instead of it you can create your own or use system sound as well...
The final application will look like the given image... you can play the sound on clicking the button.

Download AVAudioplayer iPhone SDK Tutorial Here

Ads