Chrome:navigator.mediaDevices.getUserMedia不是一个函数

时间:2016-05-19 06:25:17

标签: javascript google-chrome webrtc

我在localhost上尝试在Chrome中使用MediaDevices.getUserMedia方法。我收到标题错误。据我所知,在Chrome中,只能使用具有安全来源的此功能,并且localhost被视为安全来源。此外,这适用于Firefox。

这就是我使用它的方式,如Google Developers网站https://developers.google.com/web/updates/2015/10/media-devices?hl=en所示:

var constraints = window.constraints = {
            audio: false,
            video: true
};


navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
            callFactory.broadcastAssembly(stream);
            ...
});

7 个答案:

答案 0 :(得分:7)

尝试启用:chrome:// flags / #enable-experimental-web-platform-features

以铬为我工作

答案 1 :(得分:6)

在某些最新的浏览器#!/usr/bin/env python import paho.mqtt.client as mqtt import os, json import time organization = "xel7" username = "" password = "" #Set the variables for connecting to the iot service broker = "" devicename = "mynewdev" deviceType = "mymqttdevice" topic = "iot-2/type/mymqttdevice/id/mynewdev/evt/iotFile/fmt/json" clientID = "a:" + organization + ":appId" broker = organization + ".messaging.internetofthings.ibmcloud.com" mqttc = mqtt.Client(clientID) if username is not "": mqttc.username_pw_set(username, password=password) def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) def on_subscribe(mosq, obj, mid, granted_qos): print("Subscribed: " + str(mid) + " " + str(granted_qos)) def on_message(client, userdata, msg): with open('receivednew.txt', 'w') as fd: txt = (msg.payload.decode('string_escape')) fd.write(txt) #print txt fd.close() mqttc.connect(host=broker, port=1883, keepalive=60) test = mqttc.subscribe(topic,0) mqttc.on_connect = on_connect mqttc.on_subscribe = on_subscribe mqttc.on_message = on_message mqttc.loop_forever() 上效果不佳。因此,请尝试使用navigator.mediaDevices.getUserMedia。或者,您最好检查浏览器使用navigator.getUserMedia是否可以navigator.mediaDevices.getUserMedia,或者使用navigator.mediaDevices.getUserMedia

navigator.getUserMedia

希望这能解决您的问题。

答案 2 :(得分:4)

我在Chrome浏览器中也遇到了同样的问题。 首先通过在https://test.webrtc.org/

中对其进行测试来检查手机是否受支持

如果您的手机通过了所有情况,请检查步骤2

步骤2: 如果您托管网页或运行第三方网页,请查看是否在手机上启用了摄像头权限。

主要问题是 HTTP站点不支持WEBRTC,而HTTPS站点仅支持

This is the https site which allows web This is the http site which gives a error

答案 3 :(得分:0)

改为使用navigator.getUserMedia()

navigator.getUserMedia(constraints, successCallback, errorCallback);

答案 4 :(得分:0)

您是否尝试过添加adapter.js polyfill?查看此页面: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#Browser_compatibility

Chrome浏览器需要按照chrome://flags/#enable-experimental-web-platform-features的说明启用此功能或启用@Simon Malone

答案 5 :(得分:0)

我也遇到了这个问题,改变标志似乎没有用。我在Google的Web Server for Chrome中遇到了这个Chrome扩展程序WebRTC tutorial,它似乎可以解决问题。

答案 6 :(得分:0)

我陷入了同样的问题。一种解决方案是遵循并下载@ellerynz在上面的评论中共享的扩展名Web Server for Chrome,或者

如果您安装了python,也可以这样做

python -m SimpleHTTPServer [port]

按下Enter键后,您应该会看到以下消息:

Serving HTTP on 0.0.0.0 port 8000 ...

打开浏览器并放入

http://127.0.0.1:[port]