我根据帖子的标题查找了搜索词,但是..
我正在使用AVFoundation构建一个iPhone应用程序。
是否有一个正确的程序来限制将要录制的音频数量?我想最多10秒钟。
感谢您的帮助/建议/提示/提示。
发布于 2011-04-18 12:11:10
AVAudioRecorder有以下方法:
- (BOOL)recordForDuration:(NSTimeInterval)duration我想这样就行了!
http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioRecorder_ClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008238
发布于 2011-04-18 06:04:33
我通常不使用AVFoundation,所以我不知道确切的方法/类名称(我填写了自己的名称),但解决这个问题的一个办法是在最初开始录制时使用一个循环的NSTimer。如下所示:
@interface blahblah
...
int rec_time;
NSTimer *timer;
Recorder *recorder;
...
@end
@implementation blahblah
...
-(void)beginRecording {
[recorder startRecording];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(recordingTime)
userInfo:nil
repeats:YES];
}
-(int)recordingTime {
if (rec_time >= 10) {
[recorder endRecording];
[timer invalidate];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You recorded for too long!"...;
return;
}
rec_time = rec_time + 1;
}
...
@end发布于 2015-07-13 04:21:20
这是iOS编程指南中的一个例子,我发现它非常有用和简单。开始录制后,调用延迟为10秒的停止函数,停止录制时会自动调用委托方法audioRecorderDidFinishRecording:successfully。
@implementation ViewController{
AVAudioRecorder *recorder;
AVAudioPlayer *player;
}
- (IBAction)recordPauseTapped:(id)sender {
// Stop the audio player before recording
if (player.playing) {
[player stop];
}
if (!recorder.recording) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
// Start recording
[recorder record];
[recordPauseButton setBackgroundImage:recordingImage forState:UIControlStateNormal];
[self performSelector:@selector(stopRecording)
withObject:self afterDelay:10.0f];
} else {
[recorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
}
}
- (void)stopRecording {
[recorder stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
}
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *)avrecorder successfully:(BOOL)flag{
NSLog(@"after 10 sec");
}https://stackoverflow.com/questions/5695523
复制相似问题