【问题标题】:How to get the NSView object from Audio Unit如何从音频单元获取 NSView 对象
【发布时间】:2020-11-29 08:43:39
【问题描述】:

我可能在这里做了一些非常愚蠢的事情,但这让我陷入了困境。 我正在尝试显示音频单元的视图(如果有的话)。我的理解是 AU 工厂类应该返回一个 NSView(我不担心某些 v3 单元返回 ViewController 的选项)。我有这个方法:

func getAUcocoaView(_ audioUnit: AudioUnit ) -> NSView {
    
    var dataSize = UInt32(MemoryLayout<AudioUnitCocoaViewInfo>.size)
    var cocoaViewInfoRawPtr = UnsafeMutableRawPointer.allocate(byteCount: MemoryLayout<AudioUnitCocoaViewInfo>.size, alignment: MemoryLayout<AudioUnitCocoaViewInfo>.alignment)

    defer {
        cocoaViewInfoRawPtr.deallocate()
    }

    let result = AudioUnitGetProperty(audioUnit, kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global, 0, cocoaViewInfoRawPtr, &dataSize)
    if result != 0 {
        print("Couldn't get AudioUnit property: kAudioUnitProperty_CocoaUI")
    }
    let cocoaViewInfo = cocoaViewInfoRawPtr.bindMemory(to: AudioUnitCocoaViewInfo.self, capacity: 1).pointee
    
    let factoryClassNameCF = cocoaViewInfo.mCocoaAUViewClass
    let factoryClassName = factoryClassNameCF.takeUnretainedValue() as String
    let cocoaViewBundlePathCF = cocoaViewInfo.mCocoaAUViewBundleLocation
    let cocoaViewBundlePath = cocoaViewBundlePathCF.takeUnretainedValue()

    let bundle = Bundle(url: cocoaViewBundlePath as URL)
    let factoryClass = bundle!.classNamed(factoryClassName) as! NSObject.Type
    let auView = factoryClass.init()
    return auView as! NSView
}

在最终尝试强制转换为 NSView 之前,一切似乎都按预期工作。我的理解是 AU 工厂类返回一个 NSView ,但是这种强制转换的尝试导致: “无法将 'JUCE_AUCocoaViewClass_ab7d1f643f11dc89' (0x600000e5a730) 类型的值转换为 'NSView' (0x7fff8ceab778)。” 我可能错过了一些非常愚蠢的东西,但要么: a)如何从中获得 NSView ?或者; b) 如果不是这样,如何调用 AU 视图?

非常感谢任何帮助。

【问题讨论】:

    标签: audiounit


    【解决方案1】:

    好吧,似乎根本不可能在 Swift 中调用必要的方法。我不知道为什么我花了这么长时间把头撞在墙上试图这样做,而在 Objective C 中这很容易。 所以我只是拿了包和类名等等瞧:

    NSView * getAUCocoaView(AudioUnit mAudioUnit, NSSize size, NSBundle *viewBundle, NSString * viewClassName) { 类 viewClass = [viewBundle classNamed: viewClassName];

    id<AUCocoaUIBase> factory = [[viewClass alloc] init];
    NSView * theView = [factory uiViewForAudioUnit: mAudioUnit withSize: size];
    return theView;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多