void ChangeCamera(NSObject sender) { //CameraButton.Enabled = false; //RecordButton.Enabled = false; //PhotoButton.Enabled = false; //LivePhotoModeButton.Enabled = false; //CaptureModeControl.Enabled = false; sessionQueue.DispatchAsync(() => { var currentVideoDevice = videoDeviceInput.Device; var currentPosition = currentVideoDevice.Position; AVCaptureDevicePosition preferredPosition = AVCaptureDevicePosition.Unspecified; AVCaptureDeviceType preferredDeviceType = AVCaptureDeviceType.BuiltInDualCamera; switch (currentPosition) { //case AVCaptureDevicePosition.Unspecified: //preferredPosition = AVCaptureDevicePosition.Back; //preferredDeviceType = AVCaptureDeviceType.BuiltInDualCamera; //break; case AVCaptureDevicePosition.Unspecified: case AVCaptureDevicePosition.Front: preferredPosition = AVCaptureDevicePosition.Back; preferredDeviceType = AVCaptureDeviceType.BuiltInDualCamera; break; case AVCaptureDevicePosition.Back: preferredPosition = AVCaptureDevicePosition.Front; preferredDeviceType = AVCaptureDeviceType.BuiltInWideAngleCamera; break; } var devices = videoDeviceDiscoverySession.Devices; AVCaptureDevice newVideoDevice = null; // First, look for a device with both the preferred position and device type. foreach (var device in devices) { if (device.Position == preferredPosition && device.DeviceType.GetConstant() == preferredDeviceType.GetConstant()) { newVideoDevice = device; break; } } // Otherwise, look for a device with only the preferred position. if (newVideoDevice == null) { foreach (var device in devices) { if (device.Position == preferredPosition) { newVideoDevice = device; break; } } } if (newVideoDevice != null) { var lVideoDeviceInput = AVCaptureDeviceInput.FromDevice(newVideoDevice); session.BeginConfiguration(); // Remove the existing device input first, since using the front and back camera simultaneously is not supported. session.RemoveInput(videoDeviceInput); if (session.CanAddInput(lVideoDeviceInput)) { if (subjectAreaDidChangeObserver != null) { subjectAreaDidChangeObserver.Dispose(); } subjectAreaDidChangeObserver = NSNotificationCenter.DefaultCenter.AddObserver(AVCaptureDevice.SubjectAreaDidChangeNotification, SubjectAreaDidChange, newVideoDevice); session.AddInput(lVideoDeviceInput); videoDeviceInput = lVideoDeviceInput; } else { session.AddInput(videoDeviceInput); } if (movieFileOutput != null) { var movieFileOutputConnection = movieFileOutput.ConnectionFromMediaType(AVMediaType.Video); if (movieFileOutputConnection.SupportsVideoStabilization) { movieFileOutputConnection.PreferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.Auto; } } /* * Set Live Photo capture and depth data delivery if it is supported. When changing cameras, the * `livePhotoCaptureEnabled` and `depthDataDeliveryEnabled` properties of the AVCapturePhotoOutput gets set to NO when * a video device is disconnected from the session. After the new video device is * added to the session, re-enable Live Photo capture and depth data delivery if they are supported. */ photoOutput.IsLivePhotoCaptureEnabled = photoOutput.IsLivePhotoCaptureSupported; //photoOutput.IsDepthDataDeliveryEnabled(photoOutput.IsDepthDataDeliverySupported()); session.CommitConfiguration(); } //DispatchQueue.MainQueue.DispatchAsync(() => //{ // CameraButton.Enabled = true; // RecordButton.Enabled = CaptureModeControl.SelectedSegment == (int)AVCamCaptureMode.Movie; // PhotoButton.Enabled = true; // LivePhotoModeButton.Enabled = true; // CaptureModeControl.Enabled = true; // DepthDataDeliveryButton.Enabled = photoOutput.IsDepthDataDeliveryEnabled(); // DepthDataDeliveryButton.Hidden = !photoOutput.IsDepthDataDeliverySupported(); //}); }); }
static bool TryGetDefaultVideoCamera (AVCaptureDeviceType type, AVCaptureDevicePosition position, out AVCaptureDevice device) { device = AVCaptureDevice.GetDefaultDevice (type, AVMediaType.Video, position); return device != null; }
static bool TryGetDefaultVideoCamera(AVCaptureDeviceType type, AVCaptureDevicePosition position, out AVCaptureDevice device) { device = AVCaptureDevice.GetDefaultDevice(type, AVMediaType.Video, position); return(device != null); }
void ChangeCamera(UIButton cameraButton) { cameraButton.Enabled = false; RecordButton.Enabled = false; PhotoButton.Enabled = false; LivePhotoModeButton.Enabled = false; CaptureModeControl.Enabled = false; sessionQueue.DispatchAsync(() => { AVCaptureDevice currentVideoDevice = videoDeviceInput.Device; AVCaptureDevicePosition currentPosition = currentVideoDevice.Position; AVCaptureDevicePosition preferredPosition = 0; AVCaptureDeviceType preferredDeviceType = 0; switch (currentPosition) { case AVCaptureDevicePosition.Unspecified: case AVCaptureDevicePosition.Front: preferredPosition = AVCaptureDevicePosition.Back; preferredDeviceType = AVCaptureDeviceType.BuiltInDuoCamera; break; case AVCaptureDevicePosition.Back: preferredPosition = AVCaptureDevicePosition.Front; preferredDeviceType = AVCaptureDeviceType.BuiltInWideAngleCamera; break; } var devices = videoDeviceDiscoverySession.Devices; AVCaptureDevice newVideoDevice = null; // First, look for a device with both the preferred position and device type. Otherwise, look for a device with only the preferred position. newVideoDevice = devices.FirstOrDefault(d => d.Position == preferredPosition && d.DeviceType == preferredDeviceType) ?? devices.FirstOrDefault(d => d.Position == preferredPosition); if (newVideoDevice != null) { NSError error; var input = AVCaptureDeviceInput.FromDevice(newVideoDevice, out error); if (error == null) { session.BeginConfiguration(); // Remove the existing device input first, since using the front and back camera simultaneously is not supported. session.RemoveInput(videoDeviceInput); if (session.CanAddInput(input)) { subjectSubscriber?.Dispose(); subjectSubscriber = NSNotificationCenter.DefaultCenter.AddObserver(AVCaptureDevice.SubjectAreaDidChangeNotification, SubjectAreaDidChange, input.Device); session.AddInput(input); videoDeviceInput = input; } else { session.AddInput(videoDeviceInput); } var connection = MovieFileOutput?.ConnectionFromMediaType(AVMediaType.Video); if (connection != null) { if (connection.SupportsVideoStabilization) { connection.PreferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.Auto; } } // Set Live Photo capture enabled if it is supported.When changing cameras, the // IsLivePhotoCaptureEnabled property of the AVCapturePhotoOutput gets set to false when // a video device is disconnected from the session.After the new video device is // added to the session, re - enable Live Photo capture on the AVCapturePhotoOutput if it is supported. photoOutput.IsLivePhotoCaptureEnabled = photoOutput.IsLivePhotoCaptureSupported; session.CommitConfiguration(); } } DispatchQueue.MainQueue.DispatchAsync(() => { CameraButton.Enabled = true; RecordButton.Enabled = MovieFileOutput != null; PhotoButton.Enabled = true; LivePhotoModeButton.Enabled = true; CaptureModeControl.Enabled = true; }); }); }