使用 GWT Elemental 关闭网络摄像头和灯

Turning Webcam and Light off with GWT Elemental

我找到了这段很棒的代码来打开网络摄像头并拍照,但遗憾的是,我找不到关闭网络摄像头的方法。没有方法可以调用它,甚至从 GWT 调用打开相机也是使用本机 JavaScript,我目前不是很熟悉。有什么想法吗?这是代码:

public native static boolean getUserVideo(UserWebcamCallback callback)/*-{
        if(navigator.webkitGetUserMedia) {
            navigator.webkitGetUserMedia(
                {video: true, toString: function() {return "video";}},
                function(stream) {
                    var s = window.URL.createObjectURL(stream);
                    $entry(callback.@com.test.ElementalUtils.UserWebcamCallback::onSuccess(Ljava/lang/String;)(s));
                    },
                function() {
                    $entry(callback.@com.test.ElementalUtils.UserWebcamCallback::onFail()());
                });
            return true;
        } else {
            return false;
        }
    }-*/;

呸!以为我会在这个上度过不眠之夜。 诀窍是在代码中添加一个 "localStream" 全局变量,并在另一种方法中关闭流。像这样:

    public native static boolean getUserVideo(UserWebcamCallback callback)/*-{
            localStream;
            if(navigator.webkitGetUserMedia) {
                navigator.webkitGetUserMedia(
                    {video: true, toString: function() {return "video";}},
                    function(stream) {
                        var s = window.URL.createObjectURL(stream);

            localStream = stream;
                        $entry(callback.@com.test.ElementalUtils.UserWebcamCallback::onSuccess(Ljava/lang/String;)(s));
                        },
                    function() {
                        $entry(callback.@com.test.ElementalUtils.UserWebcamCallback::onFail()());
                    });
                return true;
            } else {
                return false;
            }
        }-*/;

public native static void stopVideo()/*-{
localStream.getTracks()[0].stop();
}-*/