Ejemplo n.º 1
0
        void ChangeCamera(CameraViewController sender)
        {
            CameraButton.Enabled = false;
            RecordButton.Enabled = false;
            StillButton.Enabled  = false;

            SessionQueue.DispatchAsync(() => {
                AVCaptureDevice currentVideoDevice        = VideoDeviceInput.Device;
                AVCaptureDevicePosition preferredPosition = AVCaptureDevicePosition.Unspecified;
                AVCaptureDevicePosition currentPosition   = currentVideoDevice.Position;

                switch (currentPosition)
                {
                case AVCaptureDevicePosition.Unspecified:
                case AVCaptureDevicePosition.Front:
                    preferredPosition = AVCaptureDevicePosition.Back;
                    break;

                case AVCaptureDevicePosition.Back:
                    preferredPosition = AVCaptureDevicePosition.Front;
                    break;
                }
                AVCaptureDevice videoDevice           = CreateDevice(AVMediaType.Video, preferredPosition);
                AVCaptureDeviceInput videoDeviceInput = AVCaptureDeviceInput.FromDevice(videoDevice);

                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(videoDeviceInput))
                {
                    subjectSubscriber.Dispose();

                    SetFlashModeForDevice(AVCaptureFlashMode.Auto, videoDevice);
                    subjectSubscriber = NSNotificationCenter.DefaultCenter.AddObserver(AVCaptureDevice.SubjectAreaDidChangeNotification, SubjectAreaDidChange, videoDevice);

                    Session.AddInput(videoDeviceInput);
                    VideoDeviceInput = videoDeviceInput;
                }
                else
                {
                    Session.AddInput(VideoDeviceInput);
                }

                AVCaptureConnection connection = MovieFileOutput.ConnectionFromMediaType(AVMediaType.Video);
                if (connection.SupportsVideoStabilization)
                {
                    connection.PreferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.Auto;
                }

                Session.CommitConfiguration();

                DispatchQueue.MainQueue.DispatchAsync(() => {
                    CameraButton.Enabled = true;
                    RecordButton.Enabled = true;
                    StillButton.Enabled  = true;
                });
            });
        }
Ejemplo n.º 2
0
        void AddObservers()
        {
            runningObserver        = Session.AddObserver("running", NSKeyValueObservingOptions.New, OnSessionRunningChanged);
            capturingStillObserver = StillImageOutput.AddObserver("capturingStillImage", NSKeyValueObservingOptions.New, OnCapturingStillImageChanged);
            recordingObserver      = MovieFileOutput.AddObserver("recording", NSKeyValueObservingOptions.New, OnRecordingChanged);

            subjectSubscriber    = NSNotificationCenter.DefaultCenter.AddObserver(AVCaptureDevice.SubjectAreaDidChangeNotification, SubjectAreaDidChange, VideoDeviceInput.Device);
            runtimeErrorObserver = NSNotificationCenter.DefaultCenter.AddObserver(AVCaptureSession.RuntimeErrorNotification, SessionRuntimeError, Session);

            // A session can only run when the app is full screen. It will be interrupted in a multi-app layout, introduced in iOS 9.
            // Add observers to handle these session interruptions
            // and show a preview is paused message. See the documentation of AVCaptureSession.WasInterruptedNotification for other
            // interruption reasons.
            interuptionObserver      = NSNotificationCenter.DefaultCenter.AddObserver(AVCaptureSession.WasInterruptedNotification, SessionWasInterrupted, Session);
            interuptionEndedObserver = NSNotificationCenter.DefaultCenter.AddObserver(AVCaptureSession.InterruptionEndedNotification, SessionInterruptionEnded, Session);
        }
Ejemplo n.º 3
0
        void ToggleMovieRecording(CameraViewController sender)
        {
            // Disable the Camera button until recording finishes, and disable the Record button until recording starts or finishes.
            CameraButton.Enabled = false;
            RecordButton.Enabled = false;

            SessionQueue.DispatchAsync(() => {
                if (!MovieFileOutput.Recording)
                {
                    if (UIDevice.CurrentDevice.IsMultitaskingSupported)
                    {
                        // Setup background task. This is needed because the IAVCaptureFileOutputRecordingDelegate.FinishedRecording
                        // callback is not received until AVCam returns to the foreground unless you request background execution time.
                        // This also ensures that there will be time to write the file to the photo library when AVCam is backgrounded.
                        // To conclude this background execution, UIApplication.SharedApplication.EndBackgroundTask is called in
                        // IAVCaptureFileOutputRecordingDelegate.FinishedRecording after the recorded file has been saved.
                        backgroundRecordingID = UIApplication.SharedApplication.BeginBackgroundTask(null);
                    }

                    // Update the orientation on the movie file output video connection before starting recording.
                    AVCaptureConnection connection = MovieFileOutput.ConnectionFromMediaType(AVMediaType.Video);
                    var previewLayer            = (AVCaptureVideoPreviewLayer)PreviewView.Layer;
                    connection.VideoOrientation = previewLayer.Connection.VideoOrientation;

                    // Turn OFF flash for video recording.
                    SetFlashModeForDevice(AVCaptureFlashMode.Off, VideoDeviceInput.Device);

                    // Start recording to a temporary file.
                    MovieFileOutput.StartRecordingToOutputFile(new NSUrl(GetTmpFilePath("mov"), false), this);
                }
                else
                {
                    MovieFileOutput.StopRecording();
                }
            });
        }