P5.js createCapture 失败回调

Rya*_*ten 5 video-capture callback video-streaming p5.js

p5.j​​s 的createCapture函数是否有回调函数失败?(即当用户权限被拒绝或用户浏览器不支持视频摄像机流时)。

我注意到在 src 中有一个成功回调,但似乎无法找到失败的回调。在浏览器控制台中,p5 还报告'DOMException: Permission denied',但是,我想以更用户友好的方式处理此问题。

如果没有回调,使用 createCapture 处理媒体故障的最佳实践是什么,因为文档中似乎没有讨论过。

gar*_*nch 1

好吧,这个答案迟了一年多,但发布这个答案可能对陷入同一问题的其他人有用。我建议不要按照下面的评论中的建议对捕获本身进行错误测试,或者重新设计 createCapture() (如果您认为应该更改,最好通过打开问题请求来完成),我建议仅在已设置的情况下测试捕获的像素数组然后继续执行脚本执行的操作。这可以简单地完成,如下所示:

//load pixel data of webcam capture
cap.loadPixels();

//all values in the pixel array start as zero so just test if they are 
//greater than zero 
if (cap.pixels[1] > 0)
{
    //put the rest of your script here
}
Run Code Online (Sandbox Code Playgroud)

下面是一个完整的实际示例:

var canvas;
var cap;
var xpos = 0;

function setup()
{
    canvas = createCanvas(windowWidth, windowHeight);
    canvas.style("display", "block");
    background("#666666");

    //create an instance of the webcam
    cap = createCapture(VIDEO);

    cap.size(640, 480);
    cap.hide();
}


function draw()
{
    //load pixel data of webcam capture
    cap.loadPixels();

    //if the first pixel's R value is set continue with the rest of the script
    if (cap.pixels[1] > 0)
    {
        var w = cap.width;
        var h = cap.height;

        copy(cap, w/2, 0, 10, h, xpos, (height / 2) - (h / 2), 10, h);

        xpos = xpos + 1;

        if (xpos > width) xpos = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)