Attention! Translated article might be found on my English blog.

2017年10月10日火曜日

自作のV3 AudioUnitのビューが作成できなかった

NOTEEnglish article is available.

こちらの記事の通り、自作AUのAVAudioUnitインスタンスは作れたものの、
今度はビューの作成に失敗しているようでした。

-[AUAudioUnit requestViewControllerWithCompletionHandler:]をコールしてもcompletionHandler内のviewControllerがnilになってしまいます。

AudioUnitV3Exampleの-[ViewController embedPlugInView]の実装をパクリ参考にし、
バンドルから直接ビューを作るようにしたところ、
無事ビューを作成することができました。

自作AUのembedPlugInViewの実装例は以下の通りです。
- (void)embedPlugInView {
    NSURL *builtInPlugInURL = [[NSBundle mainBundle] builtInPlugInsURL];
    NSURL *pluginURL = [builtInPlugInURL URLByAppendingPathComponent: @"SYVibrato.appex"];
    NSBundle *appExtensionBundle = [NSBundle bundleWithURL: pluginURL];
    
    self.vibratoViewController = [[AudioUnitViewController alloc] initWithNibName:@"AudioUnitViewController"
                                                                           bundle:appExtensionBundle];
    
    [self.vibratoProgressIndicator stopAnimation:self];
    NSRect r = self.vibratoView.frame;
    [self.vibratoViewController view].frame = NSMakeRect(0, 0, NSWidth(r), NSHeight(r));
    [self.vibratoView addSubview:self.vibratoViewController.view];
    
    [self.vibratoViewController connectAU:self.vibratoUnit.AUAudioUnit];
}
なお、 自作AUがサードパーティ製ホストアプリで認識できるようになった後であれば、
従来の方法requestViewControllerWithCompletionHandler:でもビューを作成できました。

10.13では内包アプリを起動してもすぐにはサードパーティ製ホストアプリで認識できないため、
このような修正が必要になってしまったということかもしれません。

というか、最初からAudioUnitV3Exampleの実装を真似しましょうという話ですね。