AVAudioRecorder

#import "ViewController.h"
#import 
@interface ViewController ()
{

    AVAudioRecorder *recorder;//录音对象
    AVAudioPlayer *player;//播放对象
    NSURL *fileUrl;//文件路径
    
}
@end
//按下录音
- (IBAction)recoder:(id)sender {
    //(1)url
    NSString *urlStr = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/recoder.aac"];
    
    //删除之前的录音
    [[NSFileManager defaultManager]removeItemAtPath:urlStr error:nil];
    
    
    fileUrl = [NSURL fileURLWithPath:urlStr];
    
    
    
    //(2)设置录音的音频参数
    /*
     1 ID号:acc
     2 采样率(HZ):每秒从连续的信号中提取并组成离散信号的采样个数
     3 通道的个数:(1 单声道 2 立体声)
     4 采样位数(8 16 24 32) 衡量声音波动变化的参数
     5 大端或者小端 (内存的组织方式)
     6 采集信号是整数还是浮点数
     7 音频编码质量
     
     
     */
    NSDictionary *info = @{
                           AVFormatIDKey:[NSNumber numberWithInt:kAudioFormatMPEG4AAC],//音频格式
                           AVSampleRateKey:@1000,//采样率
                           AVNumberOfChannelsKey:@2,//声道数
                           AVLinearPCMBitDepthKey:@8,//采样位数
                           AVLinearPCMIsBigEndianKey:@NO,
                           AVLinearPCMIsFloatKey:@NO,
                           AVEncoderAudioQualityKey:[NSNumber numberWithInt:AVAudioQualityMedium],
            
                           };
    
    
    /*
     url:录音文件保存的路径
     settings: 录音的设置
     error:错误
     */
    recorder = [[AVAudioRecorder alloc]initWithURL:fileUrl settings:info error:nil];
    
    [recorder prepareToRecord];
    [recorder record];
    
    
    
}
//抬手结束录音
- (IBAction)stopRecoder:(id)sender {
    [recorder stop];
    
    //手动释放
    recorder =nil;
    
    
}
//播放录音
- (IBAction)playRecoder:(id)sender {
    
    //多次播放的时候
    player = nil;
    
    player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileUrl error:nil];
    
    if (player) {
        
        [player prepareToPlay];
        [player play];
        
    }
}

你可能感兴趣的:(AVAudioRecorder)