[英]How can I get a delegate method in ViewController to see when *recorder is finished (different class)
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
在RDTRecord
I call this from a "record" button in RDTViewController
using the following: 我使用以下命令从RDTViewController
的“记录”按钮调用它:
-(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
在我的RDTViewController
不在 RDTRecord
the interface for my vc is : 我的vc的界面是:
#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: .h for RDTRecord
如下:
#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
? 问题是:如果*recorder
在RDTRecord
类中,我RDTViewController
才能在我的RDTViewController
获取委托方法audioRecorderDidFinishRecording
以“看到” *recorder
何时完成? 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? 或者也许另一种问同样问题的方法是:如果委托在另一个类中,如何让AVAudioRecorder *记录器对象“知道”使用委托 - 例如RDTViewController
以及该代码会是什么样的?
Make RDTRecord
the delegate of the audio recorder and have it implement audioRecorderDidFinishRecording:successfully:
. 让RDTRecord
成为录音机的代表,让它实现audioRecorderDidFinishRecording:successfully:
. But, also give it a property @property (assign) id <AVAudioRecorderDelegate> delegate;
但是,也给它一个属性@property (assign) id <AVAudioRecorderDelegate> delegate;
and, when your view controller wants to trigger a recording have it set itself as the delegate of RDTRecord
. 并且,当您的视图控制器想要触发录制时,它将自己设置为RDTRecord
的委托。
Now, when audioRecorderDidFinishRecording:successfully:
is called in RDTRecord
it can forward the callback to its delegate: 现在,当audioRecorderDidFinishRecording:successfully:
被称为RDTRecord
可以转发回调其委托:
- (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). (你也可以添加代理作为一个参数doRecordAudio:
和nil
的delegate
回调已根据您的其他要求运行后)。
The view controller should remove itself as a delegate when it is done (and before it is destroyed). 视图控制器应该在完成后(在它被销毁之前)作为委托删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.