如何使用NsData初始化系统声音服务
NSString *pewPewPath = [[NSBundle mainBundle]
pathForResource:@"pew-pew-lei" ofType:@"caf"];
NSURL *pewPewURL = [NSURL fileURLWithPath:pewPewPath];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &self.pewPewSound);
AudioServicesPlaySystemSound(self.pewPewSound);
你需要在你的代码中做一个LOT的错误检查。
NSString *pewPewPath = [[NSBundle mainBundle] pathForResource:@"pew-pew-lei" ofType:@"caf"];
if(pewPewPath)
{
CFURLRef pewPewURL = (CFURLRef)[NSURL fileURLWithPath:pewPewPath];
if(pewPewURL)
{
OSStatus errorCode = AudioServicesCreateSystemSoundID(pewPewURL, &self.pewPewSound);
if(errorCode)
{
NSLog(@"error in creating the system sound - %ld", errorCode);
} else {
AudioServicesPlaySystemSound(self.pewPewSound);
}
} else {
NSLog(@"couldn't make pew-pew-lei into a URL");
}
} else {
NSLog( @"no pew-pew-lei.caf file found");
}