录制音频剪辑时设置时间限制?

时间:2011-04-17 18:31:45

标签: xcode ios4 core-audio avaudiorecorder

我按照帖子标题的方式查找搜索字词,但唉...

我正在使用AVFoundation构建iPhone应用程序。

是否有正确的程序来限制将要录制的音频量?我想最多10秒钟。

感谢您提供任何帮助/建议/提示/指示..

3 个答案:

答案 0 :(得分:14)

AVAudioRecorder有以下方法:

- (BOOL)recordForDuration:(NSTimeInterval)duration

我认为这样就可以了!

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioRecorder_ClassReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008238

答案 1 :(得分:4)

我通常不会使用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

答案 2 :(得分:1)

这是来自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");
}