4

I'm new to flutter. I'm converting the existing web application to mobile application using web view in flutter. Aim is to first make this working in android.

I'm trying to make the camera, microphone option enabled in the flutter mobile application, which is already working on the web. I'm using the webview_flutter: library in the pubspec.yml for this.

This is the simple code I'm using below.

main.dart

import 'package:flutter/material.dart';
import 'package:samplewebviewapplication/webview.dart';
 
void main() => runApp(MyApp());
 
// ignore: use_key_in_widget_constructors
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Webview Project',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home:  const WebviewMobileApplication(),
      debugShowCheckedModeBanner: false,
    );
  }
}

webview.dart

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
 
class samplewebviewapplication extends StatelessWidget {
  const samplewebviewapplication({Key? key}) : super(key: key);
 
  @override
  Widget build(BuildContext context) {
    return _buildWebView();
  }
 
  Widget _buildWebView() {
    return const WebView(
      javascriptMode: JavascriptMode.unrestricted,
      initialUrl: 'https://webrtc.github.io/samples/src/content/devices/input-output/',
    );
  }
}

The generated APK when installed cannot turn on the camera or microphone option. I've added the following in the AndoroidManifest.xml file as well.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.AUDIO_CAPTURE"/>
<uses-permission android:name="android.permission.CAMERA"/>

Currently the web camera is not getting enabled or the microphone. I see there is a need to use the Permissionhandler in flutter for this. But just don't see samples to get going.

I've seen this solution from the community before here using InAppWebview but its not helping either, the code renders a lot of issue using that library. Let me know if there is a better solution to handle this.

Vibin Guevara
  • 778
  • 10
  • 27

1 Answers1

-2

You need to request camera and microphone (if needed) permissions, for example using the permission_handler plugin:

import 'package:permission_handler/permission_handler.dart';

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

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

  runApp(MyApp());
}

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

<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" />
Ayoub Benayache
  • 1,046
  • 12
  • 28