Question

I have a singleton

@implementation RDTRecord
@synthesize recorder;
+(RDTRecord *)sharedRecorder
{
static RDTRecord* sharedRecorder;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    sharedRecorder = [[RDTRecord alloc]init];
});
return sharedRecorder
}

and a method:

- (void)doRecordAudio:(int)increment{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:nil];
... file location etc.
 recorder = [[AVAudioRecorder alloc]initWithURL:outputFileURL settings:settings               error:&error];
if (recorder) {
    [recorder prepareToRecord];

    [recorder recordForDuration:(NSTimeInterval) 3.0]
}

in a class RDTRecord

I call this from a "record" button in RDTViewController using the following:

-(IBAction)recordButton:(UIButton *)sender

        //somenumber is not initialized yet so plug any int in here
        [[RDTRecord sharedRecorder] doRecordAudio:somenumber]; 
}

I want to implement the delegate...

-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag {
...do some stuff here
}

in my RDTViewController not in RDTRecord

the interface for my vc is :

#import <UIKit/UIKit.h>
#import "RDTRecord.h"

@interface RDTViewController : UIViewController <AVAudioRecorderDelegate>;

@property (weak, nonatomic) IBOutlet UIButton *recordButton;

- (IBAction)recordButton:(UIButton *)sender;
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag;
@end

.h for RDTRecord is as follows:

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import "RDTViewController.h"

@interface RDTRecord : NSObject {
    AVAudioRecorder *recorder;
}

@property (nonatomic,strong) AVAudioRecorder *recorder;

+(RDTRecord *)sharedRecorder;
- (void)doRecordAudio:(int)increment;
@end

The question is: How can I get the delegate method audioRecorderDidFinishRecording in my RDTViewController to "see" when the *recorder is finished if the *recorder is in class RDTRecord? Or perhaps another way to ask the same question would be: how do I let the AVAudioRecorder *recorder object "know" to use the delegate if the delegate is in another class - such as RDTViewController and what would that code look like?

Was it helpful?

Solution

Make RDTRecord the delegate of the audio recorder and have it implement audioRecorderDidFinishRecording:successfully:. But, also give it a property @property (assign) id <AVAudioRecorderDelegate> delegate; and, when your view controller wants to trigger a recording have it set itself as the delegate of RDTRecord.

Now, when audioRecorderDidFinishRecording:successfully: is called in RDTRecord it can forward the callback to its delegate:

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag
{
    [self.delegate audioRecorderDidFinishRecording:sharedRecorder successfully:flag];
}

(you could also add the delegate as a parameter of doRecordAudio:, and nil the delegate after the callback has run depending on your other requirements).

The view controller should remove itself as a delegate when it is done (and before it is destroyed).

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