IOS錄音機(jī)--------by talent.L

1設(shè)置權(quán)限

Privacy - Microphone Usage Description? ? ? microphoneDesciption

2引入框架AVFoundation

3.m

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVAudioRecorderDelegate>

{//錄音

AVAudioRecorder *audioRecorder;

//聲音播放

AVAudioPlayer *player;

//拖動(dòng)條

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];

//拖動(dòng)條

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

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

[self.view addSubview:processSlider];

//定時(shí)器

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

}

//拖動(dòng)條方法

-(void)processChanged

{

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

}

//定時(shí)器方法

-(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需要一個(gè)存儲(chǔ)的路徑

//? URL是本地的URL AVAudioRecorder需要一個(gè)存儲(chǔ)的路徑

NSString *name = @"tcdsgzg.aiff";

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

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

NSError *error;

//? 錄音機(jī) 初始化

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 通道數(shù) 通常為雙聲道 值2

2.AVSampleRateKey 采樣率 單位HZ 通常設(shè)置成44100 也就是44.1k

3.AVLinearPCMBitDepthKey 比特率 8 16 24 32

4.AVEncoderAudioQualityKey 聲音質(zhì)量

① AVAudioQualityMin? = 0, 最小的質(zhì)量

② AVAudioQualityLow? = 0x20, 比較低的質(zhì)量

③ AVAudioQualityMedium = 0x40, 中間的質(zhì)量

④ AVAudioQualityHigh? = 0x60,高的質(zhì)量

⑤ AVAudioQualityMax? = 0x7F 最好的質(zhì)量

5.AVEncoderBitRateKey 音頻編碼的比特率 單位Kbps 傳輸?shù)乃俾?一般設(shè)置128000 也就是128kbps

*/

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

}

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

NSLog(@"錄音結(jié)束");

//? 文件操作的類

NSFileManager *manger = [NSFileManager defaultManager];

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

//? 獲得當(dāng)前文件的所有子文件subpathsAtPath

NSArray *pathlList = [manger subpathsAtPath:path];

//? 需要只獲得錄音文件

NSMutableArray *audioPathList = [NSMutableArray array];

//? 遍歷所有這個(gè)文件夾下的子文件

for (NSString *audioPath in pathlList) {

//? ? 通過對(duì)比文件的延展名(擴(kuò)展名 尾綴) 來區(qū)分是不是錄音文件

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

//? ? ? 把篩選出來的文件放到數(shù)組中

[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];

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

player.enableRate = YES;

//設(shè)置播放速度 一般0.5-2.0

player.rate = 0.5;

[player play];

}

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

[player pause];

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容