IOS中錄音後再播放聲音太小問題解決
- (BOOL)canRecord
{
__block BOOL bCanRecord = YES;
if ([[[UIDevicecurrentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)
{
AVAudioSession *audioSession = [AVAudioSessionsharedInstance];
if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
[audioSession performSelector
if (granted) {
bCanRecord = YES;
} else {
bCanRecord = NO;
}
}];
}
}
return bCanRecord;
}
-(void)initRecordSession
{
AVAudioSession *session = [AVAudioSessionsharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];
[session setActive:YES error:nil];
}
-(void)onRecordSoundStop:(UIButton *)sender
{
AVAudioSession *session = [AVAudioSessionsharedInstance];
[session setCategory:
[session setActive:YES error:nil];
[_timerRec invalidate];
if (_recorder.currentTime > 1)
{
[_recorderstop];
PlayNodeData *model = _dataOfVideoArrary[sender.tag];
model.hasSound = YES;
[_btnPlay setImage:[UIImage imageNamed:@"simulate_image_play1"] forState:UIControlStateNormal];
}
}
//
-(void)onRecordSoundStart:(UIButton *)sender
{
if (![self canRecord])
{
[[[UIAlertViewalloc] initWithTitle:nil
message:[NSString stringWithFormat:@"應用需要訪問您的麥克風。\n請啟用麥克風-設定/隱私/麥克風"]
delegate:nil
cancelButtonTitle:@"好"
otherButtonTitles:nil] show];
return;
}
[selfinitRecordSession];
NSDictionary *settings = [[NSDictionaryalloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat:44100.0],AVSampleRateKey , //取樣率 8000/44100/96000
[NSNumber numberWithInt:kAudioFormatMPEG4AAC],AVFormatIDKey, //錄音格式
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey, //線性取樣位數 8、16、24、32
[NSNumber numberWithInt:2],AVNumberOfChannelsKey, //聲道 1,2
[NSNumbernumberWithInt:AVAudioQualityHigh],AVEncoderAudioQualityKey, //錄音質量
nil];
NSURL *strURL = [NSURLfileURLWithPath:[selfGetRecordSoundFileName:sender.tag]];
_recorder = [[AVAudioRecorderalloc] initWithURL:strURL settings:settings error:nil];
_recorder.meteringEnabled = YES;
_recorder.delegate = self;
[_recorder prepareToRecord];
[_recorder record];
_timerRec = [NSTimerscheduledTimerWithTimeInterval:0 target:selfselector:@selector(detectionVoice) userInfo:nilrepeats:YES];
}
-(void)detectionVoice
{
return;
[_recorder updateMeters];//重新整理音量資料
//獲取音量的平均值 [recorder averagePowerForChannel:0];
//音量的最大值 [recorder peakPowerForChannel:0];
double lowPassResults = pow(10, (0.05 * [_recorder peakPowerForChannel:0]));
NSLog(@"%lf",lowPassResults);
//最大50 0
//圖片 小-》大
if (0<lowPassResults<=0.06) {
;
}else if (0.06<lowPassResults<=0.13) {
;
}else if (0.13<lowPassResults<=0.20) {
;
}else if (0.20<lowPassResults<=0.27) {
;
}
}