Capture screen — chrome.desktopCapture.chooseDesktopMedia fails — PNacl extension

南楼画角 提交于 2019-12-21 20:57:19

问题


I'm trying to use the desktopCapture API in the following manner.

chrome.desktopCapture.chooseDesktopMedia(
            ["screen", "window"], onAccessApproved);

chrome.desktopCapture shows as undefined when I set a breakpoint and inspect it. Permissions in my manifest file are as follows:-

"permissions": ["desktopCapture", "notifications" ]

Common causes for failure of this API are listed here as

  • a permission is missing in the application's manifest.json file
  • the API is defined on a newer version of Chrome then the current runtime docs inherited from ChromeApi

And I don't have those problems.

  • My Chrome version is 43.0.2357.124 m
  • Pepper version is 43

FYI, I am trying to develop a Chrome extension to capture the screen using PNacl, and have borrowed from the media_stream_video example downloaded from here. But I haven't even gotten to sending a message to the pexe side yet. I'm still stuck at chrome.desktopCapture.chooseDesktopMedia returning undefined.


回答1:


You need to call chrome.desktopCapture.chooseDesktopMedia from the background script running in the context of the extension. This Sample shows a simple method to use the extension to get screen media.

Keep in mind that this is callback based, so you get access to the stream id from the callback.

This runs in the context of your page (see full example here):

    // check that the extension is installed
    if (sessionStorage.getScreenMediaJSExtensionId) {
        // send a message to your extension requesting media
        chrome.runtime.sendMessage(sessionStorage.getScreenMediaJSExtensionId,
            {type:'getScreen', id: 1}, null,
            function (data) {
                if (data.sourceId === '') { // user canceled
                    // handle error
                } else {
                    constraints.video.mandatory.chromeMediaSourceId = data.sourceId;
                    getUserMedia(constraints, callback);
                }
            }
        );
    }

And this run in the context of your extension (see full example here):

chrome.runtime.onMessageExternal.addListener(function (message, sender, callback) {
    switch(message.type) {
        case 'getScreen':
            var pending = chrome.desktopCapture.chooseDesktopMedia(message.options || ['screen', 'window'],
                                                               sender.tab, function (streamid) {
                // communicate this string to the app so it can call getUserMedia with it
                message.type = 'gotScreen';
                message.sourceId = streamid;
                callback(message);
                return false;
            });
            return true; // retain callback for chooseDesktopMedia result
    }
});


来源:https://stackoverflow.com/questions/30906689/capture-screen-chrome-desktopcapture-choosedesktopmedia-fails-pnacl-extens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!