|
40 | 40 |
|
41 | 41 | <body> |
42 | 42 | <div class="wrapper"> |
43 | | - <button id="openCamera" title="开启摄像头">开启摄像头权限</button> |
| 43 | + <button id="openCamera" title="开启摄像头">`开启摄像头权限</button> |
44 | 44 |
|
45 | 45 | <video width="700px" height="390px" class="video" autoplay="autoplay"></video> |
46 | 46 |
|
|
61 | 61 |
|
62 | 62 | // 开启摄像头 |
63 | 63 | openCamera.addEventListener("click", () => { |
64 | | - navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator |
65 | | - .mozGetUserMedia || |
66 | | - navigator.msGetUserMedia; |
| 64 | + navigator.getUserMedia = navigator.getUserMedia || navigator |
| 65 | + .webkitGetUserMedia || navigator.mozGetUserMedia || navigator.mediaDevices.getUserMedia |
67 | 66 |
|
68 | | - if (!navigator.getUserMedia) return alert( |
69 | | - 'Native device media streaming (getUserMedia) not supported in this browser.'); |
| 67 | + if (navigator.getUserMedia) { |
70 | 68 |
|
71 | | - if (this.isOpenCamera) return console.log('[已经开启过摄像头的权限了]'); |
| 69 | + if (this.isOpenCamera) return console.log('[已经开启过摄像头的权限了]'); |
72 | 70 |
|
73 | 71 |
|
74 | | - let exArray = getSources() |
75 | | - const constraints = { |
76 | | - 'video': { |
77 | | - 'optional': [{ |
78 | | - 'sourceId': exArray[0] //0为前置摄像头,1为后置 |
79 | | - }] |
80 | | - }, |
81 | | - 'audio': true |
| 72 | + let exArray = getSources() |
| 73 | + navigator.getUserMedia({ |
| 74 | + 'video': { |
| 75 | + 'optional': [{ |
| 76 | + 'sourceId': exArray[0] //0为前置摄像头,1为后置 |
| 77 | + }] |
| 78 | + }, |
| 79 | + 'audio': true |
| 80 | + }, successFunc, errorFunc); //success是获取成功的回调函数 |
| 81 | + } else { |
| 82 | + return alert('不支持访问用户媒体') |
82 | 83 | } |
83 | | - // navigator.mediaDevices.getUserMedia(constraints).then((stream) => { |
84 | | - // successFunc(stream) |
85 | | - |
86 | | - // }).catch(function (err) { |
87 | | - // alert(err.name + ": " + err.message); |
88 | | - // }); // 总是在最后检查错误; |
89 | | - navigator.getUserMedia({ |
90 | | - 'video': { |
91 | | - 'optional': [{ |
92 | | - 'sourceId': exArray[0] //0为前置摄像头,1为后置 |
93 | | - }] |
94 | | - }, |
95 | | - 'audio': true |
96 | | - }, successFunc, errorFunc); //success是获取成功的回调函数 |
97 | 84 | }) |
98 | 85 |
|
99 | 86 | // 摄像机成功回调 |
|
0 commit comments