AVAudioRecorder 录制

//建立音频会话和声音文件的URL

//注:  在.h引用里面加入AVAudioSessionDelegate以及AVAudioRecorderDelegate

- (void) viewDidLoad {
    [super viewDidLoad];
    NSString *soundFilePath = [@"/Users/Shared/" stringByAppendingString: @"sound.caf"];
    NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    self.soundFileURL = newURL;   //soundFileURL 在.h里面定义
    [newURL release];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    audioSession.delegate = self;
    [audioSession setActive: YES error: nil];
    recording = NO;
    playing = NO;
}
//一个基于AVAudioRecorder类的录制/停止方法
-(IBAction) recordOrStop: (id) sender {
    if (recording) {
        [soundRecorder stop];
        recording = NO;
        self.soundRecorder = nil;  //soundRecorder 在.h里面定义
 //recordOrStopButton 声明定义
        [recordOrStopButton setTitle: @"Record" forState: UIControlStateNormal];
        [recordOrStopButton setTitle: @"Record" forState: UIControlStateHighlighted];
        [[AVAudioSession sharedInstance] setActive: NO error: nil];
    } else {
        [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryRecord error: nil];
        NSDictionary *recordSettings =
            [[NSDictionary alloc] initWithObjectsAndKeys:
                [NSNumber numberWithFloat: 44100.0],                 AVSampleRateKey,
                [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
            nil];
        AVAudioRecorder *newRecorder = [[AVAudioRecorder alloc] initWithURL: soundFileURL
                                                                   settings: recordSettings
                                                                      error: nil];
        [recordSettings release];
        self.soundRecorder = newRecorder;
        [newRecorder release];
 
        soundRecorder.delegate = self;
        [soundRecorder prepareToRecord];  //创建音频文件 sound.caf
        [soundRecorder record];
        [recordOrStopButton setTitle: @"Stop" forState: UIControlStateNormal];
        [recordOrStopButton setTitle: @"Stop" forState: UIControlStateHighlighted];
 
        recording = YES;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值