AudioServicesPlaySystemSoundで音を鳴らす
ここでは"se.caf"というファイルをプロジェクトに追加しておいて、そのファイルを決め打ちでならすことを想定しています。
PlaySound.h
#import <UIKit/UIKit.h> #import <AudioToolbox/AudioServices.h> @interface PlaySound : NSObject { NSURL *url; SystemSoundID soundID; } - (void)play; @end
PlaySound.m
SoundIDの定義域がわからなかったのでkSystemSoundID_Errorの定義の仕方がいまいち。
#import "PlaySound.h" #define kSystemSoundID_Error kSystemSoundID_Vibrate @implementation PlaySound - (void)createSound { if(kAudioServicesNoError != AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID)){ soundID = kSystemSoundID_Error; } } - (void)createURL:(NSString*)path { url = [NSURL fileURLWithPath:path isDirectory:NO]; } - (id)init { [super init]; NSBundle *mainBundle = [NSBundle mainBundle]; [self createURL:[mainBundle pathForResource:@"se" ofType:@"caf"]]; [self createSound]; return self; } -(void)dealloc { AudioServicesDisposeSystemSoundID(soundID); [super dealloc]; } -(void)play { if(soundID == kSystemSoundID_Error){ return; } AudioServicesPlaySystemSound(soundID); } @end