ProcessCameraProvider.BindToLifecycle Not Working

Nathan Sokalski 4,116 Reputation points

I have the following code to bind my camera to a PreviewView:

private void StartPreview()
	this.cameraproviderfuture.AddListener(new Runnable(() =>
		this.preview = new Preview.Builder().SetTargetResolution(new Size(1280, 720)).Build();

		this.imgcapture = new ImageCapture.Builder().Build();

		CameraSelector camselector = null;
		if (this.cameraprovider.HasCamera(CameraSelector.DefaultBackCamera)) { camselector = CameraSelector.DefaultBackCamera; }
		else if (this.cameraprovider.HasCamera(CameraSelector.DefaultFrontCamera)) { camselector = CameraSelector.DefaultFrontCamera; }
		else { throw new System.Exception("Camera Not Found"); }

			this.cameraprovider.BindToLifecycle(this, camselector, this.preview, this.imgcapture);
		catch (Java.Lang.Exception e) { System.Diagnostics.Debug.WriteLine($"StartPreview: {e.Message}"); }
	}), ContextCompat.GetMainExecutor(this));

	//this.cameraprovider.IsBound(this.preview) is still false at this point

However, this does not seem to work (as stated in the comment at the end of the code above), but it also does not give any Exceptions. What is the problem?

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
Microsoft Technologies based on the .NET software framework.
3,368 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,235 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,291 Reputation points Microsoft Vendor


    I tested Xamarin with reference to this official tutorial on Android API level 30, and the results showed that BindToLifecycle works as expected and the IsBound method returns true.

    Are you missing permissions or API levels when writing code?

    Please refer to the following tutorial and xamarin code sample.

    private void StartCamera()
        var cameraProviderFuture = ProcessCameraProvider.GetInstance(this);
       cameraProviderFuture.AddListener(new Runnable(() =>
            // Used to bind the lifecycle of cameras to the lifecycle owner
            var cameraProvider = (ProcessCameraProvider)cameraProviderFuture.Get();
            var preview = new Preview.Builder().Build();
            this.imageCapture = new ImageCapture.Builder().Build();
            var imageAnalyzer = new ImageAnalysis.Builder().Build();
            imageAnalyzer.SetAnalyzer(cameraExecutor, new LuminosityAnalyzer(luma =>
                Log.Debug(TAG, $"Average luminosity: {luma}")
           // Select back camera as a default
            CameraSelector cameraSelector = null;
            if (cameraProvider.HasCamera(CameraSelector.DefaultBackCamera) == true)
                cameraSelector = CameraSelector.DefaultBackCamera;
            else if (cameraProvider.HasCamera(CameraSelector.DefaultFrontCamera) == true)
                cameraSelector = CameraSelector.DefaultFrontCamera;
                throw new System.Exception("Camera not found");
                // Unbind use cases before rebinding
               // Bind use cases to camera
                cameraProvider.BindToLifecycle(this, cameraSelector, preview, imageCapture, imageAnalyzer);
            catch (Exception exc)
                Log.Debug(TAG, "Use case binding failed", exc);
                Toast.MakeText(this, $"Use case binding failed: {exc.Message}", ToastLength.Short).Show();
            var b = cameraProvider.IsBound(preview); // the result will be true.
       }), ContextCompat.GetMainExecutor(this)); 

    Best Regards,

    Alec Liu.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments