简体   繁体   English

在录制音频片段时设置时间限制?

[英]Setting a time limit when recording an audio clip?

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.. 感谢您的帮助/建议/提示/指针。

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. 我通常不使用AVFoundation所以我不知道确切的方法/类名(我自己填写了),但是要解决此问题,应在最初开始录制时让NSTimer重复出现。 Something like this: 像这样:

@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

This is an example from the iOS Programming Cookbook, i found it very useful and straigtforward. 这是iOS编程手册中的一个示例,我发现它非常有用且精益求精。 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 . 开始录制后,您以10秒的延迟调用stop函数,当它停止录制时,它将自动调用委托方法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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM