iOS 录音

作者: 超_iOS | 来源:发表于2016-04-09 09:11 被阅读799次

    很早之前做过的demo,送给需要的人,有空再补充相关知识.需要导入AVFoundation框架和#import <AVFoundation/AVFoundation.h>头文件

    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UIButton *luyinbutton;
    @property (weak, nonatomic) IBOutlet UIButton *playbutton;
    @property (weak, nonatomic) IBOutlet UIImageView *imageview;
    @property (nonatomic, retain)AVAudioPlayer * avPlay;
    @property (nonatomic, retain) AVAudioRecorder * recorder;
    //坚挺音量大小,控制话筒图片
    @property (nonatomic, retain) NSTimer * timer;
    @property (nonatomic, retain) NSURL * urlPlay;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //录音按钮绑定方法
        //按下
        [self.luyinbutton addTarget:self action:@selector(butDown:) forControlEvents:UIControlEventTouchDown];
        
        //手起来时
        [self.luyinbutton addTarget:self action:@selector(butUP:) forControlEvents:UIControlEventTouchUpInside];
        
        //收移开时
        [self.luyinbutton addTarget:self action:@selector(butDragUP:) forControlEvents:UIControlEventTouchDragExit];
        
        //播放按钮绑定方法
        [self.playbutton addTarget:self action:@selector(playRecordSoundButton:) forControlEvents:UIControlEventTouchUpInside];
        [self audio];
        self.imageview.image = [UIImage imageNamed:@"record_animate_01.png"];
        
        
    }
    //播放按钮方法
    - (void)playRecordSoundButton:(UIButton *)sender
    {
        //如果正在播放声音就暂停
        if (self.avPlay.playing) {
            [self.avPlay stop];
            return;
        }
        //初始化AVAudioPlayer 对象
        AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.urlPlay error:nil];
        self.avPlay = player;
        [self.avPlay play];
        
    }
    
    - (void)butUP:(UIButton *)sender
    {
        double cTime = self.recorder.currentTime;
        if (cTime > 2) {
            NSLog(@"发送成功");
        }else{
        //删除记录的文件
        [self.recorder deleteRecording];
        }
        [self.recorder stop];//录音结束
        [self.timer invalidate];//取消定时器
    }
    
    - (void)butDragUP:(UIButton *)sender
    {
        [self.recorder deleteRecording];
        [self.recorder stop];//录音结束
        [self.timer invalidate];//取消定时器
        NSLog(@"取消发送");
    }
    
    - (void)butDown:(UIButton *)sender
    {
        if ([self.recorder prepareToRecord]) {
            //开始录音
            [self.recorder record];
        }
        //定时检测
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(detectionVice) userInfo:nil repeats:YES];
    }
    
    //检测当前声音
    - (void)detectionVice
    {
        [self.recorder updateMeters];
        double lowPassResults = pow(10, (0.5 * [self.recorder peakPowerForChannel:0]));
        //图片随音量大小变化
        if (0<lowPassResults<=0.06) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_01.png"]];
        }else if (0.06<lowPassResults<=0.13) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_02.png"]];
        }else if (0.13<lowPassResults<=0.20) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_03.png"]];
        }else if (0.20<lowPassResults<=0.27) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_04.png"]];
        }else if (0.27<lowPassResults<=0.34) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_05.png"]];
        }else if (0.34<lowPassResults<=0.41) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_06.png"]];
        }else if (0.41<lowPassResults<=0.48) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_07.png"]];
        }else if (0.48<lowPassResults<=0.55) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_08.png"]];
        }else if (0.55<lowPassResults<=0.62) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_09.png"]];
        }else if (0.62<lowPassResults<=0.69) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_10.png"]];
        }else if (0.69<lowPassResults<=0.76) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_11.png"]];
        }else if (0.76<lowPassResults<=0.83) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_12.png"]];
        }else if (0.83<lowPassResults<=0.9) {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_13.png"]];
        }else {
            [self.imageview setImage:[UIImage imageNamed:@"record_animate_14.png"]];
        }//图片根据音量来变化,大家知道就好
    
    }
    
    //录音设置
    - (void)audio
    {
        NSMutableDictionary *recodeSeting = [@{} mutableCopy];
        [recodeSeting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
        //录音通道数
        [recodeSeting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
        //线性采样位数
        [recodeSeting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
        //录音质量
        [recodeSeting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
        //数据持久化(将声音存储到磁盘中)
        NSString *strUrl = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@lll.aac", strUrl]];
        self.urlPlay = nil;
        NSError *error = nil;
        //初始化AVAudioRecorder
        self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recodeSeting error:&error];
        self.recorder.meteringEnabled = YES;
    }
    

    相关文章

      网友评论

      • hhgvg:乱jb写啊 你
        超_iOS:@hhgvg 666
      • 小猪圣骑士:最后的 self.urlPlay = nil; 应该是 self.urlPlay = url; 不然 self.urlPlay 是个空值。
      • qBryant:学习一下。。。

      本文标题:iOS 录音

      本文链接:https://www.haomeiwen.com/subject/gvzvlttx.html