xcode 在录制音频剪辑时设置时间限制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5695523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting a time limit when recording an audio clip?
提问by David DelMonte
I looked for search terms along the lines of the post title, but alas..
我沿着帖子标题的行寻找搜索词,但是唉..
I am building an iPhone app using AVFoundation.
我正在使用 AVFoundation 构建一个 iPhone 应用程序。
Is there a correct procedure to limit the amount of audio that will be recorded? I would like a maximum of 10 seconds.
是否有正确的程序来限制将要录制的音频量?我想要最多 10 秒。
Thanks for any help/advice/tips/pointers..
感谢您的任何帮助/建议/提示/指针..
回答by sbooth
AVAudioRecorder has the following method:
AVAudioRecorder 有以下方法:
- (BOOL)recordForDuration:(NSTimeInterval)duration
I think that will do the trick!
我认为这会奏效!
回答by esqew
I don't normally work with AVFoundation
so I don't know the exact method/class names (I filled in my own), but a workaround to this would be having a recurring NSTimer
beginning when the recording originally starts. Something like this:
我通常不使用,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
回答by Monicka
This is an example from the iOS Programming Cookbook, i found it very useful and straigtforward. After starting to record, you call the stop function with delay of 10 seconds, when it stops recording it will automatically call the delegate method audioRecorderDidFinishRecording:successfully
.
这是 iOS Programming Cookbook 中的一个示例,我发现它非常有用且直截了当。开始录音后,延迟10秒调用stop函数,停止录音时会自动调用delegate方法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");
}