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.
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
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
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
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, REQUEST_CAMERA_MIC_PERMISSION);
}
}
In the onCreate method, the app checks if it has the necessary permissions (camera and microphone). If not, it requests these permissions.
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.
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
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CAMERA_MIC_PERMISSION = 123;
private WebView webView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check if the permissions are granted
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
// If not, request the permissions
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, REQUEST_CAMERA_MIC_PERMISSION);
} else {
// If granted
init() // initiate WebView and load url
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_CAMERA_MIC_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
// Permissions granted initiate WebView and load url
init()
} else {
// Permissions denied
finish() // close the application
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// Clear WebView
if(webView != null) {
webView.loadUrl("about:blank")
webView = null
}
}
@SuppressLint("SetJavaScriptEnabled")
private void init() {
webView = findViewById(R.id.web_view);
WebSettings webSettings = webView.getSettings();
// Enable JavaScript
webSettings.setJavaScriptEnabled(true);
// Enable DOM storage API
webSettings.setDomStorageEnabled(true);
// Allow video autoplay
webSettings.setMediaPlaybackRequiresUserGesture(false);
// Set a WebChromeClient that handles permission requests
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(final PermissionRequest request) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
request.grant(request.getResources());
}
});
}
});
webView.loadUrl("<<url to the relevant template>>");
}
}