Android
Android integration
When integrating a WebView with WebRTC into your native application, there are several key elements and permissions that need to be declared in the AndroidManifest.xml file.
Permissions
Permissions are declared in the manifest file using the <uses-permission> element. For a WebView with WebRTC, you typically need to declare the following permissions:
INTERNET: Allows your application to open network sockets. This is necessary for the WebView to load web content.
<uses-permission android:name="android.permission.INTERNET" />CAMERA: Allows your application to use local device camera. This is required for WebRTC pages that capture images or video.
<uses-permission android:name="android.permission.CAMERA" />RECORD_AUDIO: Allows your application to use local device audio input. This is required for WebRTC pages that capture audio.
<uses-permission android:name="android.permission.RECORD_AUDIO" />MODIFY_AUDIO_SETTINGS: It's needed for WebRTC to control the audio settings for optimal communication. Pay additional attention, that only RECORD_AUDIO permissions are not enough to let application properly using audio channels
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />Features
This element is used to declare hardware or software features used by the application, which can affect whether or not the application can be installed on a device. For a WebView with WebRTC, you typically need to declare the following feature:
android.hardware.camera: Indicates that the application uses the device's camera
android.hardware.audio: Indicates that the application uses the device's microphone
If you are only going to have a viewer experience, then there is no need to require either camera or audio features
Layout
Locate WebView in your activity layout. In this case, it will be activity_main
Activities
For a WebView with WebRTC, you need to declare the activity that hosts the WebView:
Replace MainActivity with the name of your activity that hosts the WebView.
Make changes in AndroidManifest.xml file with the necessary permissions and features for a WebView with WebRTC:
Replace YourAppTheme and MainActivity with the theme and activity names used in your application.
In the MainActivity check for camera and record audio permissions first. The REQUEST_CAMERA_MIC_PERMISSION constant is used as a request code for permission requests. If permissions are not granted then WebView will not work as expected.
Permission Request
In the onCreate method, the app checks if it has the necessary permissions (camera and microphone). If not, it requests these permissions.
Handling Permission Request Result
The onRequestPermissionsResult method handles the result of the permission request. If the permissions are granted, the app can proceed with accessing the camera and microphone. If not, the app should handle the denial appropriately.
WebView Configuration
In the onCreate method, the WebView is configured. JavaScript is enabled, DOM storage is enabled, and media playback does not require user gestures. A WebChromeClient is set to handle permission requests within the WebView. Finally, the WebView loads a specific URL.
MainActivity
Last updated
Was this helpful?