Requesting permissions
Because our app uses audio and video from the user's device, we’ll need to add some code to request audio and video permissions. We'll use the EasyPermissions library to do this.
- Start by adding the EasyPermissions library to your project — open the build.gradle for your module (the app/build.gradle file) and add the following code snippet to the dependencies section:
implementation 'pub.devrel:easypermissions:3.0.0'
- In your AndroidManifest.xml file, add this code snippet inside the
manifesttags:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
ℹ️ Note: The WAKE_LOCK permission is used to keep the device from sleeping during a video call. This ensures uninterrupted media streaming.
- In your MainActivity.java file, add a new method called
onRequestPermissionsResult:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
This is boilerplate code to use the EasyPermissions library.
Important: You will have to add imports manually, by clicking EasyPermissions (red text) and pressing the key combination Option + Enter on MacOS or Alt + Enter on Windows. This step may be required when pasting the code. You can also enable "Add unambiguous imports on the fly" option (Preferences | Editor | Auto Import) to add imports automatically.
- Add the
PERMISSIONS_REQUEST_CODEproperty at the top of the MainActivity.java file:
private static final int PERMISSIONS_REQUEST_CODE = 124;
- Next we'll add a
requestPermissions()method:
@AfterPermissionGranted(PERMISSIONS_REQUEST_CODE)
private void requestPermissions() {
String[] perms = { Manifest.permission.INTERNET, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO };
if (EasyPermissions.hasPermissions(this, perms)) {
// initialize and connect to the session
} else {
EasyPermissions.requestPermissions(this, "This app needs access to your camera and mic to make video calls", PERMISSIONS_REQUEST_CODE, perms);
}
}
This checks if the permissions have already been granted. If they haven’t, we prompt the user for camera and mic permissions with the EasyPermissions.requestPermissions method.
Once permissions have been granted, this method will fire again due to the @AfterPermissionGranted(PERMISSIONS_REQUEST_CODE) annotation. We'll add some code to initialize the session and view objects in the coming steps.
- Now add
requestPermission();to call the method inside theonCreate()method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions();
}
Basic video chat
Learn the basic concepts of the Vonage Video API platform, including how users can communicate through video, voice, and messaging. Explore a basic Vonage Video API flow.