//
|
// AudioSessionHelper.m
|
// VideoPhone
|
//
|
// Created by eTouchSky on 2019/5/30.
|
// Copyright © 2019 eTouchSky. All rights reserved.
|
//
|
|
#import "AudioSessionHelper.h"
|
#import <AVFoundation/AVFoundation.h>
|
|
@interface AudioSessionHelper()
|
|
@property (nonatomic,assign) BOOL speaking;
|
|
@end
|
|
@implementation AudioSessionHelper
|
|
#pragma mark -- AVAudioSession的设置
|
-(void)setAudioSession{
|
NSError *error = nil;
|
//免提播放 有回声: 网络引起回音(两个手机隔太近就会很大)。 手机本身的设计有问题(录播),关闭降噪功能(关闭后基本没有了)
|
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
|
if (error) {
|
NSLog(@"AVAudioSessionCategoryPlayAndRecord error:%@", error);
|
}else{
|
NSLog(@"听筒播放");
|
}
|
|
[[AVAudioSession sharedInstance] setActive:YES withOptions:kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation error:&error];
|
|
[[AVAudioSession sharedInstance] setPreferredIOBufferDuration:0.05 error:&error];
|
if (error) {
|
NSLog(@"AVAudioSessionSetPreferredIOBufferDuration error:%@", error);
|
}
|
|
/**< 码率设置不正确会导致播放出来的声音变尖或变平,有一点拖音或者最后一点音少了,8000的采样率设置16000的码率很好>*/
|
[[AVAudioSession sharedInstance] setPreferredSampleRate:16000 error:&error];
|
if (error) {
|
NSLog(@"setPreferredSampleRate error:%@", error);
|
}
|
}
|
|
|
//必须先setActive:NO 再设置option 再setActive:YES, 否则有些系统,有些畸形,在听筒模式下不能采集
|
//这里打断点测试可能会报错:p2p终端,解码器终端。
|
-(NSString *)speaker:(BOOL)isSpeaking{
|
|
NSError *error = nil;
|
|
[[AVAudioSession sharedInstance] setActive:NO error:&error];
|
if (error) {
|
NSLog(@"setActive:NO error:%@", error);
|
}
|
if (isSpeaking == NO) {
|
//听筒播放 效果很好
|
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
|
if (error) {
|
NSLog(@"AVAudioSessionCategoryPlayAndRecord error:%@", error);
|
return nil;
|
}else{
|
NSLog(@"听筒播放");
|
[[AVAudioSession sharedInstance] setActive:YES withOptions:kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation error:&error];
|
if (error) {
|
NSLog(@"setActive:YES error:%@", error);
|
}
|
|
[self LogAudioSessionInfo];
|
|
return @"免提";
|
}
|
}else{
|
//免提播放 有回声: 网络引起回音(两个手机隔太近就会很大)。 手机本身的设计有问题(录播),关闭降噪功能(关闭后基本没有了)
|
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&error];
|
if (error) {
|
NSLog(@"AVAudioSessionCategoryOptionDefaultToSpeaker error:%@", error);
|
return nil;
|
}else{
|
NSLog(@"免提播放");
|
[[AVAudioSession sharedInstance] setActive:YES withOptions:kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation error:&error];
|
if (error) {
|
NSLog(@"setActive:YES error:%@", error);
|
}
|
|
//[self LogAudioSessionInfo];
|
|
return @"听筒";
|
}
|
}
|
}
|
|
-(void)LogAudioSessionInfo{
|
AVAudioSessionCategory category = [[AVAudioSession sharedInstance] category];
|
AVAudioSessionCategoryOptions option = [[AVAudioSession sharedInstance] categoryOptions];
|
NSArray* inputArray = [[AVAudioSession sharedInstance] availableInputs];
|
NSArray* outputArray = [AVAudioSession sharedInstance].currentRoute.outputs;
|
NSLog(@"%@", category);
|
NSLog(@"%lu", (unsigned long)option);
|
NSLog(@"%@", inputArray);
|
NSLog(@"%@", outputArray);
|
}
|
|
/*
|
-(void)changeMicToHeadset{
|
NSArray* inputArray = [[AVAudioSession sharedInstance] availableInputs];
|
for (AVAudioSessionPortDescription* desc in inputArray) {
|
if ([desc.portType isEqualToString:AVAudioSessionPortHeadsetMic]) {
|
NSError* error;
|
[[AVAudioSession sharedInstance] setPreferredInput:desc error:&error];
|
}
|
}
|
}
|
-(void)changeMicToBuiltIn{
|
NSArray* inputArray = [[AVAudioSession sharedInstance] availableInputs];
|
for (AVAudioSessionPortDescription* desc in inputArray) {
|
if ([desc.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
|
NSError* error;
|
[[AVAudioSession sharedInstance] setPreferredInput:desc error:&error];
|
}
|
}
|
}
|
*/
|
@end
|