音频录制之AVAudioRecorder
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface AudioPlayerViewController : UIViewController{ AVAudioPlayer *audioPlayer; AVAudioRecorder *audioRecorder; int recordEncoding; enum { ENC_AAC = 1, ENC_ALAC = 2, ENC_IMA4 = 3, ENC_ILBC = 4, ENC_ULAW = 5, ENC_PCM = 6, } encodingTypes; } - (IBAction) startRecording; - (IBAction) stopRecording; - (IBAction) playRecording; - (IBAction) stopPlaying; - (IBAction)btnclick:(id)sender; @end // M文件 #import "AudioPlayerViewController.h" @implementation AudioPlayerViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. recordEncoding = ENC_AAC; } - (void)dealloc { [audioPlayer release]; [audioRecorder release]; [super dealloc]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(IBAction) startRecording { NSLog(@"startRecording"); [audioRecorder release]; audioRecorder = nil; // Init audio with record capability AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10]; if(recordEncoding == ENC_PCM) { [recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey]; [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey]; [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; [recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; } else { NSNumber *formatObject; switch (recordEncoding) { case (ENC_AAC): formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC]; break; case (ENC_ALAC): formatObject = [NSNumber numberWithInt: kAudioFormatAppleLossless]; break; case (ENC_IMA4): formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4]; break; case (ENC_ILBC): formatObject = [NSNumber numberWithInt: kAudioFormatiLBC]; break; case (ENC_ULAW): formatObject = [NSNumber numberWithInt: kAudioFormatULaw]; break; default: formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4]; } [recordSettings setObject:formatObject forKey: AVFormatIDKey];//ID [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];//采样率 [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];//通道的数目,1单声道,2立体声 [recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];//解码率 [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];//采样位 [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey]; } NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]]; NSError *error = nil; audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error]; if ([audioRecorder prepareToRecord] == YES){ [audioRecorder record]; }else { int errorCode = CFSwapInt32HostToBig ([error code]); NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode); } NSLog(@"recording"); } -(IBAction) stopRecording { NSLog(@"stopRecording"); [audioRecorder stop]; NSLog(@"stopped"); } -(IBAction) playRecording { NSLog(@"playRecording"); // Init audio with playback capability AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.caf", [[NSBundle mainBundle] resourcePath]]]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = 0; [audioPlayer play]; NSLog(@"playing"); } -(IBAction) stopPlaying { NSLog(@"stopPlaying"); [audioPlayer stop]; NSLog(@"stopped"); } @end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?