首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在录制音频片段时设置时间限制?

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

Stack Overflow用户
提问于 2011-04-18 02:31:45
回答 3查看 6K关注 0票数 5

我根据帖子的标题查找了搜索词,但是..

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

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

感谢您的帮助/建议/提示/提示。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-04-18 12:11:10

AVAudioRecorder有以下方法:

代码语言:javascript
复制
- (BOOL)recordForDuration:(NSTimeInterval)duration

我想这样就行了!

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

票数 15
EN

Stack Overflow用户

发布于 2011-04-18 06:04:33

我通常不使用AVFoundation,所以我不知道确切的方法/类名称(我填写了自己的名称),但解决这个问题的一个办法是在最初开始录制时使用一个循环的NSTimer。如下所示:

代码语言:javascript
复制
@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
票数 4
EN

Stack Overflow用户

发布于 2015-07-13 04:21:20

这是iOS编程指南中的一个例子,我发现它非常有用和简单。开始录制后,调用延迟为10秒的停止函数,停止录制时会自动调用委托方法audioRecorderDidFinishRecording:successfully

代码语言:javascript
复制
@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");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5695523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档