Xamarin MonoMac preview webcam onto Main Window ( AVCaptureVideoPreviewLayer )


I created a basic Xamarin MonoMac Mac application.
I am trying to get a preview of my webcam onto the Main Window of the application.

I can successfully coonnect to the webcam, and the webcam light turns on but I get no video on the Main Window.

In an iOS application I would add a sub layer to the Main window and assign my captureVideoPreviewLayer to it. How do i do it with MonoMac ? Thank you for your help

My code

`session = new AVCaptureSession () {
SessionPreset = AVCaptureSession.PresetMedium

        var captureDevice = AVCaptureDevice.DefaultDeviceWithMediaType (AVMediaType.Video);

        var input = AVCaptureDeviceInput.FromDevice (captureDevice);
        if (input == null){
            Console.WriteLine ("No input - this won't work on the simulator, try a physical device");


        session.AddInput (input);

        AVCaptureVideoPreviewLayer captureVideoPreviewLayer = new AVCaptureVideoPreviewLayer (session);

        //----> preview layer onto Window here ?!

        session.StartRunning ();`


  • AshokGelalAshokGelal USMember, Beta ✭✭

    This is what you need:

    var contentView = Window.ContentView;
    contentView.WantsLayer = true;
    captureVideoPreviewLayer.Frame = contentView.Bounds;

    Update: I created a working sample project for you: https://github.com/ashokgelal/MacCaptureSample

  • GaetanSiryGaetanSiry USMember

    Thank you that work great

  • AshokGelalAshokGelal USMember, Beta ✭✭
    edited March 2014

    Awesome! If possible, please mark this question as answered. Thanks.

Sign In or Register to comment.