Skip to content

Instantly share code, notes, and snippets.

@zoul
Created October 9, 2009 08:09
Show Gist options
  • Save zoul/205857 to your computer and use it in GitHub Desktop.
Save zoul/205857 to your computer and use it in GitHub Desktop.
Simple wrapper around Audio Services
/*
- Trivial wrapper around system sound as provided by Audio Services.
- Don’t forget to link against the Audio Toolbox framework.
- Assumes ARC support.
*/
@interface Sound : NSObject
// Path is relative to the resources dir.
- (id) initWithPath: (NSString*) path;
- (void) play;
@end
#import "Sound.h"
#import <AudioToolbox/AudioToolbox.h>
@implementation Sound {
SystemSoundID handle;
}
- (id) initWithPath: (NSString*) path
{
self = [super init];
NSString *const resourceDir = [[NSBundle mainBundle] resourcePath];
NSString *const fullPath = [resourceDir stringByAppendingPathComponent:path];
NSURL *const url = [NSURL fileURLWithPath:fullPath];
OSStatus errcode = AudioServicesCreateSystemSoundID((CFURLRef) url, &handle);
NSAssert1(errcode == 0, @"Failed to load sound: %@", path);
return self;
}
- (void) dealloc
{
AudioServicesDisposeSystemSoundID(handle);
}
- (void) play
{
AudioServicesPlaySystemSound(handle);
}
@end
@zoul
Copy link
Author

zoul commented Jul 10, 2012

Thanks, I missed that. I’ve added the missing #import to the code along with some minor tweaks for ARC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment