Created
October 9, 2009 08:09
-
-
Save zoul/205857 to your computer and use it in GitHub Desktop.
Simple wrapper around Audio Services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, I missed that. I’ve added the missing
#import
to the code along with some minor tweaks for ARC.