介紹
- 錄音使用的也是AVFoundation框架
- 相關(guān)類:AVAudioRecorder
- 可以設(shè)置的一些屬性 :
<1>AVNumberOfChannelsKey 通道數(shù)
<2>AVSampleRateKey 采樣率 一般用44100
<3>AVLinearPCMBitDepthKey 比特率 一般設(shè)16 32
<4>AVEncoderAudioQualityKey 質(zhì)量
<5>AVEncoderBitRateKey 比特采樣率 一般是128000
使用
代碼示例
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVAudioRecorderDelegate>
{
AVAudioRecorder *audioRecorder;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = [UIColor colorWithRed:0.6223 green:0.4564 blue:0.9735 alpha:1.0];
[button setTitle:@"Play" forState:UIControlStateNormal];
[button setTitle:@"Pause" forState:UIControlStateSelected];
[button addTarget:self action:@selector(recorder:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self startAudioRecorder];
}
-(void)recorder:(UIButton *)sender{
sender.selected = !sender.selected;
sender.selected != YES?[audioRecorder stop]:[audioRecorder record];
}
-(void)startAudioRecorder{
/*
URL:是本地的URL 是AVAudioRecorder 需要一個(gè)存儲(chǔ)的路徑
*/
NSString *name =[NSString stringWithFormat:@"%d.aiff",(int)[NSDate date].timeIntervalSince1970];
NSString *path = [[ NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES) firstObject] stringByAppendingPathComponent:name];
NSError *error;
//初始化
/**
AVNumberOfChannelsKey:聲道數(shù) 通常為雙聲道 值2
AVSampleRateKey:采樣率 單位是HZ 通常設(shè)置成44100 44.1k
AVLinearPCMBitDepthKey:比特率 8 16 32
AVEncoderAudioQualityKey:聲音質(zhì)量 需要的參數(shù)是一個(gè)枚舉 :
AVAudioQualityMin 最小的質(zhì)量
AVAudioQualityLow 比較低的質(zhì)量
AVAudioQualityMedium 中間的質(zhì)量
AVAudioQualityHigh 高的質(zhì)量
AVAudioQualityMax 最好的質(zhì)量
AVEncoderBitRateKey:音頻的編碼比特率 BPS傳輸速率 一般為128000bps
*/
audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:@{AVNumberOfChannelsKey:@2,AVSampleRateKey:@44100,AVLinearPCMBitDepthKey:@32,AVEncoderAudioQualityKey:@(AVAudioQualityMax),AVEncoderBitRateKey:@128000} error:&error];
audioRecorder.delegate = self;
[audioRecorder prepareToRecord];
//這個(gè)是存儲(chǔ)錄音文件的路徑可以根據(jù)這個(gè)路徑查看是否錄音成功
//復(fù)制這個(gè)路徑 點(diǎn)擊桌面 commend+shift+G可以找到存放錄音文件的文件夾
NSLog(@"%@",path);
}
//獲取到錄音文件
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//NSFileManager文件操作的一個(gè)類
NSFileManager *manager = [NSFileManager defaultManager];
//獲得當(dāng)前文件的所有子文件:subpathsAtPath:
NSArray *pathList = [manager subpathsAtPath:path]
;
//需要只獲得錄音文件
NSMutableArray *audioPathList = [NSMutableArray array];
//遍歷這個(gè)文件夾下面的子文件
for (NSString *audioPath in pathList) {
//通過對(duì)比文件的延展名(擴(kuò)展名告组、尾綴)來區(qū)分是不是錄音文件
if ([audioPath.pathExtension isEqualToString:@"aiff"]) {
//把篩選出來的文件放到數(shù)組中 -> 得到所有的音頻文件
[audioPathList addObject:audioPath];
}
}
NSLog(@"錄音結(jié)束");
NSLog(@"%@",pathList);
NSLog(@"%@",audioPathList);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者