您的位置:首页 > 其它

录制音频

2015-10-15 21:41 302 查看
//

// 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: