iOS - AVFoundation

来源:互联网 发布:a类宏程序编程实例 编辑:程序博客网 时间:2024/04/29 17:45

Recorder & Player

1.Recorder

   // 设定存储路径    NSArray *pathComponents = [NSArray arrayWithObjects:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject], @"MyAudioMemo.m4a", nil];    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];    // 创建session    AVAudioSession *session = [AVAudioSession sharedInstance];    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];    // 配置recorder    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];    [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];    // 创建recorder    recorder = [[AVAudioRecorder alloc]initWithURL:outputFileURL settings:recordSetting error:nil];    recorder.delegate = self;    recorder.meteringEnabled = YES;    [recorder prepareToRecord];

2.Player

        // 根据url播放音频文件        player = [[AVAudioPlayer alloc]initWithContentsOfURL:recorder.url error:nil];        [player setDelegate:self];        [player play];

3.AVAudioRecorderDelegate, AVAudioPlayerDelegate

4.Error Handling is necessary

2 0