webRTC拉起摄像头和麦克风
Web组件可以通过W3C标准协议接口拉起摄像头和麦克风。开发者在使用该功能时,需配置"ohos.permission.CAMERA"、"ohos.permission.MICROPHONE"权限。
通过在JavaScript中调用W3C标准协议接口navigator.mediaDevices.getUserMedia(),该接口用于拉起摄像头和麦克风。constraints参数是一个包含了video和audio两个成员的MediaStreamConstraints对象,用于说明请求的媒体类型。
在下面的示例中,点击index.html前端页面中的开起摄像头按钮,打开摄像头和麦克风。
-
应用侧代码。
// xxx.ets import web_webview from '@ohos.web.webview'; import abilityAccessCtrl, { PermissionRequestResult, Permissions } from '@ohos.abilityAccessCtrl'; @Entry @Component struct WebComponent { controller: web_webview.WebviewController = new web_webview.WebviewController() aboutToAppear() { // 配置Web开启调试模式 web_webview.WebviewController.setWebDebuggingAccess(true); let atManager = abilityAccessCtrl.createAtManager(); atManager.requestPermissionsFromUser(getContext(this), ['ohos.permission.CAMERA', 'ohos.permission.MICROPHONE']) .then(data => { let result: Array<number> = data.authResults; let hasPermissions1 = true; result.forEach(item => { if (item === -1) { hasPermissions1 = false; } }) if (hasPermissions1) { console.info("hasPermissions1") } else { console.info(" not hasPermissions1") } }).catch(() => { return; }); } build() { Column() { Web({ src: $rawfile('index.html'), controller: this.controller }) .onPermissionRequest((event) => { if (event) { AlertDialog.show({ title: 'title', message: 'text', primaryButton: { value: 'deny', action: () => { event.request.deny() } }, secondaryButton: { value: 'onConfirm', action: () => { event.request.grant(event.request.getAccessibleResource()) } }, cancel: () => { event.request.deny() } }) } }) } } }
-
前端页面index.html代码。
<!-- index.html --> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <video id="video" width="500px" height="500px" autoplay="autoplay"></video> <canvas id="canvas" width="500px" height="500px"></canvas> <br> <input type="button" title="HTML5摄像头" value="开启摄像头" onclick="getMedia()"/> <script> function getMedia() { let constraints = { video: {width: 500, height: 500}, audio: true }; // 获取video摄像头区域 let video = document.getElementById("video"); // 返回的Promise对象 let promise = navigator.mediaDevices.getUserMedia(constraints); // then()异步,调用MediaStream对象作为参数 promise.then(function (MediaStream) { video.srcObject = MediaStream; video.play(); }); } </script> </body> </html>