Showing Videos in Android with Videoview with Exoplayer(with Latest updates)

When we want to load video from web,local store,project folder we wonder that how that can be achieved.
When you tried to google it,it shows so many result to achieve this but how to decide which on to use?
If you are struggling with so many deprecated and outdated code on google

Here is solution of your problem, I have written a sample app where you can decide what to choose and its latest code
Mainly, We have two option to choose
1) Android Video View
2) ExoPlayer
Both the player are working great but if you want more customization and easy use you can go ahead with Exoplayer.
Advantages of Exoplayer is, it’s more flexible and easy to customize compared to videoview and it is much more stable and has a lot of other functionalities which videoview does not, Like take a simple use case of you needing to play videos in a sequence, So you can do that pretty much easily in Exoplayer with “ConcatinatingMediaSource” and in the latest version, it has become much more flexible and easy to do but to do the same thing in videoview you had to do a lot of things to make it work. So it’s a more advanced and flexible version of videoview which has is a lot more
You can use VideoView and Exoplayer according to your requirement.if there is simple playback you can use video view and if you want customization you can use exoplayer.
- VideoView.
To Load video from raw directory for Videoview.
I have used viewbinding here so I am using binding object you can check that how i have done that from that link.
Add video view to your activity xml file
Activity.xml file
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent">
Add this code to oncreate method of your activity file
// Use the resource id
int rawId = getResources().getIdentifier(file_name_without_extension, "raw", getPackageName());
// URI formation
String path = "android.resource://" + getPackageName() + "/" + rawId;MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
// Set the URI to play video file
binding.videoView.setVideoURI(Uri.parse(path));
videoView.start();
That’s it this will load your video file to video view.
2. Exoplayer
I have updated and added all latest changes from Exoplayer so you can directly use this code.
To Load Video using Exoplayer
Add this dependency to your Module gradle file
dependencies {
implementation 'com.google.android.exoplayer:exoplayer:2.16.1'
}
Check your Java support code it should be 1.8+,check this code in your gradle file in android block.
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
}
Now its ready to use.
in .Xml file of your activity add this code
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="0dp" />
in Activity.kt Code file of your activity Oncreate()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityExoPlayerSampleBinding.inflate(layoutInflater)
setContentView(binding.root)
val player = ExoPlayer.Builder(this)
.build()
val uri = RawResourceDataSource.buildRawResourceUri
(R.raw.file_example)
player.setMediaItem(MediaItem.fromUri(uri))
//player.addListener(this) if you want to show something on status of video playing add this and implement methods. binding.videoView.player = player
player.prepare()
player.play()
}
That’s it it will load media file to exoplayer and play
And if you want to play play multiple media file use this code.
MediaItem firstItem = MediaItem.fromUri(firstVideoUri);
MediaItem secondItem = MediaItem.fromUri(secondVideoUri);
// Add the media items to be played.
player.addMediaItem(firstItem);
player.addMediaItem(secondItem);
// Prepare the player.
player.prepare();
// Start the playback.
player.play();
Note: Prior to ExoPlayer 2.12, the player needed to be given a MediaSource
rather than media items. From 2.12 onwards, the player converts media items to the MediaSource
instances that it needs internally. Read more about this process and how it can be customized on the Media sources page. It’s still possible to provide MediaSource
instances directly to the player using ExoPlayer.setMediaSource(s)
and ExoPlayer.addMediaSource(s)
.
In next Article we will load Media file from server by streaming it with Exoplayer.
Happy Coding…. (:})