ExoPlayer is an application-level media player for Android. It provides an alternative to Android’s MediaPlayer API for playing audio and video both locally and over the Internet. ExoPlayer supports features not currently supported by Android’s MediaPlayer API, including DASH and SmoothStreaming adaptive playbacks. Unlike the MediaPlayer API, ExoPlayer is easy to customize and extend, and can be updated through Play Store application updates.

Exoplayer is used in many popular video playing apps such as, YouTube, Netflix, Amazon Prime Video Player, Hotstar.

Let’s get started

So, let’s start with first things first:

1: Add dependencies

implementation 'com.google.android.exoplayer:exoplayer-core:2.18.1'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.18.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.18.1'

2: Sync project

  • sync your gradle
  • add internet access to your manifest file (which you probably might have done)

Great now lets get to the coding part for ExoPlayer:

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.ui.StyledPlayerView;

// now lets do instantiate ExoPlayer
ExoPlayer player = new ExoPlayer.Builder(this, renderersFactory ).setLoadControl(loadControl).build();
//further coding
StyledPlayerView exoPlayerView ;
exoPlayerView = findViewById(R.id.idExoPlayerVIew);
exoPlayerView.setPlayer(player);
Uri videoUri = Uri.parse(VideoUrl);
MediaItem mediaItem = MediaItem.fromUri(videoUri);
player.setMediaItem(mediaItem);
player.prepare();
player.play();

Now code structure for xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AddContext">

    <!--Widget for exoplayer view-->
    <com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@+id/idExoPlayerVIew"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

See Also: Latest posts on AlltuFaaltu 

Your Comments