Example #1
0
        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();
                //});
            });
        }