Embed Video in Android Applications
Embedding videos in Android applications requires the use of the WebView class. To allow EnableX to access the camera on the Android device, perform the following steps:
- Override
WebChromeClient.onPermissionRequest
method in the WebView class. - Add the
?skipMediaPermissionPrompt
Query String parameter to allow EnableX the camera access on the Android device.
Device Accessibility
Perform the following steps to grant the required permissions to WebView to access the device camera:
-
Permit WebView to access the device camera by adding the following permissions to the manifest file:
<uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /><uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.WAKE_LOCK" /> -
Override the
onPermissionRequest
method and customizeWebChromeClient
by using the Webview class:import android.app.Activity;import android.webkit.PermissionRequest;import android.webkit.WebChromeClient;public class CustomWebChromeClient extends WebChromeClient {private Activity activity;public CustomWebChromeClient(Activity parentActivity) {super();activity = parentActivity;}@Overridepublic void onPermissionRequest(final PermissionRequest request) {activity.runOnUiThread(() -> request.grant(request.getResources()));}} -
Create a
WebUtils
helper class to configure WebView.For example,
import android.annotation.SuppressLint;import android.webkit.CookieManager;import android.webkit.WebSettings;import android.webkit.WebView;public class WebUtils {@SuppressLint("SetJavaScriptEnabled")public static void configureWebView(WebView webView) {WebSettings webSettings = webView.getSettings();webSettings.setJavaScriptEnabled(true);webSettings.setDomStorageEnabled(true);webSettings.setDatabaseEnabled(true);webSettings.setMediaPlaybackRequiresUserGesture(false);CookieManager.getInstance().setAcceptCookie(true);CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);}} -
Set up your activity.
-
Configure WebView.
-
Request device permissions if needed.
-
Add the ?skipMediaPermissionPrompt` parameter to the room URL and load it.
package com.meeting.webview;import android.Manifest;import android.content.pm.PackageManager;import android.os.Build;import android.os.Bundle;import android.webkit.WebView;import android.webkit.WebViewClient;import androidx.annotation.NonNull;import androidx.annotation.RequiresApi;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;import java.util.List;public class MeetingNew extends AppCompatActivity {public String roomUrl = ""; // Replace by your ownUrlprivate static final int PERMISSION_REQUEST_CODE = 1;private String[] requiredPermissions = {Manifest.permission.CAMERA,Manifest.permission.MODIFY_AUDIO_SETTINGS,Manifest.permission.RECORD_AUDIO};private WebView webView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.meeting_webview);this.webView = findViewById(R.id.webView);WebUtils.configureWebView(this.webView);this.webView.setWebChromeClient(new CustomWebChromeClient(this));this.webView.setWebViewClient(new WebViewClient());}@Overrideprotected void onResume() {super.onResume();if (this.webView.getUrl() == null) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && this.isPendingPermissions()) {// This explicitly requests the camera and audio permissions.// It's fine for a demo app but should probably be called earlier in the flow,// on a user interaction instead of onResume.this.requestCameraAndAudioPermissions();} else {this.loadLowCodeRoomUrl();}}}private void loadLowCodeRoomUrl() {this.webView.loadUrl(roomUrl);}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {switch (requestCode) {case PERMISSION_REQUEST_CODE:if (this.grantResultsContainsDenials(grantResults)) {// Show some permissions required dialog.} else {// All necessary permissions granted, continue loading.this.loadLowCodeRoomUrl();}break;default:super.onRequestPermissionsResult(requestCode, permissions, grantResults);}}@RequiresApi(api = Build.VERSION_CODES.M)private void requestCameraAndAudioPermissions() {this.requestPermissions(this.getPendingPermissions(), PERMISSION_REQUEST_CODE);}@RequiresApi(api = Build.VERSION_CODES.M)private String[] getPendingPermissions() {List<String> pendingPermissions = new ArrayList<>();for (String permission : this.requiredPermissions) {if (this.checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED) {pendingPermissions.add(permission);}}return pendingPermissions.toArray(new String[pendingPermissions.size()]);}private boolean isPendingPermissions() {if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {return false;}return this.getPendingPermissions().length > 0;}private boolean grantResultsContainsDenials(int[] grantResults) {for (int result : grantResults) {if (result == PackageManager.PERMISSION_DENIED) {return true;}}}}
-
File Download
Perform the following steps:
-
Create a method for decoding the download file in the base64 format and save the converted file in a particular location.
Sample Code
public String createAndSaveFileFromBase64Url(String url) {final int notificationId = 1;File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);String filetype = url.substring(url.indexOf("/") + 1, url.indexOf(";"));String filename = System.currentTimeMillis() + "." + filetype;File file = new File(path, filename);try {if(!path.exists())path.mkdirs();if(!file.exists())file.createNewFile();String base64EncodedString = url.substring(url.indexOf(",") + 1);byte[] decodedBytes = Base64.decode(base64EncodedString, Base64.DEFAULT);OutputStream os = new FileOutputStream(file);os.write(decodedBytes);os.close();} catch (IOException e) {Log.w("ExternalStorage", "Error writing " + file, e);Toast.makeText(getApplicationContext(),"", Toast.LENGTH_LONG).show();}return file.toString();} -
Call this method in webview DownloadListner.
webView.setDownloadListener(new DownloadListener() {public void onDownloadStart(String url, String userAgent, StringcontentDisposition, String mimetype, long contentLength) {if (url.startsWith("data:")) { //when url is base64 encoded dataString path = createAndSaveFileFromBase64Url(url);return;}}
After setting up Video Embed, you can add new features to it, customize its look and feel, and integrate it with other services and applications. For more information, see Integration Capabilities.