不build议使用CFURLCreateDataAndPropertiesFromResource。 并寻找替代品

除了Apple的Load Preset Demo示例代码中包含的其他一些东西外,现在不build议使用CFURLCreateDataAndPropertiesFromResource。 但是我找不到替代品 – 既没有select权,也没有看到参考文献告诉我,它不再是已经完成的事情。

CFDataRef propertyResourceData = 0; Boolean status; SInt32 errorCode = 0; OSStatus result = noErr; // Read from the URL and convert into a CFData chunk status = CFURLCreateDataAndPropertiesFromResource ( kCFAllocatorDefault, (__bridge CFURLRef) presetURL, &propertyResourceData, NULL, NULL, &errorCode ); NSAssert (status == YES && propertyResourceData != 0, @"Unable to create data and properties from a preset. Error code: %d '%.4s'", (int) errorCode, (const char *)&errorCode); // Convert the data object into a property list CFPropertyListRef presetPropertyList = 0; CFPropertyListFormat dataFormat = 0; CFErrorRef errorRef = 0; presetPropertyList = CFPropertyListCreateWithData ( kCFAllocatorDefault, propertyResourceData, kCFPropertyListImmutable, &dataFormat, &errorRef ); // Set the class info property for the Sampler unit using the property list as the value. if (presetPropertyList != 0) { result = AudioUnitSetProperty( self.samplerUnit, kAudioUnitProperty_ClassInfo, kAudioUnitScope_Global, 0, &presetPropertyList, sizeof(CFPropertyListRef) ); CFRelease(presetPropertyList); } if (errorRef) CFRelease(errorRef); CFRelease (propertyResourceData); return result; 

对于属性: CFURLCopyResourcePropertiesForKeys示例属性: kCFURLFileSizeKeykCFURLContentModificationDateKey ,或者使用[NSURL resourceValuesForKeys:error:] Foundation风格。

对于数据: +[NSData dataWithContentsOfURL:options:error:]

他们没有被logging为替代品,AFAIK。 大多数这些新的替代API已经存在了几年。

编辑

在这个例子中你发布了编辑,程序没有请求属性,所以你只需要在URL presetURL的数据。

你可以通过以下方式来实现

 NSURL * presetURL = ...; // do review these options for your needs. you can make great // optimizations if you use memory mapping or avoid unnecessary caching. const NSDataReadingOptions DataReadingOptions = 0; NSError * outError = nil; NSData * data = [NSData dataWithContentsOfURL:presetURL options:DataReadingOptions error:&outError]; const bool status = nil != data; // << your `status` variable if (!status) { // oops - an error was encountered getting the data see `outError` } else { // use the data } 

我发现我可以删除更多的代码,只需使用以下内容:

 OSStatus result = noErr; NSData* data = [NSData dataWithContentsOfURL:presetURL]; id propertyList = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL]; // Set the class info property for the Sampler unit using the property list as the value. if (propertyList) { result = AudioUnitSetProperty( self.samplerUnit, kAudioUnitProperty_ClassInfo, kAudioUnitScope_Global, 0, (__bridge CFPropertyListRef)propertyList, sizeof(CFPropertyListRef) ); } return result; 

我结束了使用这个代码https://developer.apple.com/library/mac/technotes/tn2283/_index.html#//apple_ref/doc/uid/DTS40011217-CH1-TNTAG2

 - (OSStatus) loadSynthFromPresetURL: (NSURL *) presetURL { OSStatus result = noErr; AUSamplerInstrumentData auPreset = {0}; auPreset.fileURL = (__bridge CFURLRef) presetURL; auPreset.instrumentType = kInstrumentType_AUPreset; result = AudioUnitSetProperty(self.samplerUnit, kAUSamplerProperty_LoadInstrument, kAudioUnitScope_Global, 0, &auPreset, sizeof(auPreset)); return result; 
Interesting Posts