iOS音频录制基础


#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>


@interface ViewController ()<AVAudioRecorderDelegate>

{

    AVAudioRecorder *audioRecorder;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    NSString *path = [NSTemporaryDirectory()stringByAppendingPathComponent:@"rec.caf"];

    NSLog(@"%@",path);

//    settings 录制 音频设置

//     NSDictionary *setting = @{AVNumberOfChannelsKey:@(2),AVEncoderAudioQualityKey:@(AVAudioQualityHigh),AVEncoderBitRateKey:@(32),AVSampleRateKey:@(40000)};

    audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:nil error:nil];

//    录音的当前时间

//    audioRecorder.currentTime

//    预录制

    audioRecorder.delegate = self;

    [audioRecorder prepareToRecord];

    [audioRecorder record];

     NSLog(@"----%@", audioRecorder.settings);

    UIButton *button = [UIButton buttonWithType:0];

    button.frame = CGRectMake(100, 100, 100, 100);

    button.backgroundColor = [UIColor redColor];

    [button addTarget:self action:@selector(stopRec) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

    

}

#pragma mark ------AudioRecorderDelegate的代理方法----------

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

{

    NSLog(@"...........");

    

//    NSFileManager 文件管理类 创建文件  移动  复制  删除

//    判断文件是否存在

//    是否可以读取  写入

//    subpathsAtPath得到指定目录下的子文件

    

//  初始化文件管理类(单例)

     NSFileManager *manager = [NSFileManager defaultManager];

//    path 指定文件创建的目录  contents指定文件的内容  attributes文件的属性

    

    NSString *path = [NSTemporaryDirectory()stringByAppendingPathComponent:@"DFSW"];

//创建文件

    NSString *image = [[NSBundle mainBundle]pathForResource:@"王菲3.jpg" ofType:nil];

   

    NSData *data = [NSData dataWithContentsOfFile:image];

   

   BOOL success = [manager createFileAtPath:path contents:data attributes:nil];

    if (success) {

        NSLog(@"~~~~~%@",path);

    }

//    移动文件

//    目标目录

    NSString *newPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"DFSW"];

//    删除文件

//    [manager removeItemAtPath:path error:nil];

    if ([manager fileExistsAtPath:newPath] == YES) {

        NSLog(@"文件存在");

    }

//    [manager moveItemAtPath:path toPath:newPath error:nil];

    NSLog(@"!!!!%@",newPath);

    

//   获得目录里面的子文件

//    返回值是一个数组 里面存的是所有子文件的名字

//   NSArray *all = [manager subpathsAtPath:newPath];

}


- (void)stopRec

{

    [audioRecorder stop];

}



@end








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值