How To Build A Video Streaming App For Android Native (Java/Kotlin)

TechTalks

Build A Video Streaming App For Android Native

In this post, we will look at how to build a video streaming app for Android Native EnableX Low-Code Video Embed and Android Native (Java/Kotlin).

To gain understanding about the Low Code Video Embed, read the following blogs:

 

Prerequisites

Create a Project

To start with, create a Video Embed Project in the EnableX portal:

  • Go to the EnableX portal. If you do not have an account, Signup here.
  • Next, “Create Projects”, enter relevant information.
  • Now, select “Video Embed” in the “Service” section.
  • Once you are done with project creation, “APP ID” and “APP Key” are generated. You will require these credentials (“APP ID” and “APP Key”) later to gain access in the EnableX Meeting Room.

Therefore, it is advisable to save this for further use (see Embed the Meeting URL).

Let’s Start Building

  • Open Android Studio and select – Start a new Android Studio project.
  • On the Choose your project panel, select > Phone and Tablet > Empty Activity, and then click Next.
  • Now, click Finish. Follow the on-screen instructions if you need to install any plug-ins.

Add Required App Permissions

Add required permissions (for camera/audio media) in the /App/Src/Main/AndroidManifest.Xml file for device access according to your needs:

Build A Video Streaming App For Android - pic-1

 

You need to use Webview class. Create custom WebChromeClient class and override onPermissionRequest method:

Build A Video Streaming App For Android Native - pic-2

 

Then, create a WebUtils helper class to configure the WebView. Here is a possible configuration:

Build A Video Streaming App For Android Native - pic-3

 

Finally, setup your activity following these steps:

  • Configure the webView.
  • Request the permissions if needed.
  • Add the? skipMediaPermissionPrompt parameter to the room URL and load it.

 

Here is an example:


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 MeetingActivity  extends AppCompatActivity {

public String roomUrl = " "; // Replace by your own
private String roomParameters = "?skipMediaPermissionPrompt";

private 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;

@Override
protected 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());
}

@Override
protected void onResume() {
super.onResume();
if (this.webView.getUrl() == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && this.isPendingPermissions()) {

this.requestCameraAndAudioPermissions();
} else {
this.loadLowCodeEmbedRoomUrl();
}
}
}

private void loadLowCodeEmbedRoomUrl() {
this.webView.loadUrl(roomUrl+ roomParameters);
}

@Override
public 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.loadLowCodeEmbedRoomUrl();
}
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;
}
}
return false;
}
}

 

Hey! You can go live now.

join meeting

To conclude

We hope you will find this blog useful and easy to follow, even if you have not built a livestreaming app before.

 

 

Are you looking for feature-rich APIs to build exciting solutions?

Sign up for free to begin!
Signup Cpaas API