iOS 錄音及播放 音波圖波形

#pragma mark -錄音設(shè)置配置

- (void)initRecordConfig{

//錄音設(shè)置

NSMutableDictionary*recordSetting = [[NSMutableDictionaryalloc]init];

//設(shè)置錄音格式AVFormatIDKey==kAudioFormatLinearPCM

[recordSettingsetValue:[NSNumbernumberWithInt:kAudioFormatLinearPCM]forKey:AVFormatIDKey];

//設(shè)置錄音采樣率(Hz)如:AVSampleRateKey==8000/44100/96000(影響音頻的質(zhì)量),采樣率必須要設(shè)為11025才能使轉(zhuǎn)化成mp3格式后不會(huì)失真

[recordSettingsetValue:[NSNumbernumberWithFloat:11025.0]forKey:AVSampleRateKey];

//錄音通道數(shù)1或2,要轉(zhuǎn)換成mp3格式必須為雙通道

[recordSettingsetValue:[NSNumbernumberWithInt:2]forKey:AVNumberOfChannelsKey];

//線性采樣位數(shù)8、16、24、32

[recordSettingsetValue:[NSNumbernumberWithInt:16]forKey:AVLinearPCMBitDepthKey];

//錄音的質(zhì)量

[recordSettingsetValue:[NSNumbernumberWithInt:AVAudioQualityHigh]forKey:AVEncoderAudioQualityKey];

//存儲(chǔ)錄音文件

_recordUrl= [NSURLURLWithString:[NSTemporaryDirectory()stringByAppendingString:@"selfRecord.caf"]];

//初始化

_audioRecorder= [[AVAudioRecorderalloc]initWithURL:_recordUrlsettings:recordSettingerror:nil];

//開(kāi)啟音量檢測(cè)

_audioRecorder.meteringEnabled=YES;

_audioRecorder.delegate=self;

}

錄音長(zhǎng)按按鈕

#pragma mark- UILongPressGestureRecognizer

//添加手勢(shì)操作,長(zhǎng)按按鈕

- (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=@"松開(kāi)結(jié)束";

}

if(gestureRecognizer.state==UIGestureRecognizerStateEnded) {

[imageViewstopAnimating];

[selfstopRecordVoice];

self.remindLabel.text=@"按住說(shuō)話";

}

}

#pragma mark --------開(kāi)始錄音

- (void)startRecordVoice{

[self.sendkBtnsetUserInteractionEnabled:NO];

_audioSession= [AVAudioSessionsharedInstance];//得到AVAudioSession單例對(duì)象

[self.audioSessionsetCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];//設(shè)置類別,表示該應(yīng)用同時(shí)支持播放和錄音

[self.audioSessionsetActive:YESerror:nil];//啟動(dòng)音頻會(huì)話管理,此時(shí)會(huì)阻斷后臺(tái)音樂(lè)的播放.

[self.audioRecorderprepareToRecord];

[self.audioRecorderpeakPowerForChannel:1.0];

[self.audioRecorderrecord];

self.second=0;

self.timer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(timeAnim)userInfo:nilrepeats:YES];

}

#pragma mark -定時(shí)器

- (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];//一定要在錄音停止以后再關(guān)閉音頻會(huì)話管理(否則會(huì)報(bào)錯(cuò)),此時(shí)會(huì)延續(xù)后臺(tái)音樂(lè)播放

self.timeLabel.textColor= [UIColorcolorWithHexString:@"E6E8EB"];

[MBProgressHUDshowError:@"說(shuō)話時(shí)間不得低于5秒"toView:self.view];

}else{

[selftransformCAFToMP3];

[self.timerinvalidate];

[self.audioRecorderstop];//錄音停止

[self.audioSessionsetCategory:AVAudioSessionCategoryPlaybackerror:nil];//此處需要恢復(fù)設(shè)置回放標(biāo)志,否則會(huì)導(dǎo)致其它播放聲音也會(huì)變小

[self.audioSessionsetActive:NOerror:nil];//一定要在錄音停止以后再關(guān)閉音頻會(huì)話管理(否則會(huì)報(bào)錯(cuò)),此時(shí)會(huì)延續(xù)后臺(tái)音樂(lè)播放

if(self.playerView==nil){

NSString*videoStr = [NSTemporaryDirectory()stringByAppendingPathComponent:@"myselfRecord.mp3"];

NSURL*fileURL = [NSURLfileURLWithPath:videoStr];

AVURLAsset*asset = [AVURLAssetURLAssetWithURL:fileURLoptions:nil];

//錄音完的時(shí)候調(diào)用,顯示音波圖

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");

//基于現(xiàn)有的一個(gè)值,再進(jìn)行平移

self.sendkBtn.hidden=NO;

//基于現(xiàn)有的一個(gè)值,再進(jìn)行平移

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;

}];

}

}

音波圖的庫(kù)http://www.open-open.com/lib/view/home/1422534470439

最后編輯于
?著作權(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)容