轉自:http://blog.csdn.net/like7xiaoben/article/details/9001806
匯入架構:
代碼:
[cpp] view plain copy #import <UIKit/UIKit.h> #import <AudioToolbox/AudioToolbox.h> @interface MsgPlaySound : NSObject { SystemSoundID sound;//系統聲音的id 取值範圍為:1000-2000 } - (id)initSystemShake;//系統 震動 - (id)initSystemSoundWithName:(NSString *)soundName SoundType:(NSString *)soundType;//初始化系統聲音 - (void)play;//播放 @end
[cpp] view plain copy #import "MsgPlaySound.h" @implementation MsgPlaySound - (id)initSystemShake { self = [super init]; if (self) { sound = kSystemSoundID_Vibrate;//震動 } return self; } - (id)initSystemSoundWithName:(NSString *)soundName SoundType:(NSString *)soundType { self = [super init]; if (self) { NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",soundName,soundType]; //[[NSBundle bundleWithIdentifier:@"com.apple.UIKit" ]pathForResource:soundName ofType:soundType];//得到蘋果架構資源UIKit.framework ,從中取出所要播放的系統聲音的路徑 //[[NSBundle mainBundle] URLForResource: @"tap" withExtension: @"aif"]; 擷取自訂的聲音 if (path) { OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&sound); if (error != kAudioServicesNoError) {//擷取的聲音的時候,出現錯誤 sound = nil; } &