录制音频

//

//  ViewController.m

//  录音

//

//  Created by zhusongxiu on 15/8/20.

//  Copyright (c) 2015 zhusongxiu. All rights reserved.

//


#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);

    

//    AVNumberOfChannelsKey  通道

//    AVEncoderAudioQualityKey 音质

//    AVEncoderBitRateKey 比特率

//    比特率 音频的传输速度  8 16 32 64 值越大 传输速度越快 音质越高

    

    NSDictionary *setting = @{AVNumberOfChannelsKey:@(2),AVEncoderAudioQualityKey:@(AVAudioQualityMedium),AVEncoderBitRateKey:@(64),AVSampleRateKey:@(44100)};

    

//    settings :录音的音频设置 采样率  通道 质量。。。

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

    

    audioRecorder.delegate = self;

//    录音的当前时间:

//    audioRecorder.currentTime

    

//    预录制

    [audioRecorder prepareToRecord];

    

//    录制

    [audioRecorder record];

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

    UIButton *stopRecorder = [UIButton buttonWithType:UIButtonTypeCustom];

    stopRecorder.frame = CGRectMake(100, 100, 100, 30);

    stopRecorder.backgroundColor = [UIColor yellowColor];

    [stopRecorder addTarget: self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:stopRecorder];

    

}


- (void)stop{

    [audioRecorder stop];

}



#pragma mark ----------录音结束-----------

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

    NSLog(@"^_^");

    

//    文件管理类:NSFileManager

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

//    判断文件(或目录)是否存在 ,是否可以读取写入

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

    

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

    NSFileManager *manager = [NSFileManager defaultManager];

//     Path  指定创建文件的目录

//     contents 指定文件创建的内容

//     attributes 文件的属性

    

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

//    创建文件

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

    if (success) {

        NSLog(@"文件路径:%@",path);

//        移动文件

//        目标目录

        NSString *newpath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"dd.jpg"];

        NSError *error;

//        移动 剪切文件

       BOOL ss = [manager moveItemAtPath:path toPath:newpath error:&error];

//        拷贝 两个文件都存在

//        [manager copyItemAtPath:path toPath:newpath error:&error];

//        删除文件

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

        

//        判断文件是否存在

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

            NSLog(@"文件已经存在");

        }

        

//        获得目录中的子文件

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

        NSArray *all = [manager subpathsAtPath:newpath];

        

        

        if (ss) {

            NSLog(@"移动后%@",newpath);


        }else{

            NSLog(@"%@",error);

        }

    }

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值