5

I've looked everywhere, and I can't find a way to adjust the input volume of an input device for an AVCaptureSession. The best I can do, which doesn't help me at all, is get the levels of audio from the device by accessing the connections (AVCaptureConnections) from the AVCaptureAudioDataOutput - i.e., by monitoring the levels from a preview output. Is there any way to change the input gain, or even to get the audio input level directly in AVFoundation? I'm still learning a bit, so I apologize if I have missed anything obvious.

Edit: I should note that this is for OSX.

4

3 回答 3

2

So, I used some info that He Was linked, along with some research into Core Audio, and I turned it into a github project. That way, other people wanting to change the input volume of a device while using AVFoundation don't have to reinvent the wheel. You can find the classes here: https://github.com/ewrobinson/ERVolumeAdjust

于 2013-05-25T01:06:04.310 回答
1
self.audioSession = [AVAudioSession sharedInstance];
if (self.audioSession.isInputGainSettable) {
    BOOL success = [self.audioSession setInputGain:sender.value error:&error];
    if (!success) NSLog(@"inputGain error: %@",error);
}

This is for ios6+ ... for ios5, you can use AudioSessionGetProperty / AudioSessionSetProperty functions to achieve the same.

However not all hardware is capable of adjustable input gain. For example, I get gain settable on an iPhone 3GS/ios6 with builtin mic, but gain NOT settable on an iPad mini with builtin mic. On the iPhone 3GS gain is also not settable until recording has actually started. (edit: iPhone 4S/ios5 also has gain settable with builtin mic)

update
As you have pointed out, you are looking for an OSX solution, not iOS. I have moved this answer (with more detail) to a better place.

For OSX, you may find this Q&A useful:

How do you set the input level (gain) on the built-in input (OSX Core Audio / Audio Unit)?

于 2013-05-07T02:57:33.090 回答
1

You can adjust the gain by setting the volume property of your session output's audio connection.
Take into account that the connection might have multiple channels.

I am assuming a reference to a fileOutput ivar here, that I stored before adding the output to the session.

AVCaptureConnection* audioConnection = [fileOutput connectionWithMediaType:AVMediaTypeAudio];
if(audioConnection)
{
    for(AVCaptureAudioChannel* audioChannel in [audioConnection audioChannels])
    {
        audioChannel.volume = 0.5;
    }
}
于 2013-05-07T07:01:31.973 回答