flutter 无法从H5 WebView 访问摄像头和录音权限

AndroidManifest.xml需要在 中添加以下权限:

c 复制代码
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.VIDEO_CAPTURE" />
<uses-permission android:name="android.permission.AUDIO_CAPTURE" />
c 复制代码
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:permission_handler/permission_handler.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Permission.camera.request();
  await Permission.microphone.request();

  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: InAppWebViewPage()
    );
  }
}

class InAppWebViewPage extends StatefulWidget {
  @override
  _InAppWebViewPageState createState() => new _InAppWebViewPageState();
}

class _InAppWebViewPageState extends State<InAppWebViewPage> {
  InAppWebViewController _webViewController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text("InAppWebView")
        ),
        body: Container(
            child: Column(children: <Widget>[
              Expanded(
                child: Container(
                  child: InAppWebView(
                      initialUrl: "https://appr.tc/r/158489234",
                      initialOptions: InAppWebViewGroupOptions(
                        crossPlatform: InAppWebViewOptions(
                          mediaPlaybackRequiresUserGesture: false,
                          debuggingEnabled: true,
                        ),
                      ),
                      onWebViewCreated: (InAppWebViewController controller) {
                        _webViewController = controller;
                      },
                      androidOnPermissionRequest: (InAppWebViewController controller, String origin, List<String> resources) async {
                        return PermissionRequestResponse(resources: resources, action: PermissionRequestResponseAction.GRANT);//主要是这行代码是授权的
                      }
                  ),
                ),
              ),
            ]))
    );
  }
}
相关推荐
白茶三许2 小时前
【2025】Flutter 卡片组件封装与分页功能实现:实战指南
flutter·开源·openharmony
Bervin121382 小时前
Flutter Android环境的搭建
android·flutter
fouryears_2341710 小时前
现代 Android 后台应用读取剪贴板最佳实践
android·前端·flutter·dart
等你等了那么久11 小时前
Flutter国际化语言轻松搞定
flutter·dart
神经蛙397116 小时前
settings.gradle' line: 22 * What went wrong: Plugin [id: 'org.jetbrains.kotlin.a
flutter
stringwu17 小时前
一个bug 引发的Dart 与 Java WeakReference 对比探讨
flutter
火柴就是我2 天前
从头写一个自己的app
android·前端·flutter
●VON2 天前
Flutter 项目成功运行后,如何正确迁移到 OpenHarmony?常见疑问与跳转失效问题解析
flutter·华为·openharmony·开源鸿蒙
●VON2 天前
Flutter 编译开发 OpenHarmony 全流程实战教程(基于 GitCode 社区项目)
flutter·openharmony·gitcode
消失的旧时光-19432 天前
Flutter 组件:Row / Column
flutter