gpt4 book ai didi

快速回调函数

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:34 24 4
gpt4 key购买 nike

我正在尝试使用 The Amazing Audio Engine在 OS X 上使用 Swift 录制一些音频。为此,我需要实现一个回调函数来接收音频并对其进行处理。该文档有一些 examples关于如何使用 Objective-C 做到这一点:

@interface MyAudioReceiver : NSObject <AEAudioReceiver>
@end
@implementation MyAudioReceiver
static void receiverCallback(__unsafe_unretained MyAudioReceiver *THIS,
__unsafe_unretained AEAudioController *audioController,
void *source,
const AudioTimeStamp *time,
UInt32 frames,
AudioBufferList *audio) {

// Do something with 'audio'
}
-(AEAudioReceiverCallback)receiverCallback {
return receiverCallback;
}
@end
...
id<AEAudioReceiver> receiver = [[MyAudioReceiver alloc] init];

id<AEAudioReceiver> receiver = [AEBlockAudioReceiver audioReceiverWithBlock:
^(void *source,
const AudioTimeStamp *time,
UInt32 frames,
AudioBufferList *audio) {
// Do something with 'audio'
}];

据我所知:

var audioController: AEAudioController? = nil
audioController = AEAudioController(audioDescription: AEAudioStreamBasicDescriptionInterleaved16BitStereo, inputEnabled: true)
do {
try audioController?.start()
} catch {
NSLog("An error happened while starting AEAudioController.")
}

let receiver = MyAudioReceiver();
audioController?.addInputReceiver(receiver)

class MyAudioReceiver : NSObject, AEAudioReceiver {
var receiverCallback: AEAudioReceiverCallback! {
// what do I do here?
}
}

现在我在 receiverCallback 属性中遇到错误。我是在正确的轨道上还是我的方法完全错误?

我不知道如何在 Swift 中做完全相同的事情。我该怎么做?

最佳答案

在 Swift 中,函数和 block 几乎是平等对待的,统一的概念是 closures .我建议您阅读(连同文档的其余部分)以了解闭包语法和语义。

关于快速回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33844684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com