打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
IOS AVFoundation的录音与播放
这是我第一次撰写博客,大部分内容都来自网络,写的不对地方还请多多指教,如果有摘录的地方与原作略有相同还请谅解说明。请多多支持。
1,AVAudioSession的使用
AVAudioSession是一个单例模式。在IOS7以前可以不用设置,在IOS7上不设置AVAudioSession则不可以录音。
一,设置AVAudioSession的类别(部分)及开启音频会话
Category(类别)作用
AVAudioSessionCategoryPlayback后台播放
AVAudioSessionCategoryRecord录音
AVAudioSessionCategoryPlayAndRecord后台播放及录音
具体代码如下:
//录音权限设置AVAudioSession * audioSession = [AVAudioSession sharedInstance];//设置类别只支持录音[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];//启动音频会话管理,此时会阻断后台音乐的播放[audioSession setActive:YES error:nil];二,在录音或播放结束后,要关闭音频会话,来延续后台音乐的播放
AVAudioSession * audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];[audioSession setActive:NO error:nil];
三,要想启用其他程序的后台音乐播放,则要用如下设置
AVAudioSession * audioSession = [AVAudioSession sharedInstance];[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];[audioSession setActive:NO withFlags:AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivati??on error:nil];
2,AVAudioRecorder的基本使用
一,设置参数
重点作用值
AVFormatIDKey录音格式kAudioFormatMPEG4AAC,kAudioFormatLinearPCM ...
AVSampleRateKey录音采样率
影响音频的质量8000,44100,96000
AVNumberOfChannelsKey录音通道数1或2
AVLinearPCMBitDepthKey线性采样位数8,16,24,32
AVEncoderAudioQualityKey录音质量AVAudioQualityMin,AVAudioQualityLow,
AVAudioQualityMedium,AVAudioQualityHigh,
AVAudioQualityMax
二,保存路径的网址设置
// CFUUID每次都会产生一个唯一号CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);NSString * cfuuidString =(NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));NSString * catchPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSString * audioRecordFilePath = [catchPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.AAC”,cfuuidString];NSURL * url = [NSURL fileURLWithPath:audioRecordFilePath];
三,AVAudioRecorder初始化
NSError *error = nil;AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error];
四,所有代码如下:
//录音参数设置设置NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc] init];//设置录音格式[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];//设置录音采样率[recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];//录音通道数[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];//线性采样位数[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];//录音的质量[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];//录音文件保存的网址CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);NSString *cfuuidString = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, cfuuid));NSString *catchPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSString *audioRecordFilePath = [catchPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.AAC", cfuuidString];NSURL *url = [NSURL fileURLWithPath:audioRecordFilePath];NSError *error = nil;//初始化AVAudioRecorder_recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error];if (!error){ //NSLog(@"初始化录音错误:%@", error);} else { if ([_recorder prepareToRecord]){ //录音最长时间设置 [_recorder recordForDuration:20]; //委托事件 _recorder.delegate = self; [_recorder record]; //开启音量检测 _recorder.meteringEnabled = YES; //开启定时器,音量监测 _timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(volumeMeters:) userInfo:nil repeats:YES]; }}#pragma mark 实时监测音量变化定时器任务- (void)volumeMeters:(NSTimer *)timer{ //刷新音量数据 [_recorder updateMeters]; double lowPassResults = POW(10, (0.05 * [_recorder peakPowerForChannel:0])); if(0 <lowPassResults <= 0.14){ }else if(0.14 <lowPassResults <= 0.28){ }else if(0.28 <lowPassResults <= 0.42){ }else if(0.42 <lowPassResults <= 0.56){ }else if(0.56 <lowPassResults <= 0.7){ }else if(0.7 <lowPassResults <= 0.84){ }else if(0.84 <lowPassResults <= 0.98){ }else{ }}// AVAudioRecorder委托事件- (void)audioRecorderDidFinishRecording(AVAudioRecorder *)recorder successfully:(BOOL)flag{ //录音结束}- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error{ //录音编码错误}
3,AVAudioPlayer的使用
主要用于音频文件的播放,它主要有两个初始化方法:initWithData与initWithContentsOfURL。两个一般都可以使用,但在使用initWithContentsOfURL时要注意传入文件的文件名的格式,稍有不同,则无法播放,如:aac文件,如果后缀名为大写AAC,则无法播放。
// initWithContentsOfURLNSURL * urlAudio = [NSURL fileURLWithPath:audioPath];AVAudioPlayer * _player = [[AVAudioPlayer alloc] initWithContentsOfURL:urlAudio error:nil];// initWithDataNSData * dataAudio = [NSData dataWithContentsOfFile:audioPath];NSError *error = nil;AVAudioPlayer *_player = [[AVAudioPlayer alloc] initWithData:dataAudio error:&error];//属性设置[_player prepareToPlay]; //准备播放[_player play]; //播放[_player pause]; //暂停播放[_player stop]; //停止播放_player.duration; //播放持续时间,只读_player.volume = 0.8; //设置音量大小_player.currentTime = 15.0; //设置当前播放时间_player.numberOfLoops = 3; //循环播放时间_player.delegate = self; //委托事件// AVAudioPlayer委托事件- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ //音频文件播放结束}- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{ //音频文件解码错误}
注:录音类AVAudioRecorder最好设置为全局变量。如果为局部变量,当销毁掉时将结束录音
附:据此写出的仿微信录音框架:点击打开链接
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
实例编程iPhone 录音和播放
iOS录音功能的实现
[转载]音频视频
iOS音频播放 (七):播放iPod Library中的歌曲
iphone AVAudioRecorder进行录音
iOS获取当前app版本
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服