录制音频
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
// 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
相关文章推荐
- 判断是否是变形字符串
- Lesson01UIView 用户界面
- NoSQL数据库是否适用于非互联网行业
- 浅析C++中的重载运算符(重点是重载+=)
- Wing IDE 5.1.8的爆破点
- 浅析C++中的重载运算符(重点是重载+=)
- 点击启动图标如何定位到App上次打开的页面
- php 不能同时提交form
- 【特种兵Excel教程】如何用Excel制作堆积柱形图表?
- CCDT与CDT
- 判断是否安装微博
- 【白话经典算法系列之十七】 数组中只出现一次的数 其他三次
- tomcat 多域名配置问题,context配置书写规范导致的jsp编译异常
- html中的简单实例演示(checkbox)的使用
- iOS 警告收录及科学快速的消除方法
- Android 开发记录
- mysql NULL 查找条件里包含NULL值是的情况
- Java IO 概述
- java的Split函数如何区分多个空格
- 通过调整像素点来改变图片颜色