将AVAudioRecorderDelegate分配给非UIViewController

时间:2014-02-19 01:41:31

标签: ios objective-c avfoundation avaudiosession

我正在创建一个iOS应用程序,它会在出现提示时开始录制音频,而不是按下按钮时。因此,我将AVAudioRecorder放在一个从它自己的类调用的调度程序对象中。但是,当我将调度程序指定为录像机的代理时,我会收到警告“分配给'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

我的问题是我是否必须将音频处理迁移到控制器以便包含录像机。感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

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

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

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