ios录音小操作

//  ViewController.h
//  录音

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)clickRecordBtn:(id)sender;
- (IBAction)clickPauseBtn:(id)sender;
- (IBAction)clickResumeBtn:(id)sender;
- (IBAction)clickStopBtn:(id)sender;

@property (weak, nonatomic) IBOutlet UIProgressView *progressView;

@end


//  ViewController.m
//  录音


#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioPlayerDelegate,AVAudioRecorderDelegate>

@property (nonatomic, strong) AVAudioPlayer *audioPlay; //音频播放器
@property (nonatomic, strong) AVAudioRecorder *audioRecored; //音频录音机

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ViewController

-(NSTimer *)timer
{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(aduioPowerChange) userInfo:nil repeats:YES];
    }
    return _timer;
}

#pragma mark 录音声波的状态监测
-(void)aduioPowerChange
{
    [self.audioRecored updateMeters];//更新测量值
    float power= [self.audioRecored averagePowerForChannel:0];//取得第一个通道的音频,注意音频(声音)强度范围时-160到0 ,0代表最大输入
    NSLog(@".......... %f",power);
    CGFloat progress=(1.0/50.0)*(power+50.0);
    [_progressView setProgress:progress];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置音频会话
    [self setAudioSession];
}

#pragma mark 设置音频会话
-(void)setAudioSession
{
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    //设置为播放和录音的状态,以便可以在录制完成后播放
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [audioSession setActive:YES error:nil];
}

#pragma mark 获得音频的URL
-(NSURL *)getAudioUrl
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *audioPath = [documentPath stringByAppendingPathComponent:@"test.wav"];
    NSURL *url = [NSURL fileURLWithPath:audioPath];
    return url;
}

#pragma mark 获得音频播放器
-(AVAudioPlayer *)audioPlay
{
    if (!_audioPlay) {
        
        //1. 获得url路径
        NSURL *audioUrl = [self getAudioUrl];
        NSError *error;
        _audioPlay = [[AVAudioPlayer alloc] initWithContentsOfURL:audioUrl error:&error];
        _audioPlay.volume = 1.0;
        if (error) {
            NSLog(@"创建音频播放器的时候出现了错误!%@",[error localizedDescription]);
        }
    }
    return _audioPlay;
}

#pragma mark 获得录音机对象
-(AVAudioRecorder *)audioRecored
{
    if (!_audioRecored)
    {
        //1. 创建录音后的音频的保存路径
        NSURL *audioUrl = [self getAudioUrl];
        
        //2. 录音机的文件设置
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        //设置录音的音频格式
        [dic setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
        //设置录音的采样率 电话采样率为8000
        [dic setObject:@(8000) forKey:AVSampleRateKey];
        //设置通道
        [dic setObject:@(1) forKey:AVNumberOfChannelsKey];
        //采样点位数 8、16、24、32
        [dic setObject:@(8) forKey:AVLinearPCMBitDepthKey];
        //是否采用浮点数采样
        [dic setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
        //其他设置......
        
        //3. 创建录音机
        NSError *error;
        _audioRecored = [[AVAudioRecorder alloc] initWithURL:audioUrl settings:dic error:&error];
        if (error) {
            NSLog(@"创建录音机失败");
        }
        _audioRecored.meteringEnabled=YES;//如果要监控声波则必须设置为YES
        
        _audioRecored.delegate = self;
    }
    return _audioRecored;
}

#pragma mark 点击录音按钮
- (IBAction)clickRecordBtn:(id)sender {
    if (!self.audioRecored.isRecording) {
        
        [_audioPlay stop];
        
        [_audioRecored record];
        
        self.timer.fireDate = [NSDate distantPast];
    }
}

#pragma mark 点击暂停按钮
- (IBAction)clickPauseBtn:(id)sender {
    if (self.audioRecored.isRecording) {
        [_audioRecored pause];
        
        self.timer.fireDate = [NSDate distantFuture];
    }
}

#pragma mark 点击恢复按钮
- (IBAction)clickResumeBtn:(id)sender {
    [self clickRecordBtn:nil];
}

#pragma mark 点击停止按钮
- (IBAction)clickStopBtn:(id)sender {
    
    [_audioRecored stop];
    self.timer.fireDate = [NSDate distantFuture];
}

#pragma mark 协议中方法,录音结束
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag
{
    NSLog(@"录音结束");
    if (!self.audioPlay.isPlaying) {
        [_audioPlay play];
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值