IOS development to achieve the recording function
- 2020-05-17 06:35:54
- OfStack
Import framework:
#import <AVFoundation/AVFoundation.h>
Declare global variables:
@interface ViewController ()<AVAudioRecorderDelegate>
{
AVAudioRecorder *audioRecorder;
}
@end
In ViewDidLoad:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
[button setTitle:@"TICK" forState:UIControlStateNormal];
button.backgroundColor = [UIColor brownColor];
[button addTarget:self action:@selector(startAudioRecoder:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Button trigger event
- (void)startAudioRecoder:(UIButton *)sender{
sender.selected = !sender.selected;
if (sender.selected != YES) {
[audioRecorder stop];
return;
}
// URL Is local URL AVAudioRecorder Need to be 1 The path to storage
NSString *name = [NSString stringWithFormat:@"%d.aiff",(int)[NSDate date].timeIntervalSince1970];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:name];
NSError *error;
// The tape recorder Initialize the
audioRecorder = [[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:@{AVNumberOfChannelsKey:@2,AVSampleRateKey:@44100,AVLinearPCMBitDepthKey:@32,AVEncoderAudioQualityKey:@(AVAudioQualityMax),AVEncoderBitRateKey:@128000} error:&error];
[audioRecorder prepareToRecord];
[audioRecorder record];
audioRecorder.delegate = self;
/*
1.AVNumberOfChannelsKey The channel number It is usually a double track value 2
2.AVSampleRateKey Sampling rate unit HZ Usually set to 44100 That is 44.1k
3.AVLinearPCMBitDepthKey Bit rate 8 16 24 32
4.AVEncoderAudioQualityKey Sound quality
1. AVAudioQualityMin = 0, Minimum mass
2. AVAudioQualityLow = 0x20, Lower quality
3. AVAudioQualityMedium = 0x40, Intermediate mass
(4) AVAudioQualityHigh = 0x60, High quality
5. AVAudioQualityMax = 0x7F The best quality
5.AVEncoderBitRateKey The bit rate of audio encoding unit Kbps Rate of transmission 1 A set 128000 That is 128kbps
*/
NSLog(@"%@",path);
}
Agent method:
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
NSLog(@" End of the recording ");
// File operation class
NSFileManager *manger = [NSFileManager defaultManager];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// Gets all subfiles of the current file subpathsAtPath
NSArray *pathlList = [manger subpathsAtPath:path];
// You only need to get the recording file
NSMutableArray *audioPathList = [NSMutableArray array];
// Walk through all the subfiles in this folder
for (NSString *audioPath in pathlList) {
// By comparing the file's extension name (extension) The final syllable) To distinguish whether it is a recording file
if ([audioPath.pathExtension isEqualToString:@"aiff"]) {
// Put the filtered files into an array
[audioPathList addObject:audioPath];
}
}
NSLog(@"%@",audioPathList);
}