- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
NSURL *url = [[NSBundle mainBundle]URLForResource:@"buyao.caf" withExtension:nil];
SystemSoundID soundID;
//創(chuàng)建soundID soundI'D是Int類型
AudioServicesCreateSystemSoundID(CFBridgingRetain(url), &soundID);
//這個(gè)是主要的播放的方法
AudioServicesPlaySystemSound(soundID);
}
以上代碼,每次都會(huì)創(chuàng)建soundID,這樣消耗資源,優(yōu)化:
? ? ? - 將ID定義成熟性進(jìn)行懶加載
if (!_soundID) {
NSURL *url = [[NSBundle mainBundle]URLForResource:@"buyao.caf" withExtension:nil];
SystemSoundID soundID;
//創(chuàng)建soundID soundI'D是Int類型
AudioServicesCreateSystemSoundID(CFBridgingRetain(url), & soundID);
}
- 這樣就會(huì)保證只有一個(gè)