IOS录音机--------by talent.L

1设置权限

Privacy - Microphone Usage Description      microphoneDesciption

2引入框架AVFoundation

3.m

#import "ViewController.h"

#import

@interface ViewController ()

{//录音

AVAudioRecorder *audioRecorder;

//声音播放

AVAudioPlayer *player;

//拖动条

UISlider *processSlider;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(100, 100, 100, 100);

[button setTitle:@"录音/停止" forState:UIControlStateNormal];

button.backgroundColor = [UIColor brownColor];

[button addTarget:self action:@selector(startAudioRecoder:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeCustom];

buttonTwo.frame = CGRectMake(100, 300, 100, 100);

[buttonTwo setTitle:@"播放" forState:UIControlStateNormal];

buttonTwo.backgroundColor = [UIColor redColor];

[buttonTwo addTarget:self action:@selector(startPlayer:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:buttonTwo];

UIButton *buttonTwo1 = [UIButton buttonWithType:UIButtonTypeCustom];

buttonTwo1.frame = CGRectMake(250, 300, 100, 100);

[buttonTwo1 setTitle:@"停止" forState:UIControlStateNormal];

buttonTwo1.backgroundColor = [UIColor redColor];

[buttonTwo1 addTarget:self action:@selector(startPlayer1:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:buttonTwo1];

//拖动条

processSlider = [[UISlider alloc] initWithFrame:CGRectMake(90, 220, 200, 20)];

[processSlider addTarget:self action:@selector(processChanged) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:processSlider];

//定时器

[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(updateSliderValue) userInfo:nil repeats:YES];

}

//拖动条方法

-(void)processChanged

{

[player setCurrentTime:processSlider.value*player.duration];

}

//定时器方法

-(void)updateSliderValue

{

processSlider.value = player.currentTime/player.duration;

}

//录音 和 停止

- (void)startAudioRecoder:(UIButton *)sender{

sender.selected = !sender.selected;

if (sender.selected != YES) {

[audioRecorder stop];

return;

}

//  URL是本地的URL AVAudioRecorder需要一个存储的路径

//  URL是本地的URL AVAudioRecorder需要一个存储的路径

NSString *name = @"tcdsgzg.aiff";

NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:name];

//    NSLog(@"%@",path);

NSError *error;

//  录音机 初始化

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 通道数 通常为双声道 值2

2.AVSampleRateKey 采样率 单位HZ 通常设置成44100 也就是44.1k

3.AVLinearPCMBitDepthKey 比特率 8 16 24 32

4.AVEncoderAudioQualityKey 声音质量

① AVAudioQualityMin  = 0, 最小的质量

② AVAudioQualityLow  = 0x20, 比较低的质量

③ AVAudioQualityMedium = 0x40, 中间的质量

④ AVAudioQualityHigh  = 0x60,高的质量

⑤ AVAudioQualityMax  = 0x7F 最好的质量

5.AVEncoderBitRateKey 音频编码的比特率 单位Kbps 传输的速率 一般设置128000 也就是128kbps

*/

//    NSLog(@"%@",path);

}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{

NSLog(@"录音结束");

//  文件操作的类

NSFileManager *manger = [NSFileManager defaultManager];

NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

//  获得当前文件的所有子文件subpathsAtPath

NSArray *pathlList = [manger subpathsAtPath:path];

//  需要只获得录音文件

NSMutableArray *audioPathList = [NSMutableArray array];

//  遍历所有这个文件夹下的子文件

for (NSString *audioPath in pathlList) {

//    通过对比文件的延展名(扩展名 尾缀) 来区分是不是录音文件

if ([audioPath.pathExtension isEqualToString:@"aiff"]) {

//      把筛选出来的文件放到数组中

[audioPathList addObject:audioPath];

}

}

NSLog(@"%@",audioPathList);

}

-(void)startPlayer:(UIButton *)btn{

NSString *name = @"tcdsgzg.aiff";

NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:name];

NSData *data = [[NSData data]initWithContentsOfFile:path];

player = [[AVAudioPlayer alloc]initWithData:data error:nil];

//设置是否可以倍速(或者放慢速度)播放

player.enableRate = YES;

//设置播放速度 一般0.5-2.0

player.rate = 0.5;

[player play];

}

-(void)startPlayer1:(UIButton *)btn{

[player pause];

}

@end

你可能感兴趣的:(IOS录音机--------by talent.L)