首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何子类AVAudioUnit?

如何子类AVAudioUnit?
EN

Stack Overflow用户
提问于 2016-11-12 01:09:11
回答 1查看 1.3K关注 0票数 4

因为实例化AVAudioUnit的方法如下:

代码语言:javascript
复制
[AVAudioUnit instantiateWithComponentDescription:componentDescription options:0 completionHandler:^(__kindof AVAudioUnit * _Nullable audioUnit, NSError * _Nullable error) {
    }];

我应该如何对AVAudioUnit进行子类分类?我试过这样做:

代码语言:javascript
复制
[MySubclassOfAVAudioUnit instantiateWithComponentDescription:componentDescription options:0 completionHandler:^(__kindof AVAudioUnit * _Nullable audioUnit, NSError * _Nullable error) {
    }];

但是,在块中返回的audioUnit仍然是AVAudioUnit类型,而不是MySubclassOfAVAudioUnit类型。

根据Fistman的回答,我用苹果的示例代码注册了我的自定义AUAudioUnit子类:

代码语言:javascript
复制
componentDescription.componentType = kAudioUnitType_Effect;
componentDescription.componentSubType = 0x666c7472; /*'fltr'*/
componentDescription.componentManufacturer = 0x44656d6f; /*'Demo'*/
componentDescription.componentFlags = 0;
componentDescription.componentFlagsMask = 0;

我希望我的AVAudioUnit子类总是使用我的AUAudioUnit

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-12 20:33:12

来自instantiateWithComponentDescription:completionHandler:

返回的AVAudioUnit实例通常是根据组件的类型选择的子类(AVAudioUnitEffect、AVAudioUnitGenerator、AVAudioUnitMIDIInstrument或AVAudioUnitTimeEffect)。

我搞错了--您不能实例化您自己的AVAudioUnit子类,只能实例化您的AUAudioUnit,包装在相关的内置AVFoundation AVAudioUnit子类中(例如AVAudioUnitEffect等)。

以下代码将导致实例化MyAUAudioUnit ( AUAudioUnit的子类):

代码语言:javascript
复制
#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线程中有一个代码片段和一些可能的问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40558532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档