'Player.EventListener is deprecated how to use instead of Player.Listener in Java

In Exoplayer version 2.14.1 Playerd.EventListener() is deprecated. when I read docs its says use instead of Player.Listener, but I have no idea how to use that method like below code.

simpleExoPlayer.addListener(new Player.EventListener() {
            @Override
            public void onPlaybackStateChanged(int state) {
                if (state == simpleExoPlayer.STATE_READY) {

                    aspectRatioFrameLayout.setAspectRatio(16f / 9f);
                } else {
                    playerView.hideController();
                }
            }
        });


Solution 1:[1]

As per there Documentation you would want something like:

simpleExoPlayer.addListener(new Player.Listener() {
        @Override
        public void onPlaybackStateChanged(@State int state) {
            if (state == Player.STATE_READY) {

                aspectRatioFrameLayout.setAspectRatio(16f / 9f);
            } else {
                playerView.hideController();
            }
        }
    });

Solution 2:[2]

In Kotlin use this code, for more details refer to this Documentation

 player!!.addListener(object : Player.Listener { // player listener

            override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
                when (playbackState) { // check player play back state
                    Player.STATE_READY -> {
                      aspectRatioFrameLayout.setAspectRatio(16f / 9f)
                    }
                    Player.STATE_ENDED -> { 
                         //your logic 
                    }
                    Player.STATE_BUFFERING ->{ 
                         //your logic 
                    }
                    Player.STATE_IDLE -> { 
                         //your logic 
                    }
                    else -> {
                       playerView.hideController()
                    }
                }
            }
        })

Solution 3:[3]

        exoPlayer.addListener(new Player.Listener() {
            public void onPlaybackStateChanged(int playbackState) {
                if(playbackState == Player.STATE_ENDED){
                    ..............
                }
            }
        });

This works for me on March 2022

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DevWithZachary
Solution 2 Tippu Fisal Sheriff
Solution 3 Arun Kumar