7

Does anyone have a snippet that uses the AudioToolBox framework that can be used to play a short sound? I would be grateful if you shared it with me and the rest of the community. Everywhere else I have looked doesn't seem to be too clear with their code.

Thanks!

esqew
  • 42,425
  • 27
  • 92
  • 132
  • Is there something in particular you're looking for? The AV Foundation framework is the easiest way to play sounds, but I assume you want something more? – Nathan S. Mar 20 '10 at 16:43
  • Nah, Id just like to know the standard way of playing a short sound... many people around the 'net use AudioToolBox... thanks for the response :) – esqew Mar 20 '10 at 16:56

3 Answers3

11

Here is an easy example using the AVAudioPlayer:

-(void)PlayClick
{
    NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] 
                                               pathForResource:@"click"
                                               ofType:@"caf"]];
    AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
    [click play];
    [click release];
}

This assumes a file called "click.caf" available in the main bundle. As I play this sound a lot, I actually keep it around to play it later instead of releasing it.

Nathan S.
  • 5,244
  • 3
  • 45
  • 55
  • @VineeshTP You probably need to change the name/type to be the same as the file you're wanting to play. But, I suggest asking a new question and detailing exactly why it doesn't work for you. – Nathan S. Jul 17 '12 at 15:53
  • @NathanS.: I changed the file file name. – Vineesh TP Jul 18 '12 at 03:32
  • Player should be released right after finish playing. In this example it will be killed before you can hear a melody. Keep reference and clean it in delegate method (AVAudioPlayerDelegate) – MikeR May 30 '13 at 18:09
  • MikeR you are wrong, releasing will only reduce the referece count by 1. Therefor when finish playing the AVAudioPlayer object will reduce the last referece count and the memory will be dealloc by then. Of course you can always use ARC and forget about this. – user2387149 Feb 19 '14 at 17:40
7

I wrote a simple Objective-C wrapper around AudioServicesPlaySystemSound and friends:

#import <AudioToolbox/AudioToolbox.h>

/*
    Trivial wrapper around system sound as provided
    by Audio Services. Don’t forget to add the Audio
    Toolbox framework.
*/

@interface Sound : NSObject
{
    SystemSoundID handle;
}

// Path is relative to the resources dir.
- (id) initWithPath: (NSString*) path;
- (void) play;

@end

@implementation Sound

- (id) initWithPath: (NSString*) path
{
    [super init];
    NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
    NSString *fullPath = [resourceDir stringByAppendingPathComponent:path];
    NSURL *url = [NSURL fileURLWithPath:fullPath];

    OSStatus errcode = AudioServicesCreateSystemSoundID((CFURLRef) url, &handle);
    NSAssert1(errcode == 0, @"Failed to load sound: %@", path);
    return self;
}

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

- (void) play
{
    AudioServicesPlaySystemSound(handle);
}

@end

Lives here. For other sound options see this question.

Community
  • 1
  • 1
zoul
  • 102,279
  • 44
  • 260
  • 354
  • 1
    I suggest people use @zoul's solution for system sounds (button clicks etc). It supports less formats, but it's supposed to lag less and it's the preferred documented solution to be used for system sounds. AVAudioPlayer is great for playing music, on the other hand - but that's not what the original poster asked. :-) – Ivan Vučica Aug 09 '12 at 15:07
2

Source: AudioServices - iPhone Developer Wiki

The AudioService sounds are irrespective of the the device volume controller. Even if the phone is in silent mode the Audio service sounds will be played. These sounds can only be muted by going Settings -> Sounds -> Ringer and Alerts.

Custom system sounds can be played by the following code:

CFBundleRef mainbundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainbundle, CFSTR("tap"), CFSTR("aif"), NULL);
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject);

Invoking the vibration in iPhone

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Playing inbuilt system sounds.

AudioServicesPlaySystemSound(1100);
esqew
  • 42,425
  • 27
  • 92
  • 132
Govind
  • 2,337
  • 33
  • 43