Android app crashes after some time when using WebCamTexture

Hello everyone,

I am working on simple AR app where I will show camera as a background texture and some 3D objects before it. I have done all I wanted to complete the app but facing one issue.

My App crashes after 10-15 seconds of launching. App Works as I expect but after 10-15 seconds, it automatically stops or you can say it crashes. I don’t know where I may be wrong, even my app size doesn’t exceeds 30 mb in size.

Your response is greatly appreciated.

I have attached a code sample below for your reference.

using UnityEngine;
using UnityEngine.UI;

public class CameraManager : MonoBehaviour
{
private bool isCamAvailable;
private WebCamTexture backCam;
private Texture defaultBackground;

public RawImage background;
public AspectRatioFitter fit;

private void Start()
{
    defaultBackground = background.texture;
    WebCamDevice[] devices = WebCamTexture.devices;

    if (devices.Length == 0) {
        Debug.Log("No camera detected");
        isCamAvailable = false;
        return;
    }
    for (int i = 0; i < devices.Length; i++)
    {
        if (!devices*.isFrontFacing) {*

backCam = new WebCamTexture(devices*.name, Screen.width, Screen.height);*
}
}
if (backCam == null) {
Debug.Log(“No back cam found!!”);
backCam = new WebCamTexture(devices[0].name, Screen.width, Screen.height);
//return;
}
backCam.Play();
background.texture = backCam;
isCamAvailable = true;
}
private void Update()
{
if (!isCamAvailable)
return;
float ratio = (float)backCam.width / (float)backCam.height;
fit.aspectRatio = ratio;
float scaleY = backCam.videoVerticallyMirrored ? -1f : 1f;
background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
int orient = -backCam.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
}
}

Having the same issue, tested on S6 Edge after 10 sec it crashes and closes the app.

Tested on Huawei Mate 8 and works perfectly. Seems it’s an issue of memory handling but still I have not resolved.