iOS 录音及播放 音波图波形
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 录音及播放 音波图波形相关的知识,希望对你有一定的参考价值。
参考技术A #pragma mark -录音设置配置- (void)initRecordConfig
//录音设置
NSMutableDictionary*recordSetting = [[NSMutableDictionaryalloc]init];
//设置录音格式AVFormatIDKey==kAudioFormatLinearPCM
[recordSettingsetValue:[NSNumbernumberWithInt:kAudioFormatLinearPCM]forKey:AVFormatIDKey];
//设置录音采样率(Hz)如:AVSampleRateKey==8000/44100/96000(影响音频的质量),采样率必须要设为11025才能使转化成mp3格式后不会失真
[recordSettingsetValue:[NSNumbernumberWithFloat:11025.0]forKey:AVSampleRateKey];
//录音通道数1或2,要转换成mp3格式必须为双通道
[recordSettingsetValue:[NSNumbernumberWithInt:2]forKey:AVNumberOfChannelsKey];
//线性采样位数8、16、24、32
[recordSettingsetValue:[NSNumbernumberWithInt:16]forKey:AVLinearPCMBitDepthKey];
//录音的质量
[recordSettingsetValue:[NSNumbernumberWithInt:AVAudioQualityHigh]forKey:AVEncoderAudioQualityKey];
//存储录音文件
_recordUrl= [NSURLURLWithString:[NSTemporaryDirectory()stringByAppendingString:@"selfRecord.caf"]];
//初始化
_audioRecorder= [[AVAudioRecorderalloc]initWithURL:_recordUrlsettings:recordSettingerror:nil];
//开启音量检测
_audioRecorder.meteringEnabled=YES;
_audioRecorder.delegate=self;
录音长按按钮
#pragma mark- UILongPressGestureRecognizer
//添加手势操作,长按按钮
- (void)handSpeakBtnPressed:(UILongPressGestureRecognizer*)gestureRecognizer
UIView*view= gestureRecognizer.view;
UIImageView*imageView = (UIImageView*)view.superview;
if(gestureRecognizer.state==UIGestureRecognizerStateBegan)
if(self.typeIndex==2||self.typeIndex==4)
[selfplayVoiceBtnAction];
return;
[imageViewstartAnimating];
[selfstartRecordVoice];
self.remindLabel.text=@"松开结束";
if(gestureRecognizer.state==UIGestureRecognizerStateEnded)
[imageViewstopAnimating];
[selfstopRecordVoice];
self.remindLabel.text=@"按住说话";
#pragma mark --------开始录音
- (void)startRecordVoice
[self.sendkBtnsetUserInteractionEnabled:NO];
_audiosession= [AVAudioSessionsharedInstance];//得到AVAudioSession单例对象
[self.audioSessionsetCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];//设置类别,表示该应用同时支持播放和录音
[self.audioSessionsetActive:YESerror:nil];//启动音频会话管理,此时会阻断后台音乐的播放.
[self.audioRecorderprepareToRecord];
[self.audioRecorderpeakPowerForChannel:1.0];
[self.audioRecorderrecord];
self.second=0;
self.timer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(timeAnim)userInfo:nilrepeats:YES];
#pragma mark -定时器
- (void)timeAnim
if(self.second>=TOTAL_SECOND)
[selfstopRecordVoice];
[self.timerinvalidate];
self.second++;
if(self.second<10)
self.timeLabel.textColor= [UIColorcolorWithHexString:@"818C9E"];
self.timeLabel.text= [NSStringstringWithFormat:@"0:0%ld",(long)self.second];
else
self.timeLabel.textColor= [UIColorcolorWithHexString:@"818C9E"];
self.timeLabel.text= [NSStringstringWithFormat:@"0:%ld",(long)self.second];
#pragma mark --------停止录音
- (void)stopRecordVoice
if(self.typeIndex!=1)
return;
[self.recordBtnImageViewstopAnimating];
if(self.second<5)
self.timeLabel.text=@"0:00";
[self.timerinvalidate];
[self.audioRecorderstop];//录音停止
[self.audioSessionsetActive:NOerror:nil];//一定要在录音停止以后再关闭音频会话管理(否则会报错),此时会延续后台音乐播放
self.timeLabel.textColor= [UIColorcolorWithHexString:@"E6E8EB"];
[MBProgressHUDshowError:@"说话时间不得低于5秒"toView:self.view];
else
[selftransformCAFToMP3];
[self.timerinvalidate];
[self.audioRecorderstop];//录音停止
[self.audioSessionsetCategory:AVAudioSessionCategoryPlaybackerror:nil];//此处需要恢复设置回放标志,否则会导致其它播放声音也会变小
[self.audioSessionsetActive:NOerror:nil];//一定要在录音停止以后再关闭音频会话管理(否则会报错),此时会延续后台音乐播放
if(self.playerView==nil)
NSString*videoStr = [NSTemporaryDirectory()stringByAppendingPathComponent:@"myselfRecord.mp3"];
NSURL*fileURL = [NSURLfileURLWithPath:videoStr];
AVURLAsset*asset = [AVURLAssetURLAssetWithURL:fileURLoptions:nil];
//录音完的时候调用,显示音波图
self.playerView= [[SYWaveformPlayerViewalloc]initWithFrame:CGRectMake(0,116,self.view.frame.size.width,82)asset:assetcolor:[UIColorlightGrayColor]progressColor:[UIColorcolorWithHexString:@"818C9E"]];
[self.WaveformViewaddSubview:self.playerView];
self.timeLabel.font= [UIFontsystemFontOfSize:32];
[self.timeLabelsetFont:[UIFontfontWithName:@"Helvetica-Bold"size:32]];
self.timeLabel.frame=CGRectMake(0,221,WIDTH,32);
self.remindLabel.hidden=YES;
self.typeIndex=2;
self.recordBtnImageView.image=kImageWithName(@"btn_recording_play");
//基于现有的一个值,再进行平移
self.sendkBtn.hidden=NO;
//基于现有的一个值,再进行平移
self.delectVoiceBtn.hidden=NO;
self.delectVoiceBtn.alpha=0;
self.sendkBtn.alpha=0;
[UIViewanimateWithDuration:0.5animations:^
self.delectVoiceBtn.transform=CGAffineTransformTranslate(self.delectVoiceBtn.transform, -(WIDTH/2-55),0);
self.sendkBtn.transform=CGAffineTransformTranslate(self.sendkBtn.transform,WIDTH/2-55,0);
self.delectVoiceBtn.alpha=1;
self.sendkBtn.alpha=1;
];
音波图的库 http://www.open-open.com/lib/view/home/1422534470439
基于Swift的iOS应用程序开发:录音及播放声音
本文所涉及内容适合在SwiftUI架构下使用。文末提供有完整的封装好的类。
欢迎给我留言,或是写邮件给我:
randolph.carter.xu@gmail.com
28300098@qq.com
一、声明
首先声明文件的保存路径及音频文件的后缀名:
public let PATH_DOC = "/Documents";
public let AUDIO_SUFFIX = "m4a";
然后是与录音与播放有关的一些类:
//录音器、会话和播放器
public var audioRecorder:AVAudioRecorder!;
public var audioSession:AVAudioSession!;
public var audioPlayer:AVAudioPlayer!;
这三个类都存在于AVKit中:
import AVKit
二、权限
在XCode的Info配置中添加对于麦克风的使用权限:
Privacy - Microphone Usage Description
在代码中,我们需要在创建AVAudioSession的时候来判断权限是否已经获取到了:
以上是关于iOS 录音及播放 音波图波形的主要内容,如果未能解决你的问题,请参考以下文章