Android相机服务因打开太快​​而崩溃

问题描述

我在输出日志中遇到以下错误

08-20 15:12:19.488 W/CameraBase(14231): An error occurred while connecting to camera: 0
08-20 15:12:19.488 W/CameraBase(14231): Camera service died!

因为相机服务消失了。我看到一个弹出窗口,显示“很遗憾,Google App已停止。”。

出现该弹出窗口时。语音识别功能由于无法在Google应用中运行而停止工作。

因此,尝试打开相机会导致相机损坏,从而损坏google,从而损坏一切。

我找出了释放相机的原因。

我发现,如果使用调试器逐步调试它,则不会导致此问题。

这使我相信在某种竞赛条件下,由于上次通话尚未完全释放摄像机,因此无法打开摄像机。

我可以手动插入一个延迟来解决此问题,但是我宁愿找到一个无法猜测正确延迟的基于状态或回调的解决方案。

由于android摄像头有很多问题,要使其正常工作,需要反复试验。

public async Task<bool> GetCamera()
{
    //Android.Hardware.Camera res = null;

    Stopwatch sw = new Stopwatch();
    sw.Start();

    while (sw.ElapsedMilliseconds < 30000)
    {
        try
        {
            if (this.cam != null)
            {
                //this.cam.Unlock();
                this.cam.StopPreview();
                this.cam.SetPreviewCallback(null);
                this.cam.Release();
                //this.cam.dispose();
                this.cam = null;
            }

            this.cam = Android.Hardware.Camera.Open(this.cameraIndex);

            //this.EnableHDRecording();

            // locking and then unlocking here causes the auto focus to fail
            // no idea why
            //this.LockCamera();


            //this.cam.SetPreviewTexture(surfaceTexture);
            this.cam.SetPreviewdisplay(this.previewSurface);
            this.cam.StartPreview();

            //this.UnlockCamera();


            break;
        }
        catch (Exception x)
        {
            x.ToString();
        }

        await Task.Delay(100);
    }

    if (this.cam == null)
    {
        throw new System.Exception("Unable to  open camera and set preview");
    }

    return true;
    //return res;
}

我怎么知道什么时候可以重新打开相机,以免因调用open太快而使服务崩溃?

此外,如果您想知道为什么我要释放相机然后再要求使用相机。这是因为经过一段随机的时间。调用AutoFocus()时,相机的自动对焦停止工作,并且直到我重新获得相机后才重新开始工作。我怀疑这可能是因为我的通话崩溃并重新启动了服务,从而使相机重置为可以再次工作。

我也在使用android 5.1 API 22。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)