2013-09-26 35 views
3

除了蘋果的Load Preset Demo示例代碼中包含的其他一些東西外,現在不鼓勵對CFURLCreateDataAndPropertiesFromResource的調用。但是我無法找到替代品 - 既沒有選擇權,也沒有看到參考文獻告訴我,它不再是完成的事情。不建議使用CFURLCreateDataAndPropertiesFromResource。並尋找替代品

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; 

回答

4

有關屬性:CFURLCopyResourcePropertiesForKeys示例屬性:kCFURLFileSizeKeykCFURLContentModificationDateKey,或基金會風格[NSURL resourceValuesForKeys:error:]

對於這些數據:+[NSData dataWithContentsOfURL:options:error:]

他們沒有被記錄爲替代品,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 
} 
+0

我試圖轉換有問題的代碼(請參閱編輯的文章),但我在這裏陌生的地方,我不確定如何使用dataWithContentsOfURL:options:錯誤代替返回布爾值的代碼。 – HenryRootTwo

+0

@stugrimshaw看到更新的答案 – justin

+0

我開始獲得照片,這對我有很大的幫助。所以我猜,CFPropertyListCreateWithData現在必須替換爲使用NSData對象的東西?由於propertyResourceData仍然爲0,此時NSAssert失敗。 – HenryRootTwo

0

我發現我可以只用除去甚至更多的代碼如下:

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; 
0

我結束了使用此代碼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;