gpt4 book ai didi

ios - 将 AVAudioRecorderDelegate 分配给非 UIViewController

转载 作者:行者123 更新时间:2023-11-29 03:14:07 25 4
gpt4 key购买 nike

我正在创建一个 iOS 应用程序,它在出现提示时而不是在按下按钮时开始录制音频。因此,我将 AVAudioRecorder 放在一个从它自己的类调用的调度程序对象中。但是,当我将 调度程序指定为记录器的委托(delegate) 时,我收到警告 Assigning to 'id<AVAudioRecorderDelegate>' from incompatible type 'LWPScheduler *__strong' .这是调度程序的实现:

@implementation LWPScheduler
@synthesize tempo;
@synthesize userName;

+(LWPScheduler *)masterScheduler{
static LWPScheduler *masterScheduler = nil;
if (masterScheduler == nil)
{
masterScheduler = [[self alloc] init];
}
return masterScheduler;
}

- (id)init
{
self = [super init];
if (self) {
self.tempo = 120;
self.userName = @"Tim Burland";

//recording init
// Set the audio file
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
@"MyAudioMemo.m4a",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];

//audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

//recorder settings
NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

// Initiate and prepare the recorder
_recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
_recorder.delegate = self; //Incompatible Type Warning Here
_recorder.meteringEnabled = YES;
[_recorder prepareToRecord];
}
return self;
}

@end

我的问题是我是否必须将音频处理迁移到 Controller 以用于包含录音机的 View 。感谢您的帮助!

最佳答案

在您的接口(interface)(或私有(private)类扩展)上告诉编译器您符合委托(delegate)期望的协议(protocol)。例如:

@interface LWPScheduler : NSObject <AVAudioRecorderDelegate>
// ...
@end

该协议(protocol)定义了您可能必须实现的必需和/或可选方法(Xcode 会警告您需要的方法)。在告诉接口(interface)类确认协议(protocol)后,_recorder.delegate = self; 将正常工作。

关于ios - 将 AVAudioRecorderDelegate 分配给非 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21869274/

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