1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #import "ViewController.h" #import <AVFoundation/AVFoundation.h>
@interface ViewController () @property(nonatomic,assign) SystemSoundID soundID; @end
@implementation ViewController
-(SystemSoundID)soundID { if (!_soundID) { //1-创建URL NSURL *url=[[NSBundle mainBundle]URLForResource:@"raw/buyao.aac" withExtension:nil]; //2-创建音效ID AudioServicesCreateSystemSoundID(CFBridgingRetain(url), &_soundID); } return _soundID; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //3-播放音效 AudioServicesPlayAlertSound(self.soundID); } @end
|