4

I have web Page to Show user camera, how I can access the camera on the flutter webview?

I try to do that with this URL "https://webrtc.github.io/samples/src/content/getusermedia/gum/" but return getUserMedia error: NotAllowedError

Javad Zobeidi
  • 191
  • 1
  • 4
  • 9

3 Answers3

15

You can try my plugin flutter_inappwebview.

To request permissions about the camera and microphone, you can use the permission_handler plugin.

Also, it has the androidOnPermissionRequest event for Android, that is an event fired when the WebView is requesting permission to access the specified resources (that is the Android native WebChromeClient.onPermissionRequest event).

An example of using WebRTC that works on Android:

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);
                      }
                  ),
                ),
              ),
            ]))
    );
  }
}

This example uses the room 158489234 on https://appr.tc/, that is a video chat demo app based on WebRTC (https://github.com/webrtc/apprtc). To get it work, you need to set the option mediaPlaybackRequiresUserGesture to false.

Also, you need to add these permissions in the AndroidManifest.xml:

<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" />
Lorenzo Pichilli
  • 2,896
  • 1
  • 27
  • 50
  • @MadMan for which platform? On iOS it won't work (see https://github.com/pichillilorenzo/flutter_inappwebview/issues/200), otherwise open an issue to the plugin repository with all useful information and errors you are getting – Lorenzo Pichilli Dec 12 '19 at 17:42
  • It worked well in Android. Do you know which permission I need to open the dialog box to select a file or an image from Android camera? – Bruno Yuzo Aug 14 '20 at 21:17
  • I've used this HTML code to get it working `` – Bruno Yuzo Aug 15 '20 at 02:37
  • 4
    what about iOS? – Sanjay Kumar Jan 17 '21 at 06:39
  • @LorenzoPichilli - How would I be able to request client authorizations when a client attempts to upload documents instead of asking in the beginning? (Please see - https://stackoverflow.com/questions/66565247/how-can-i-ask-for-user-permissions-when-user-tries-to-upload-file-in-mobile-appl) – PARAS GUPTA Mar 18 '21 at 05:23
  • @LorenzoPichilli Some of the code in your snippet is out of date, could you please update it? Thanks – Haplo Jul 06 '21 at 23:11
  • Could you share the full code please. Also its not up to date. Im trying the similar program it does not even resolve without error. @LorenzoPichilli – Vibin Guevara Apr 11 '22 at 18:11
  • Hey, i couldn't get this to work at https://webcam-test.com/ – Muhammad Adil Jul 19 '22 at 18:42
  • 1
    I never see permission access dialog.. – Muhammad Adil Jul 19 '22 at 18:43
0

this worked for me

final WebViewController controller = WebViewController.fromPlatformCreationParams(params, onPermissionRequest: (resources) async {
          return resources.grant();
        });
Hasan koç
  • 51
  • 3
0

To access the camera in a WebView you should take care of the following:

  1. Grant camera access before opnening the webview (This can be done through permission_handler package)
  2. Properly initialize you WebViewController, be sure to await for all the async methods like await controller.setJavaScriptMode(JavaScriptMode.unrestricted);
  3. For IOS you must also provide some platform specific parameters using the WebKitWebViewControllerCreationParams class of the webview_flutter_wkwebview package (See the example below)
  4. Grant onPermissionRequest to controller (See the example below)

Your controller should look similar to this, and you may use a FutureBuilder to initialize it before launching your WebView:

Future<WebViewController> _getController() async {
late final PlatformWebViewControllerCreationParams params;
params = WebViewPlatform.instance is WebKitWebViewPlatform
    ? WebKitWebViewControllerCreationParams(
        allowsInlineMediaPlayback: true, mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{})
    : const PlatformWebViewControllerCreationParams();

final controller = WebViewController.fromPlatformCreationParams(
  params,
  onPermissionRequest: (request) {
    request.grant();
  },
);
await controller.setJavaScriptMode(JavaScriptMode.unrestricted);
await controller.loadRequest(Uri.parse( "Your url" ));

return controller;
}