我正在开发具有视频聊天功能的移动应用。我为webrtc找到了一个不错的javascript库,它似乎在QWebView
中加载得很好,但我需要允许它访问摄像头和麦克风,我无法弄清楚如何做到这一点。可能吗? QWebEngineView
有一个方便的信号和插槽,但它不支持移动。
清单权限不能像here描述的那样工作。
相应的Qt bug:cant access camera and mic with QML WebView
有任何想法吗?
Qt在5.10版本中实现了Android的权限模型。
见这里:http://doc.qt.io/qt-5/qtandroid.html。
您必须使用的功能是:
如果要访问摄像头和麦克风,则必须在使用前请求权限。例:
QStringList perms;
perms << "android.permission.CAMERA" << "android.permission.RECORD_AUDIO";
QtAndroid::checkPermissions(perms, [](const PermissionResultMap& resMap) {
foreach(const QString &key, resMap.keys()) {
qDebug() << "Result of permission" << key << ":" << resMap[key];
}
});
清单权限不适用于Android 22>。因此,在您的情况下,您需要访问运行时权限。在Java中执行此操作。
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
arrayOf(Manifest.permission.READ_CONTACTS),
MY_PERMISSIONS_REQUEST_READ_CONTACTS)
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
从Official Doc看。但在你的情况下,我认为你需要通过NDK级别来调用相同的行为。它也描述了here。
我们通过重新编译QT代码并更新相应的jar解决了这个QT错误。我们使用维护工具下载了Android的QT源代码。在通过QT源代码时,我们发现由于某些原因,Qt开发人员没有覆盖onPermissionRequest()
的WebCromeClient
,WebView不允许媒体访问JS函数。请按照以下步骤解决问题。
QtAndroidWebViewController.java
中的代码。该类位于〜/ Qt / 5.10.0 / Src / qtwebview / src / jar / src / org / qtproject / qt5 / android / view。将以下函数添加到内部类QtAndroidWebChromeClient
。
@Override public void onPermissionRequest(PermissionRequest request){request.grant(request.getResources()); }QtAndroidWebViewClient
中的一些方法。QtAndroidWebView.jar
,QtAndroidWebView-bundled.jar
中生成两个jar。