- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
NSURL *url = [[NSBundle mainBundle]URLForResource:@"buyao.caf" withExtension:nil];
SystemSoundID soundID;
//创建soundID soundI'D是Int类型
AudioServicesCreateSystemSoundID(CFBridgingRetain(url), &soundID);
//这个是主要的播放的方法
AudioServicesPlaySystemSound(soundID);
}
以上代码,每次都会创建soundID,这样消耗资源,优化:
- 将ID定义成熟性进行懒加载
if (!_soundID) {
NSURL *url = [[NSBundle mainBundle]URLForResource:@"buyao.caf" withExtension:nil];
SystemSoundID soundID;
//创建soundID soundI'D是Int类型
AudioServicesCreateSystemSoundID(CFBridgingRetain(url), & soundID);
}
- 这样就会保证只有一个
网友评论