-2

I have a UIButton in a nib (xib) that id like to play a sound when touched, what protocol needs to be followed to make this happen?

** overhauled question** Throughout my time here on Stack Overflow, i've learned a lot, and not just about what i'm usually needing help with (Objective-C, xCode), but with how Stack Overflow works. I find it to be a vast wealth of information and support and i've looked at how i've asked this question before and it made absolutely no sense at all. To help future users, i've edited what I was originally asking for, in a way others can understand like Naveen did below. Apologies

theProject
  • 145
  • 3
  • 16
  • I think it would be EXTREMELY easy to just download the sound file for the internet or some other site. – Allison May 18 '12 at 02:10
  • ok in retrospect as ive learned a lot more since when i asked this question was really new to iOS, but the votes down.. really? im sure someone will stumble across this one day, Naveen got me down there and his answer was right on! – theProject Aug 04 '12 at 01:04

2 Answers2

3

Add this framework

AudioToolBox framework

Include this header file .h file

#include <AudioToolbox/AudioToolbox.h>
CFURLRef        soundFileURLRef;
SystemSoundID   soundFileObject;

Add this code in viewdidLoad

NSURL *tapSound   = [[NSBundle mainBundle] URLForResource: @"Voice035"
                                            withExtension: @"amr"];

    // Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];

    // Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
                                  soundFileURLRef,
                                  &soundFileObject
                                  );

On Button Tap, Call this method

 AudioServicesPlayAlertSound (soundFileObject);

Do release in dealloc

AudioServicesDisposeSystemSoundID (soundFileObject);
CFRelease (soundFileURLRef);

For more info : http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html#//apple_ref/c/func/AudioServicesCreateSystemSoundID

Naveen Thunga
  • 3,675
  • 2
  • 24
  • 31
  • 1
    Naveen, I am so so sorry that I did not accept this answer a LONG TIME ago, this how i went about implementing it for what I needed it too, i just havent been on SO in awhile, but firmly believe rep is important. thanks again – theProject Aug 04 '12 at 01:00
2

I would say, create an 'Observer' class that plays the sound that all buttons are connected to. I'll give an example below, It is a singleton class. It's written on a whim, and not tested but gives you an idea.

//SoundObserver.h

#include <AVFoundation/AVFoundation.h>

@interface SoundObserver {

    AVAudioPlayer *audioPlayer;
}
-(void)playSound;
+(SoundObserver*)sharedInstance;

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

@end


//SoundObserver.m

static SoundObserver *instance = nil;

@implementation SoundObserver


-(id)init {

    self = [super init];

    if(self) {

  //Get path to file.
  NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sound"
                                                       ofType:@"wav"];

  // filePath to URL
  NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];

  //Initialize the AVAudioPlayer.
  self.audioPlayer = [[AVAudioPlayer alloc]
                           initWithContentsOfURL:fileURL error:nil];

  // Preloads buffer and prepare for use.
  [self.audioPlayer prepareToPlay];

  [filePath release];
  [fileURL release];

    }

}

+(SoundObserver*)sharedInstance {

        @synchronized(self) {

        if (instance == nil)

            instance = [[self alloc] init];
    }

    return instance;
}


-(void)playSound {

    //make sure the audio resets to beginning each activation.

    [self.audioPlayer play];
}

-(void)dealloc {

    //Clean up
    [self.audioPlayer release];
    self.audioPlayer = nil;
    [super dealloc];
}



// User example.

In applicationDidFinishLaunching:

    [SoundObserver sharedInstance];

From here you can connect all buttons to the same function, or call it from anywhere in the App that #import's SoundObserver.h

-(IBOutlet)buttonClick:(id)sender {

    [[SoundObserver sharedInstance] playSound];
}
mark
  • 21
  • 1
  • thanks mark, i just seen how bad i asked this question, as i used my iPhone and the 6 to 8 app, ughh what a mess, but it looks like you got what i was getting at, I'm going to try this method and ill you know! – theProject May 18 '12 at 08:05