iOS 录音机 功能(系统方法)~demo

//联系人:石虎  QQ: 1224614774昵称:嗡嘛呢叭咪哄

/**

注意点: 1.看 GIF 效果图.

       2.看连线视图的效果图.

       3.看实现代码(直接复制实现效果).

*/

一、GIF 效果图:

 这是实现录音机效果图--略


二、连线视图的效果图:

图1:


图2:


图3:


图4:


图5:


图6:



          *****************  录音机 代码实现  ******************

三、实现代码:

=========================

===================================================

====================

//  ViewController.m

//  录音机

//

//  Created by 石虎 on 2017/6/28.

//  Copyright © 2017 shihu. All rights reserved.

//*****************  录音机  ******************


#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>


@interface ViewController ()<AVAudioRecorderDelegate>

{

    AVAudioRecorder *_audioRecoder;//音频录音机

    AVAudioPlayer *_avplayer;//音频播放器

    NSTimer *_timer;//定时器

    int      _count;//保存数量

    BOOL     _isSwitch;//是否开关

}


//开始录制属性

@property (weak,nonatomic)IBOutletUIButton *btnStart;

//显示时间

@property (weak,nonatomic)IBOutletUILabel *showTime;

//回放属性

@property (weak,nonatomic)IBOutletUIButton *btnPlayBack;

//文件路径

@property (nonatomic ,copy)NSString *documentsPath;


//开始录制

- (IBAction)startRecording:(id)sender;

//停止录制

- (IBAction)stopRecording:(id)sender;

//回放录音

- (IBAction)playBackRecording:(id)sender;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    _isSwitch =YES;

}


//开始录制

- (IBAction)startRecording:(id)sender {

    //判断录音控制器是否为空或者正在录制;

    if (_audioRecoder==nil &&_audioRecoder.isRecording)

    {

        //设置控制器停止录制;

        [_audioRecoderstop];

        //设置按钮的标题为开始录制;

        [_btnStartsetTitle:@"开始录制"forState:UIControlStateNormal];

        [_timerinvalidate];

        _timer =nil;


    }else{

        _count =0;

        _timer = [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfselector:@selector(repeatShowTime:)userInfo:@"admin"repeats:YES];

        

#pragma mark 下面设置录音的参数和录音文件的保存路径等信息

        //获取音频文件的保存路径

        NSString *destinationStr = [[selfdocumentsPath]stringByAppendingPathComponent:@"sound.wav"];

        NSURL *destinationUrl = [NSURLfileURLWithPath:destinationStr];

        //创建一个Dictionary,用于保存录制属性

        NSMutableDictionary *recordSettings = [[NSMutableDictionaryalloc]init];

        //设置录制音频的格式

        [recordSettings setObject:[NSNumbernumberWithInt:kAudioFormatLinearPCM]forKey:AVFormatIDKey];

        //设置录制音频的采样率

        //        [recordSettings setObject:[NSNumber numberWithFloat:@"1".floatValue] forKey:AVSampleRateKey];

        //设置录制音频的通道数

        [recordSettings setObject:[NSNumbernumberWithInt:(_isSwitch =/* DISABLES CODE */ (YES) ?2:1)]forKey:AVNumberOfChannelsKey];

        //设置录制音频采用高位优先的记录格式

        [recordSettings setObject:[NSNumbernumberWithBool:YES]forKey:AVLinearPCMIsBigEndianKey];

        //设置采样信号采用浮点数

        [recordSettings setObject:[NSNumbernumberWithBool:YES]forKey:AVLinearPCMIsFloatKey];

        NSError *recorderSetupError =nil;

        

#pragma mark 到这里开始实例化录音对象

        //初始化AVAudioRecorder

        _audioRecoder = [[AVAudioRecorderalloc]initWithURL:destinationUrlsettings:recordSettingserror:&recorderSetupError];

        _audioRecoder.delegate =self;

        [_audioRecoderrecord];

        //设置单个按钮的状态为录音

        [_btnStartsetTitle:@"正在录音"forState:UIControlStateNormal];

    }

}


//停止播放

- (IBAction)stopRecording:(id)sender {

    

    [_audioRecoderstop];

    [_btnStartsetTitle:@"开始录制"forState:UIControlStateNormal];

    //设置计时器为初始值;

    if (_timer) {

        [_timerinvalidate];

        _timer =nil;

    }

    _count =0;

    _showTime.text =@"00:00";

}


//回放录音

- (IBAction)playBackRecording:(id)sender {

    

    //获取音频文件的保存路径

    NSString *destinationString = [[selfdocumentsPath]stringByAppendingPathComponent:@"sound.wav"];

    NSURL *url = [NSURLfileURLWithPath:destinationString];

    //创建AVAudioPlayer对象

    _avplayer = [[AVAudioPlayeralloc]initWithContentsOfURL:urlerror:nil];

    //开始播放

    [_avplayerplay];

    _btnPlayBack.backgroundColor=[UIColorgreenColor];

    

}


//获取Documents目录路径

-(NSString *)documentsPath{

    

    if (!_documentsPath) {

        

        NSArray *searchPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

        _documentsPath = searchPath[0];

        

    }

    

    return_documentsPath;

}


#pragma mark- 录制音频的代理方法

- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder

{

    NSLog(@"---->被中断!");

}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)aRecorder successfully:(BOOL)flag

{

    if(flag)

    {

        NSLog(@"---->录制完成!!");

    }

}

- (void)repeatShowTime:(NSTimer *)tempTimer {

    

    _count++;

    //设置在文本框上显示时间;

    _showTime.text = [NSStringstringWithFormat:@"%02d:%02d",_count/60,_count%60];

}


- (void)dealloc {  //销毁NSTimer</span>

    if (_timer) {

        [_timerinvalidate];

        _timer =nil;

    }

}


@end


    谢谢!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值