#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