#import <AudioToolbox/AudioToolbox.h> //先import AudioToolbox後才能使用這個功能
SystemSoundID magicSound; //宣告一個新的聲音叫magicSound(自已隨便取)是屬於SystemSoundID型式
-(void)playMagicSound; //宣告播放magicSound的功能
在.m的實作部份
//在init的時侯先設定magicSound要播放音效檔
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//下面紅字這一段是設定magicSound
NSURL *magicUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"magic-chime" ofType:@"wav"]];
OSStatus magicError = AudioServicesCreateSystemSoundID((__bridge CFURLRef)magicUrl, &magicSound);
if(magicError){
NSLog(@"error:%ld",magicError);
}
}
return self;
}
//實作playMagicSound
-(void)playMagicSound{
AudioServicesPlaySystemSound(magicSound);
}
//播放的使用方法
[self playMagicSound];
0 意見:
張貼留言