因为实例化AVAudioUnit的方法如下:
[AVAudioUnit instantiateWithComponentDescription:componentDescription options:0 completionHandler:^(__kindof AVAudioUnit * _Nullable audioUnit, NSError * _Nullable error) {
}];我应该如何对AVAudioUnit进行子类分类?我试过这样做:
[MySubclassOfAVAudioUnit instantiateWithComponentDescription:componentDescription options:0 completionHandler:^(__kindof AVAudioUnit * _Nullable audioUnit, NSError * _Nullable error) {
}];但是,在块中返回的audioUnit仍然是AVAudioUnit类型,而不是MySubclassOfAVAudioUnit类型。
根据Fistman的回答,我用苹果的示例代码注册了我的自定义AUAudioUnit子类:
componentDescription.componentType = kAudioUnitType_Effect;
componentDescription.componentSubType = 0x666c7472; /*'fltr'*/
componentDescription.componentManufacturer = 0x44656d6f; /*'Demo'*/
componentDescription.componentFlags = 0;
componentDescription.componentFlagsMask = 0;我希望我的AVAudioUnit子类总是使用我的AUAudioUnit。
发布于 2016-11-12 20:33:12
来自instantiateWithComponentDescription:completionHandler:
返回的AVAudioUnit实例通常是根据组件的类型选择的子类(AVAudioUnitEffect、AVAudioUnitGenerator、AVAudioUnitMIDIInstrument或AVAudioUnitTimeEffect)。
我搞错了--您不能实例化您自己的AVAudioUnit子类,只能实例化您的AUAudioUnit,包装在相关的内置AVFoundation AVAudioUnit子类中(例如AVAudioUnitEffect等)。
以下代码将导致实例化MyAUAudioUnit ( AUAudioUnit的子类):
#import <AVFoundation/AVFoundation.h>
@interface MyAUAudioUnit : AUAudioUnit {
}
@end
@implementation MyAUAudioUnit
// implement it here
@end
// later
- (void)instantiateMyAUAudioUnitWrappedInAVAudioUnit {
// register it (need only be done once)
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Effect;
desc.componentSubType = 0x666c7472; /*'fltr'*/
desc.componentManufacturer = 0x44656d6f; /*'Demo'*/
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
[AUAudioUnit registerSubclass:MyAUAudioUnit.class asComponentDescription:desc name:@"MyAU" version:1];
// Instantiate as many times as you like:
[AVAudioUnit instantiateWithComponentDescription:desc options:0 completionHandler:^(AVAudioUnit * audioUnit, NSError *error) {
NSLog(@"AVAudioUnit: %@, error: %@", audioUnit, error);
}];
}错位
因此,要实例化AVAudioUnit子类,首先必须使用AUAudioUnit方法注册它:
+[AUAudioUnit registerSubclass:asComponentDescription:name:version:]
在这个devforum线程中有一个代码片段和一些可能的问题。
https://stackoverflow.com/questions/40558532
复制相似问题