[Help] Playing Sound on UIButton Click

Discussion in 'iOS Development' started by cocotutch, Jul 25, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Ok,

    Here is the code I want to use:

    H File:

    [OBJC]#import <AudioToolbox/AudioToolbox.h>

    SystemSoundID theSound;
    NSURL *pathToChickenSound;
    NSString *pathToSound;
    [/OBJC]

    M File:

    [OBJC]pathToSound = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource

    Please Register or Log in to view images

    "click" ofType

    Please Register or Log in to view images

    "wav"] isDirectory:NO];
    OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)pathToSound, &theSound);

    AudioServicesPlaySystemSound (theSound);[/OBJC]

    But it's recommended by Apple that you "Destroy" the sound after it's played to free up memory. I want this same sound to apply to every button, that is:

    Blue(UIButton) Clicked > Sound Plays + Functions complete > Sound Destroys.
    Red(UIButton) Clicked > Same Sound Plays even though it was Destroyed on the last button + Functions complete > Sound Destroys again.

    How can I achieve this? I have been at it for ages, and I want to add sound support for iPhone and iPod Touch 2G/3G/3GS users. I keep hitting walls with the code however, If I click the button in the Simulator, the sound plays at least 4-5 times, I want it to play once, its a simple "Click" similar to the Keyboard Click Sound.

    cocotutch
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    This is a better way to accomplish what you are doing:

    Sound.h
    [OBJC]
    #import <Foundation/Foundation.h>
    #import <Audiotoolbox/AudioToolbox.h>

    @interface Sound : NSObject {

    }

    + (void)soundEffect

    Please Register or Log in to view images

    int)soundNumber;

    + (void)vibrate; // this is optional, I threw it in there for fun

    @end
    [/OBJC]

    Sound.m
    [OBJC]
    #import "Sound.h"

    @implementation Sound

    + (void) soundEffect

    Please Register or Log in to view images

    int)soundNumber {
    NSString *effect;
    NSString *type;
    if (soundNumber == 0) {
    effect = @"click";
    type = @"caf";
    }
    else if (soundNumber == 1) {
    effect = @"beep";
    type = @"caf";
    }
    /*else if (soundNumber == 2) {
    effect = @"alert";
    type = @"caf";
    }*/

    SystemSoundID soundID;

    NSString *path = [[NSBundle mainBundle] pathForResource:effect ofType:type];
    NSURL *url = [NSURL fileURLWithPath

    Please Register or Log in to view images

    ath];

    AudioServicesCreateSystemSoundID ((CFURLRef)url, &soundID);

    AudioServicesPlaySystemSound(soundID);


    }

    + (void)vibrate {
    AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
    }

    - (void)dealloc {
    [super dealloc];
    }

    @end

    I commented out the last case just to demonstrate that you can add as many as you want and call them the same, simple way every time.

    [/OBJC]

    Call the method

    [OBJC][Sound soundEffect

    Please Register or Log in to view images

    int)soundNumber];[/OBJC]

    i.e.

    [OBJC][Sound soundEffect:1];[/OBJC]

    to vibrate

    [OBJC][Sound vibrate];[/OBJC]

    __

    You assign the sound file by putting the file name and extension in the correct places, and then adding the integer value to the call.

    You can call [OBJC][Sound soundEffect

    Please Register or Log in to view images

    int)SoundNumber];[/OBJC]
    from anywhere, just import the Sound.h file.
  3. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Create the sound ID in your init method of that class, and then dispose of it in the dealloc method.
  4. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    So in dealloc:

    [Sound release];

    ??
  5. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    You need to do a little reading on allocating objects, their retain counts, and things along that line.
  6. Unregistered

    Unregistered Guest

    pathForResource returning null

    Running in iPhoneSimulator iPad 3.2. I moded the code a bit, but essentially the same. Added a log after pathForResource.


    NSString *path = [[NSString alloc] initWithFormat

    Please Register or Log in to view images

    "%@",[[NSBundle mainBundle] pathForResource:effect ofType:type]];

    NSLog(@"path = %@",path);

    Output:

    2010-11-17 03:22:20.191 PushAndPop[5238:40b] dumnAction //(this calls [Sound soundEffect:1])
    2010-11-17 03:22:20.193 PushAndPop[5238:40b] path = (null)

    Any Ideas?

Share This Page