作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个单例
@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]
}
RDTRecord
RDTViewController
中的“记录”按钮调用它使用以下内容:
-(IBAction)recordButton:(UIButton *)sender
//somenumber is not initialized yet so plug any int in here
[[RDTRecord sharedRecorder] doRecordAudio:somenumber];
}
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)sharedRecorder successfully:(BOOL)flag {
...do some stuff here
}
RDTViewController
不在
RDTRecord
#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
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
audioRecorderDidFinishRecording
在我的
RDTViewController
到“看”的时候
*recorder
如果
*recorder
则结束正在上课
RDTRecord
?
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];
}
doRecordAudio:
的参数,并在回调运行后添加
nil
和
delegate
,具体取决于您的其他要求)。
关于ios - 如何在 ViewController 中获取委托(delegate)方法以查看 *recorder 何时完成(不同的类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22120896/
我是一名优秀的程序员,十分优秀!