Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

camera preview freezes when screen is locked & unlocked.

zzinzzin CAMember ✭✭

When i lock the the screen and then unlock the screen, the android camera becomes frozen for some odd reason. this is code.

` static readonly string TAG = "CameraPreview";

static readonly int CAMERA_ID = 1;

Camera camera;
public static FragmentOne NewInstance ()
    return new FragmentOne ();
Preview t;
public override void OnCreate (Bundle savedInstanceState)
    base.OnCreate (savedInstanceState);
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    if (camera == null) {camera = GetCameraInstance (CAMERA_ID);}
    t = new Preview(this.Activity,camera);
    View root = inflater.Inflate (Resource.Layout.framelayout, null);
    SurfaceOrientation displayRotation = Activity.WindowManager.DefaultDisplay.Rotation;
    FrameLayout preview =  root.FindViewById <FrameLayout> (Resource.Id.camera_preview);
    preview.AddView (t);
    return root;
public override void OnResume ()
    base.OnResume ();
    if (camera == null) {camera = GetCameraInstance (CAMERA_ID);}
    t.SetCamera (camera); 
    Log.Debug (TAG, "resumed");


public override void OnPause () 
    base.OnPause ();
    if (camera != null) {
            t.SetCamera (null);
            camera.Release ();
            camera = null;

public static Camera GetCameraInstance (int cameraId)
    Camera c = null;

    try {
        c = Camera.Open (cameraId); 

        Log.Debug(TAG,"camera Opening");// attempt to get a Camera instance
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
        Log.Debug (TAG, string.Format ("Camera {0} is not available: {1}", cameraId,  e.Message));

    return c;   // returns null if camera is unavailable
}   `
Sign In or Register to comment.