gpt4 book ai didi

ios - 如何在 ViewController 中获取委托(delegate)方法以查看 *recorder 何时完成(不同的类)

转载 作者:行者123 更新时间:2023-12-01 19:02:07 29 4
gpt4 key购买 nike

我有一个单例

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

和一个方法:
- (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]
}

在一个类(class) RDTRecord
我从 RDTViewController 中的“记录”按钮调用它使用以下内容:
-(IBAction)recordButton:(UIButton *)sender

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

我想实现委托(delegate)...
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag {
...do some stuff here
}

在我的 RDTViewController不在 RDTRecord
我的 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 为 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

问题是:我怎样才能得到委托(delegate)方法 audioRecorderDidFinishRecording在我的 RDTViewController到“看”的时候 *recorder如果 *recorder 则结束正在上课 RDTRecord ?
或者,问同样问题的另一种方式可能是:如果委托(delegate)在另一个类中,我如何让 AVAudioRecorder *recorder 对象“知道”使用委托(delegate) - 例如 RDTViewController那个代码会是什么样子?

最佳答案

制作 RDTRecord录音机的委托(delegate)并让它实现audioRecorderDidFinishRecording:successfully: .但是,也给它一个属性@property (assign) id <AVAudioRecorderDelegate> delegate;并且,当您的 View Controller 想要触发录制时,请将其设置为 RDTRecord 的委托(delegate).

现在,当 audioRecorderDidFinishRecording:successfully:RDTRecord 中调用它可以将回调转发给它的委托(delegate):

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

(您也可以将委托(delegate)添加为 doRecordAudio: 的参数,并在回调运行后添加 nildelegate,具体取决于您的其他要求)。

View Controller 应在完成后(以及在销毁之前)将自己作为委托(delegate)移除。

关于ios - 如何在 ViewController 中获取委托(delegate)方法以查看 *recorder 何时完成(不同的类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22120896/

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