HarmonyOS 鸿蒙Next 在WebView中通过getUserMedia打开摄像头失败
HarmonyOS 鸿蒙Next 在WebView中通过getUserMedia打开摄像头失败
config.json已经配置如下权限:
....
{
"name": "ohos.permission.CAMERA"
},
{
"name": "ohos.permission.WRITE_MEDIA"
},
{
"name": "ohos.permission.MODIFY_AUDIO_SETTINGS"
},
{
"name": "ohos.permission.MEDIA_LOCATION"
},
{
"name": "ohos.permission.MANAGE_USER_STORAGE"
},
{
"name": "ohos.permission.MICROPHONE"
},
....
MainAbility.java添加了如下代码:
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
// 省略若干代码
webView = (WebView)findViewById(ResourceTable.Id_webView);
webView.getWebConfig().setJavaScriptPermit(true);
webView.getWebConfig().setSecurityMode(webView.getWebConfig().SECURITY_ALLOW);
webView.getWebConfig().setDataAbilityPermit(true);
webView.getWebConfig().setLoadsImagesPermit(true);
webView.getWebConfig().setLocationPermit(true);
webView.getWebConfig().setMediaAutoReplay(true);
webView.getWebConfig().setWebStoragePermit(true);
// 省略若干代码
}
....
index.html关键内容如下:
<button onclick="showCamera()">开启摄像头</button>
<div class="bg-content" style="display: none;">
<div class="show-photo">
<video id="video" controls=true width="300px" height="400px" autoplay=false></video>
<canvas id="canvas" width="300px" height="400px" style="display: none;"></canvas>
<a id="downloadA"></a>
</div>
</div>
其中showCamera()对应js代码为:
function showCamera() {
$(".bg-content").css("display", "block");
let constraints = {
video: {
width: 600,
height: 600,
facingMode: 'environment' // 后置摄像头
},
audio: true
};
//获得video摄像头区域
let video = document.getElementById("video");
// 这个Promise对象返回成功后的回调函数带一个 MediaStream 对象作为其参数
let promise = navigator.mediaDevices.getUserMedia(constraints);
console.log(navigator.mediaDevices)
console.log('open media stream...')
promise.then(function(MediaStream) {
console.log('open media stream!')
mediaStreamTrack = typeof MediaStream.stop === 'function' ? MediaStream : MediaStream.getTracks()[1];
// video.srcObject = MediaStream;
video.src = window.URL && window.URL.createObjectURL(MediaStream) || MediaStream
video.muted = true;
video.play();
});
}
最后的 异常表现为video组件黑屏 上述代码部分借鉴网上资料,请求帮忙看看
更多关于HarmonyOS 鸿蒙Next 在WebView中通过getUserMedia打开摄像头失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html
您好,您的问题需要进一步定位,请您通过在线提单进一步解决:https://developer.huawei.com/consumer/cn/support/feedback/#/,感谢您的反馈和支持。
更多关于HarmonyOS 鸿蒙Next 在WebView中通过getUserMedia打开摄像头失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
报错信息:
message:"Permission denied"
name:"NotAllowedError"
在HarmonyOS鸿蒙Next系统中,如果在WebView中通过getUserMedia
API打开摄像头失败,可能的原因及解决方法如下:
-
权限配置: 确保应用已在
config.json
中声明了使用摄像头的权限。例如,需要添加ohos.permission.CAMERA
权限。同时,检查是否在运行时请求并获得了用户的摄像头使用授权。 -
WebView配置: WebView组件需要正确配置以支持WebRTC功能,这通常包括允许网页访问摄像头和麦克风等媒体设备。检查WebView的初始化代码,确保已启用相关设置。
-
系统兼容性: 不同版本的HarmonyOS可能对WebView的支持有所差异。确认当前系统版本是否支持通过WebView的
getUserMedia
访问摄像头。 -
浏览器兼容性: 虽然问题是在WebView中,但
getUserMedia
的实现依赖于底层的WebView引擎(如Chromium)。检查WebView引擎的版本,确保它支持所需的WebRTC功能。 -
调试与日志: 使用开发者工具或日志系统检查是否有更详细的错误信息。这有助于定位问题是否由权限、配置错误或其他系统级问题引起。
如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html,