6

I would like to make a sound play when a button is pressed. Also, there is more than one sound. I am using Xcode 4.4.1 and Storyboard.

In the .h file

{
    IBOutlet UIButton *playSound;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
user
  • 99
  • 1
  • 1
  • 5
  • 1
    Please show what you have currently; it's unlikely anyone is going to write a project for you. – Dustin Aug 09 '12 at 16:45
  • I'm modifying your question's title to remove Xcode from it. Xcode is merely the developer environment. What you really care about is that you want to do this under iOS. – Michael Dautermann Aug 09 '12 at 16:51
  • Actually, it's Cocoa-Touch that's important, as it's the API he will use inside iOS. :) – Almo Aug 09 '12 at 17:22
  • { IBOutlet UIButton *playSound; } in the.h file – user Aug 09 '12 at 17:30

2 Answers2

22

I thought it would be fun to write this type example so I wrote it. It demonstrates how to play different random sound when button is pressed:

-(IBAction)buttonPressedWithSound:(id)sender {

    int randomSoundNumber = arc4random() % 4; //random number from 0 to 3

    NSLog(@"random sound number = %i", randomSoundNumber);

    NSString *effectTitle;

    switch (randomSoundNumber) {
        case 0:
            effectTitle = @"sound1";
            break;
        case 1:
            effectTitle = @"sound2";
            break;
        case 2:
            effectTitle = @"sound3";
            break;
        case 3:
            effectTitle = @"sound4";
            break;

        default:
            break;
    }

    SystemSoundID soundID;

    NSString *soundPath = [[NSBundle mainBundle] pathForResource:effectTitle ofType:@"caf"];
    NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];

    AudioServicesCreateSystemSoundID ((CFURLRef)soundUrl, &soundID);
    AudioServicesPlaySystemSound(soundID);  
}

Explanation:

  • Add four sounds in Your project: sound1.caf, sound2.caf, sound3.caf and sound4.caf.

  • Import AudioToolbox framework to Your project. And include in .h #import <AudioToolbox/AudioToolbox.h>.

  • Don't forget to connect Your button to buttonPressedWithSound via IBAction.

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
  • @KyleGreenlaw You're getting this because You didn't included AudioToolbox framework to project (take a look at explanation at the bottom). – Justin Boo Aug 09 '12 at 18:41
  • Can i use a mp3 file and not a caf file? – user Aug 09 '12 at 18:44
  • Sure, just change type to mp3 like this: `NSString *soundPath = [[NSBundle mainBundle] pathForResource:effectTitle ofType:@"mp3"];` – Justin Boo Aug 09 '12 at 18:51
  • @KyleGreenlaw I created project for you for more clarity so take a look at it http://www8.zippyshare.com/v/70589015/file.html. – Justin Boo Aug 09 '12 at 19:14
  • Could you provide a link to the sound effects? – Zorayr Apr 11 '14 at 07:00
  • @JustinBoo thanks. It's just that I have been looking to find an appropriate button press sound effect, but I can't seem to find the right one. That's why I asked for the source. I don't think my users would like the car tag sounds from soundbible when they press a button. – Zorayr Apr 11 '14 at 18:34
  • @JustinBoo "mp3" didn't work for me in this case, I had to convert sound to .caf – Boris Y. Nov 17 '17 at 16:21
1

I have found this way, and it works for me

SystemSoundID soundID;
NSString *soundPath = [[NSBundle mainBundle] pathForResource:ClickSoundFile ofType:FileTypemp3];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID ((__bridge CFURLRef)soundUrl, &soundID);
AudioServicesPlaySystemSound(soundID);

*Note

ClickSoundFile : Sound file name FileTypemp3 : file type mp3

Rahul K Rajan
  • 776
  • 10
  • 19