void ExposureModeChanged (NSObservedChange obj)
{
var newValue = obj.NewValue;
var oldValue = obj.OldValue;
if (newValue != null && newValue != NSNull.Null) {
var newMode = (AVCaptureExposureMode)newValue.AsInt ();
if (oldValue != null && oldValue != NSNull.Null) {
var oldMode = (AVCaptureExposureMode)oldValue.AsInt ();
// It’s important to understand the relationship between ExposureDuration and the minimum frame rate as represented by ActiveVideoMaxFrameDuration.
// In manual mode, if ExposureDuration is set to a value that's greater than ActiveVideoMaxFrameDuration, then ActiveVideoMaxFrameDuration will
// increase to match it, thus lowering the minimum frame rate. If ExposureMode is then changed to automatic mode, the minimum frame rate will
// remain lower than its default. If this is not the desired behavior, the min and max frameRates can be reset to their default values for the
// current ActiveFormat by setting ActiveVideoMaxFrameDuration and ActiveVideoMinFrameDuration to CMTime.Invalid.
if (oldMode != newMode && oldMode == AVCaptureExposureMode.Custom) {
NSError error = null;
if (VideoDevice.LockForConfiguration (out error)) {
VideoDevice.ActiveVideoMaxFrameDuration = CMTime.Invalid;
VideoDevice.ActiveVideoMinFrameDuration = CMTime.Invalid;
VideoDevice.UnlockForConfiguration ();
} else {
Console.WriteLine ($"Could not lock device for configuration: {error}");
}
}
}
DispatchQueue.MainQueue.DispatchAsync (() => {
ExposureModeControl.SelectedSegment = Array.IndexOf (exposureModes, newMode);
ExposureDurationSlider.Enabled = (newMode == AVCaptureExposureMode.Custom);
ISOSlider.Enabled = (newMode == AVCaptureExposureMode.Custom);
if (oldValue != null && oldValue != NSNull.Null) {
var oldMode = (AVCaptureExposureMode)oldValue.AsInt ();
Console.WriteLine ($"exposure mode: {StringFromExposureMode (oldMode)} -> {StringFromExposureMode (newMode)}");
} else {
Console.WriteLine ($"exposure mode: {StringFromExposureMode (newMode)}");
}
});
}
}