Voiced by Amazon Polly |
Introduction
ExoPlayer is an open-source media playback library for android by Google, written in Java. It is an application-level media player which is used to play audio and video both locally and over the internet in android applications. Over 140000 android applications in the Google play store are using ExoPlayer including YouTube, Netflix, Facebook, and WhatsApp. It is designed in a way that is highly customizable, flexible, and easy to use. As it is a library, we can use any of its versions as per our application requirement and we can update it by updating this library.
Customized Cloud Solutions to Drive your Business Success
- Cloud Migration
- Devops
- AIML & IoT
ExoPlayer Vs MediaPlayer
Steps to Implement
We are creating a simple player using the ExoPlayer library using Java as a programming language. We will fetch the video from the URL and play the video inside our ExoPlayer.
Step-1
- Create a new project in the android studio with “No Activity”, give any name to your project, and choose Java as a programming language.
Step-2
- Add the dependency of ExoPlayer in the build.gradle(Module:app)
- There are different dependencies for core support, DASH support, HLS support, and UI support.
- Add the dependency of UI and Core with the latest version for the basic Implementation of ExoPlayer.
1 2 |
implementation 'com.google.android.exoplayer:exoplayer-ui:2.18.1' implementation 'com.google.android.exoplayer:exoplayer-core:2.18.1' |
Then enable the view binding by adding the following code in the same file.
1 2 3 4 |
viewBinding { enabled=true; } |
- After adding the dependency sync the project.
Step-3
- Add permission for internet access to our application.
- Navigate to AndroidManifest.xml inside the manifest folder of our app and add the following permissions.
1 2 |
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> |
- Add the following parameter in the application tag of this file to allow the HTTP & Cleartext Traffic on our application.
1 |
android:usesCleartextTraffic="true" |
Step-4
- Implementation of ExoPlayer in XML file.
- Navigate to app > res > layout > activity.xml and add the following code inside that XML file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> // PlayerView of ExoPlayer <com.google.android.exoplayer2.ui.StyledPlayerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/player_view" android:keepScreenOn="true" app:use_controller="true" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> |
- We are using the StyledPlayerView for a player view of our ExoPlayer which is the latest provided by the ExoPlayer library. Here we are using the default playback controls of our ExoPlayer but can also use our customized playback controls. For customized controls, create a new XML file in the layout folder and write the XML code as our requirement and replace the parameter app:use_controller=”true” with app:controller_layout_id=”@layout/ (XML filename)”.
Step-5
- Implementation of ExoPlayer in MainActivity.java.
- Navigate to app > java > com.example.(app_name) > MainActivity and add the following code in that file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
//com.example.blog is my package name, change it with your package name package com.example.blog; import androidx.appcompat.app.AppCompatActivity; import com.google.android.exoplayer2.ExoPlayer; import android.net.Uri; import android.os.Bundle; import com.example.blog.databinding.ActivityMainBinding; import com.google.android.exoplayer2.MediaItem; public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding=ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); //Creating the object of Exoplayer ExoPlayer player = new ExoPlayer.Builder(this).build(); //Binding the playerview with the Exoplayer object binding.playerView.setPlayer(player); //It is a Demo Video URL that is used as a media item in Exoplayer String videoURL = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"; //Parsing the Video URL to VideoURI Uri videouri = Uri.parse(videoURL); //Creating an object of MediaItem and adding the videoURI MediaItem mediaItem = MediaItem.fromUri(videouri); //Setting the MediaItem to our Player player.setMediaItem(mediaItem); //Preparing the player player.prepare(); //Playing the Video player.play(); } } |
Output-
Conclusion
We implemented the ExoPlayer Application with default playback controls and with basic functionality. The important feature of the ExoPlayer is that it is highly customizable. Its library has lots of classes and predefined functions which we can use to customize our application as per our needs. There is an ExoPlayer official website available where we can read the official documentation of this library and it also provides a JavaDoc Overview (ExoPlayer library) where we can find the explanation of all the packages, classes, and functions of this library.
Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.
- Cloud Training
- Customized Training
- Experiential Learning
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon EMR and many more.
FAQs
1. Is it supported on all android devices?
ANS: – No, Exoplayer is supported in android devices having an android version of more than 4.1 and an android API level of more than 16.
2. Can I use ExoPlayer to directly play YouTube videos?
ANS: – ExoPlayer cannot play YouTube videos, i.e., those with URLs like https://www.youtube.com/watch?v=.

WRITTEN BY Rohit Lovanshi
Rohit Lovanshi works as a Research Associate (Infra, Migration, and Security Team) at CloudThat. He is AWS Developer Associate certified. He has a positive attitude and works effectively in a team. He loves learning about new technology and trying out different approaches to problem-solving.
Comments