Search code examples
c#.netblazormauihybrid

.NET MAUI Capture photo and capture video in the same view


I want the device camera to open and the user being able to capture a photo or video.

I tried using MediaPicker, but unfortunately it does have only 4 methods, only two of which allow us to do something with the device camera: CaptureVideoAsync() and CapturePhotoAsync().

I can use them separately and get video / photo.

But I'd like to open only one view and the user might choose there to record a video or snap a photo.


Solution

  • I think in your case it would be better to create an own component, which is using the Camera.MAUI NuGet. Just add the CameraView and buttons for taking photos/recording videos to your own component and you should be good to go.

    After the initial setup (refer to link below) you can react on button click as follows (code was taken from the official docs):

    • Record a video:
        var result = await cameraView.StartRecordingAsync(Path.Combine(FileSystem.Current.CacheDirectory, "Video.mp4"), new Size(1920, 1080));
        ....
        result = await cameraView.StopRecordingAsync();
    
    • Take a photo
        var stream = await cameraView.TakePhotoAsync();
        if (stream != null)
        {
            var result = ImageSource.FromStream(() => stream);
            snapPreview.Source = result;
       }
    

    More information: