JLChen
2020-07-10 ea7dfe09eaed6edbebca1d2d89a135fc0c81695e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
//  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