Playing sounds in iPhone SDK?

24,852

Solution 1

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.

Solution 2

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.

Solution 3

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);
Share:
24,852
esqew
Author by

esqew

In my current role at EY, I assist some of the world's most prominent organizations in realizing the incredible power of enterprise-grade intelligent automation by building scalable, effective, and efficient strategies, operating models, and governance models for robotic process automation, machine learning, natural language processing, and optical character recognition technologies. I've also assisted on numerous initiatives within these same organizations to build, test, and deploy business process-specific applications of these technologies. Note to visitors from closed questions I believe strongly in the mission of Stack Overflow as a useful knowledge repository. As such, I volunteer my time and energy to maintaining the site's content quality in accordance with our guidelines. If I've left a comment on your post or voted to close your question, it's not a personal affront - it's always to ensure that this site remains as a helpful source of information for everyone. Content disclaimer My contributions to Stack Exchange are solely my own; any opinions that may appear in my contributions do not necessarily reflect the views of any of my current or former employers.

Updated on July 26, 2022

Comments

  • esqew
    esqew almost 2 years

    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!