How to implement Exoplayer 2.11.1 in android?

I also faced this problem and this my solution

Declaration

private val exoPlayer: SimpleExoPlayer by lazy { SimpleExoPlayer.Builder(this).build()}

Play Song

private fun prepareExoPlayerFromURL(url: String) {
        val dataSourceFactory =
            DefaultDataSourceFactory(this, Util.getUserAgent(this, resources.getString(R.string.app_name)), null)
        val extractorsFactory = DefaultExtractorsFactory()
//        val audioSource = ExtractorMediaSource(uri, dataSourceFactory, extractorsFactory, null, null)

        val concateMediaSource = ConcatenatingMediaSource()

        // to play from song list
        for (i in mSongList) {      // song list song arraylist
            val mediaSource = ProgressiveMediaSource
                .Factory(
                    DefaultDataSourceFactory(this, dataSourceFactory),
                    DefaultExtractorsFactory()
                )
                .createMediaSource(Uri.parse(i.musicFile)/*Uri.parse(i.uri)*/)
            concateMediaSource.addMediaSource(mediaSource)
        }

        // to play single song
       /* val audioSource = ProgressiveMediaSource
            .Factory(
                DefaultDataSourceFactory(this, dataSourceFactory),
                DefaultExtractorsFactory()
            )
            .createMediaSource(Uri.parse(url))*/

        exoPlayer.prepare(concateMediaSource/*audioSource*/)
        exoPlayer.seekToDefaultPosition(songPosition)
        exoPlayer.playWhenReady = true

        setNotification()
    }

to set listener of player and notification

private fun setListoner() {
        exoPlayer.addListener(object : Player.EventListener {
            override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
                when (playbackState) {
                    Player.STATE_BUFFERING -> Log.e(TAG,"STATE_BUFFERING")
                    Player.STATE_ENDED -> Log.e(TAG,"STATE_ENDED")
                    Player.STATE_IDLE -> Log.e(TAG,"STATE_IDLE")
                    Player.STATE_READY ->{
                        if (playWhenReady) {
                            Log.e(TAG, "PlaybackStatus.PLAYING")
                        } else {
                            Log.e(TAG, "PlaybackStatus.PAUSED")
                        }
                    }
                    else -> Log.e(TAG,"PlaybackStatus.IDLE")
                }
            }
        })
    }

to Kill player

 private fun onDestroy() {
        if (exoPlayer != null) {
            exoPlayer.release()
            exoPlayer = null
            mediaSource = null
        }
    }

for more detail you can see official documentation

The Universal Music Player uses ExoPlayer for local audio playback.

Building feature-rich media apps with ExoPlayer (Google I/O '18)


For Java Guys (Long Live Java)

In Activity

private PlayerView epPlayerView = findViewById(R.id.design_reference);

The Public Function

public static void runExoPlayer(PlayerView epPlayerView,
                                String url,
                                Context context) {

    Uri videoUri = Uri.parse(url);

    SimpleExoPlayer  exoPlayer = new SimpleExoPlayer.Builder(context).build();
    epPlayerView.setPlayer(exoPlayer);

    MediaItem mediaItem = MediaItem.fromUri(videoUri);
    exoPlayer.clearMediaItems();
    exoPlayer.setMediaItem(mediaItem);
    exoPlayer.prepare();
    exoPlayer.setPlayWhenReady(true);

}

Build Gradle

   // Exo Media Player
    implementation 'com.google.android.exoplayer:exoplayer:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-core:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-dash:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-hls:2.15.1'
    implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.15.1'
    androidTestImplementation 'androidx.test:rules:1.4.0'

With the new update, you can create a simple player instance using SimpleExoPlayer.Builder:

simpleExoplayer = SimpleExoPlayer.Builder(context).build()

You can also supply the Builder with different arguments. See https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/SimpleExoPlayer.Builder.html

You can use this simple custom class I created to help you get started.

class ExoPlayerHelper(
    private val playerView: PlayerView,
    onError: (ExoPlaybackException) -> Unit,
    onPlayerBuffer: (Boolean) -> Unit
) {

    private var exoPlayer: ExoPlayer? = null
    private var mediaSource: ProgressiveMediaSource? = null

    private val playerListener = object : Player.EventListener {
        override fun onPlayerError(error: ExoPlaybackException) {
            super.onPlayerError(error)
            onError(error)
        }

        override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
            super.onPlayerStateChanged(playWhenReady, playbackState)
            onPlayerBuffer(playbackState == Player.STATE_BUFFERING)
        }
    }

    fun initializePlayer(url: String) {
        exoPlayer = SimpleExoPlayer.Builder(playerView.context).build()
        exoPlayer!!.repeatMode = Player.REPEAT_MODE_ALL
        exoPlayer!!.addListener(playerListener)

        playerView.player = exoPlayer

        val userAgent =
            Util.getUserAgent(playerView.context, playerView.context.getString(R.string.app_name))
        mediaSource = ProgressiveMediaSource
            .Factory(
                DefaultDataSourceFactory(playerView.context, userAgent),
                DefaultExtractorsFactory()
            )
            .createMediaSource(Uri.parse(url))

        exoPlayer!!.prepare(mediaSource!!, true, false)
        exoPlayer!!.playWhenReady = true
    }

    private fun killPlayer() {
        if (exoPlayer != null) {
            exoPlayer!!.release()
            exoPlayer = null
            mediaSource = null
            playerView.player = null
        }
    }
}