IOS中录音后再播放声音太小问题解决
2014-07-02 11:05
127 查看
- (BOOL)canRecord
{
__block BOOL bCanRecord =
YES;
if ([[[UIDevice
currentDevice] systemVersion]
compare:@"7.0"] !=
NSOrderedAscending)
{
AVAudioSession *audioSession = [AVAudioSession
sharedInstance];
if ([audioSession
respondsToSelector:@selector(requestRecordPermission:)]) {
[audioSession
performSelector:@selector(requestRecordPermission:)
withObject:^(BOOL granted) {
if (granted) {
bCanRecord =
YES;
}
else {
bCanRecord =
NO;
}
}];
}
}
return bCanRecord;
}
-(void)initRecordSession
{
AVAudioSession *session = [AVAudioSession
sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord
error:nil];
[session
setActive:YES
error:nil];
}
-(void)onRecordSoundStop:(UIButton *)sender
{
AVAudioSession *session = [AVAudioSession
sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback
error:nil]; //此处需要恢复设置回放标志,否则会导致其它播放声音也会变小
[session
setActive:YES
error:nil];
[_timerRec
invalidate];
if (_recorder.currentTime > 1)
{
[_recorder
stop];
PlayNodeData *model =
_dataOfVideoArrary[sender.tag];
model.hasSound =
YES;
[_btnPlay
setImage:[UIImage
imageNamed:@"simulate_image_play1"]
forState:UIControlStateNormal];
}
}
//
-(void)onRecordSoundStart:(UIButton *)sender
{
if (![self
canRecord])
{
[[[UIAlertView
alloc] initWithTitle:nil
message:[NSString
stringWithFormat:@"应用需要访问您的麦克风。\n请启用麦克风-设置/隐私/麦克风"]
delegate:nil
cancelButtonTitle:@"好"
otherButtonTitles:nil]
show];
return;
}
[self
initRecordSession];
NSDictionary *settings = [[NSDictionary
alloc] 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
[NSNumber
numberWithInt:AVAudioQualityHigh],AVEncoderAudioQualityKey,
//录音质量
nil];
NSURL *strURL = [NSURL
fileURLWithPath:[self
GetRecordSoundFileName:sender.tag]];
_recorder = [[AVAudioRecorder
alloc] initWithURL:strURL
settings:settings error:nil];
_recorder.meteringEnabled =
YES;
_recorder.delegate =
self;
[_recorder
prepareToRecord];
[_recorder
record];
_timerRec = [NSTimer
scheduledTimerWithTimeInterval:0 target:self
selector:@selector(detectionVoice)
userInfo:nil
repeats: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) {
;
}
}
{
__block BOOL bCanRecord =
YES;
if ([[[UIDevice
currentDevice] systemVersion]
compare:@"7.0"] !=
NSOrderedAscending)
{
AVAudioSession *audioSession = [AVAudioSession
sharedInstance];
if ([audioSession
respondsToSelector:@selector(requestRecordPermission:)]) {
[audioSession
performSelector:@selector(requestRecordPermission:)
withObject:^(BOOL granted) {
if (granted) {
bCanRecord =
YES;
}
else {
bCanRecord =
NO;
}
}];
}
}
return bCanRecord;
}
-(void)initRecordSession
{
AVAudioSession *session = [AVAudioSession
sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord
error:nil];
[session
setActive:YES
error:nil];
}
-(void)onRecordSoundStop:(UIButton *)sender
{
AVAudioSession *session = [AVAudioSession
sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback
error:nil]; //此处需要恢复设置回放标志,否则会导致其它播放声音也会变小
[session
setActive:YES
error:nil];
[_timerRec
invalidate];
if (_recorder.currentTime > 1)
{
[_recorder
stop];
PlayNodeData *model =
_dataOfVideoArrary[sender.tag];
model.hasSound =
YES;
[_btnPlay
setImage:[UIImage
imageNamed:@"simulate_image_play1"]
forState:UIControlStateNormal];
}
}
//
-(void)onRecordSoundStart:(UIButton *)sender
{
if (![self
canRecord])
{
[[[UIAlertView
alloc] initWithTitle:nil
message:[NSString
stringWithFormat:@"应用需要访问您的麦克风。\n请启用麦克风-设置/隐私/麦克风"]
delegate:nil
cancelButtonTitle:@"好"
otherButtonTitles:nil]
show];
return;
}
[self
initRecordSession];
NSDictionary *settings = [[NSDictionary
alloc] 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
[NSNumber
numberWithInt:AVAudioQualityHigh],AVEncoderAudioQualityKey,
//录音质量
nil];
NSURL *strURL = [NSURL
fileURLWithPath:[self
GetRecordSoundFileName:sender.tag]];
_recorder = [[AVAudioRecorder
alloc] initWithURL:strURL
settings:settings error:nil];
_recorder.meteringEnabled =
YES;
_recorder.delegate =
self;
[_recorder
prepareToRecord];
[_recorder
record];
_timerRec = [NSTimer
scheduledTimerWithTimeInterval:0 target:self
selector:@selector(detectionVoice)
userInfo:nil
repeats: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) {
;
}
}
相关文章推荐
- IOS中录音后再播放声音太小问题解决
- 小程序ios音频播放没声音问题的解决
- ios之audio unit的录音和播放一起,解决audioqueue播放PCM延迟问题
- 解决android录音文件传到ios端,ios播放失败问题
- iOS 使用AudioRecorder录音后在使用AVPlayer播放不了声音的问题
- 关于iOS录音后播放声音小的问题
- ios 同时录音和播放问题
- 【iOS-Cocos2d游戏开发之七】在cocos2d中添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题!更新解决添加组件Cocos2d动画停止播放的BUG】
- 【iOS-Cocos2d游戏开发之七】在cocos2d中添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题!【11月28日更新解决添加组件Cocos2d动画停止播放的BUG】
- 【iOS-Cocos2d游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)/动画播放出现毛边以及禁止游戏中自动锁屏问题!【2011年12月18日补充】
- iOS设备播放mkv格式视频无声音解决
- 解决win7出现其他声音出现播放的音乐就变小问题
- 简单的Android音乐播放,解决incall类型不能通过音量按键控制声音大小的问题
- 解决Ubuntu9.10下播放WMV没声音问题
- 在后台有音乐播放的情况下用AVAudioRecorder进行录音问题的解决
- IOS 录音的时候 监测声音大小并显示出来当前声音大小 可播放(超级简陋版)
- iphone ios AVAudioRecorder 录音后用avplayer播放声音小
- 解决mini2440声卡全双工问题,实现同时录音及播放
- 【iOS-Cocos2d游戏开发之十八】解决滚屏背景/拼接地图有黑边(缝隙)/动画播放出现毛边以及禁止游戏中自动锁屏问题!【2011年12月18日补充】
- iPhone 录音和播放(解决录音延迟问题)