Question

I'm creating an iOS application that starts recording audio when a prompt occurs rather than when a button is pressed. Because of this, I've put the AVAudioRecorder in a scheduler object that is called from it's own class. However, when I assign the scheduler as the delegate for the recorder, I get the warning Assigning to 'id<AVAudioRecorderDelegate>' from incompatible type 'LWPScheduler *__strong'. Here is the implementation for the scheduler:

@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

My question is whether I have to migrate the audio handling to the controller for the view the recorder would be contained in. Thanks for your help!

Was it helpful?

Solution

On your interface (or private class extension) tell the compiler that your are conforming to the protocol the delegate expects. For e.g.:

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

The protocol defines required and/or optional methods you may have to implement (Xcode will warn you about the required one). After telling the interface the class confirms to the protocol, _recorder.delegate = self; will just work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top