录制音频剪辑时设置时间限制?
我查找了帖子标题的搜索条件,但唉..录制音频剪辑时设置时间限制?
我正在使用AVFoundation构建iPhone应用程序。
是否有正确的程序来限制将被录制的音频数量?我想最多10秒。
感谢您的帮助/咨询/提示/指针..
AVAudioRecorder有以下方法:
- (BOOL)recordForDuration:(NSTimeInterval)duration
我认为,将这样的伎俩!
我通常不与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
谢谢你。我会等待和看到其他答复,但我真的很感谢你的时间和精力。 – 2011-04-17 23:16:02
顺便说一句,你为什么不与AVFoundation合作?有没有更好的方法?再次感谢.. – 2011-04-17 23:17:14
不,这只是我通常不使用音频。 – esqew 2011-04-19 00:20:51
这是从iOS编程食谱一个例子,我发现它是非常有用的和straigtforward。开始记录后,您会以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");
}
完美..谢谢。 – 2011-04-18 08:28:59
@DavidDelMonte是否为你工作? – Monicka 2015-07-13 06:51:50
那么,它在4年前被回答和接受。我想它是有效的。我不知道现在是否一样。为什么? – 2015-07-13 06:54:09