How to Create a Dynamic Video Player in Android with Firebase Realtime Database?
Most of the apps use the video player to display so many videos inside their application. So for playing the video the app plays the video from its video URL. But what if we want to update that video on a real-time basis. So, in that case, we have to update our database and then later on we have to update our APK. So this is not an efficient way to do it. In this article, we will take a look at the implementation of the dynamic video player in Android using Firebase Realtime Database.
What we are going to build in this article?
We will be building a simple application in which we will be playing a video in an ExoPlayer and we will be loading a video inside our ExoPlayer from Firebase. Along with that, we will be able to change our video in runtime. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Connect your app to Firebase
After creating a new project. Navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot.
Inside that column Navigate to Firebase Realtime Database. Click on that option and you will get to see two options on Connect app to Firebase and Add Firebase Realtime Database to your app. Click on Connect now option and your app will be connected to Firebase. After that click on the second option and now your App is connected to Firebase.
After completing this process you will get to see the below screen.
Now verify that your app is connected to Firebase or not. Go to your build.gradle file. Navigate to the app > Gradle Scripts > build.gradle (app) file and make sure that the below dependency is added in your dependencies section.
After adding this dependency add the dependency of ExoPlayer in your Gradle file.
Step 3: Add the dependency for ExoPlayer View in build.gradle file
Navigate to the app > Gradle Scripts > build.gradle (app) file and add below dependency in it.
// dependency for exoplayer
// for core support in exoplayer.
// for adding dash support in our exoplayer.
// for adding hls support in exoplayer.
// for smooth streaming of video in our exoplayer.
// for generating default ui of exoplayer
After adding this dependency sync your project. Now we will move towards our XML part.
Step 4: Adding permission for the Internet
As we are loading our video from the internet so we have to add permissions for the Internet in the Manifest file. Navigate to the app > AndroidManifest.xml file and add the below permissions in it.
Step 5: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
Step 6: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Step 7: Adding URL for Video in your Firebase Console
For adding Video URL in Firebase Console. Browse for Firebase in your browser and Click on Go to Console option in the top right corner as shown in the below screenshot.
After clicking on Go to Console option you will get to see your project. Click on your project name from the available list of projects.
After clicking on your project. Click on the Realtime Database option in the left window.
After clicking on this option you will get to see the screen on the right side. On this page click on the Rules option which is present in the top bar. You will get to see the below screen.
In this project, we are adding our rules as true for reading as well as write because we are not using any authentication to verify our user. So we are currently setting it to true to test our application. After changing your rules. Click on the publish button at the top right corner and your rules will be saved there. Now again come back to the Data tab. Now we will be adding our data to Firebase manually from Firebase itself.
Inside Firebase Realtime Database. Navigate to the Data tab. Inside this tab on the database section click on the “+” icon. After clicking on the “+” icon you will get to see two input fields which are the Name and Value fields. Inside the Name field, you have to add the reference for your video file which in our case is “url“. And in our value field, we have to add the URL for our video file. After adding the value in this field. Click on the Add button and your data will be added to Firebase Console.
After adding the URL for your video. Now run your app and see the output of the app below:
You can change the URL of your video dynamically.
Please Login to comment...