Player.EventListener is deprecated how to use instead of Player.Listener in Java
Asked Answered
A

5

11

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();
                }
            }
        });
Acid answered 25/6, 2021 at 11:38 Comment(0)
I
20

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();
            }
        }
    });
Iridaceous answered 25/6, 2021 at 12:15 Comment(1)
@GaneshMB great! please mark my answer if you are happy :)Iridaceous
E
5

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()
                    }
                }
            }
        })
Endothelioma answered 2/7, 2021 at 9:15 Comment(3)
sorry Tippu currently I'm working with java but I appreciate you its also needs for laterAcid
Okay got it, hope it will use for other's.Endothelioma
onPlayerStateChanged deprecated. Use onPlaybackStateChanged(int) and onPlayWhenReadyChanged(boolean, int) instead exoplayer.dev/doc/reference/com/google/android/exoplayer2/…Apprise
H
1
        exoPlayer.addListener(new Player.Listener() {
            public void onPlaybackStateChanged(int playbackState) {
                if(playbackState == Player.STATE_ENDED){
                    ..............
                }
            }
        });

This works for me on March 2022

Hanky answered 19/3, 2022 at 1:54 Comment(0)
S
0

Now in player version 2 it has changed

player?.addListener(object :Player.EventListener{
    
    override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
        super.onPlayerStateChanged(playWhenReady, playbackState)
        if (playbackState == Player.STATE_READY) {
                        
        } else {
                       
        }
    }
})
Salon answered 4/7, 2022 at 8:30 Comment(0)
S
0

You should use new version: com.google.android.exoplayer:exoplayer:2.18.6

This function deprecated:

@Deprecated
    default void onPlayerStateChanged(boolean playWhenReady, @State int playbackState) {}

New function is:

 default void onPlaybackStateChanged(@State int playbackState) {}

New usage:

exoPlayer?.addListener(object : Player.Listener {
            override fun onPlaybackStateChanged(@Player.State state: Int) {
                when (state) {
                    Player.STATE_READY -> {
                        // The player is able to immediately play from its current position. The player will be playing if getPlayWhenReady() is true, and paused otherwise.
                    }
                    Player.STATE_BUFFERING -> {
                       // The player is not able to immediately play the media, but is doing work toward being able to do so. This state typically occurs when the player needs to buffer more data before playback can start.
                    }
                    Player.STATE_IDLE -> {
                       // The player is idle, meaning it holds only limited resources.The player must be prepared before it will play the media.
                    }
                    Player.STATE_ENDED -> { 
                       // The player has finished playing the media.
                    }
                    else -> {
                      // Other things
                    }
                }
            }
        })
Sharanshard answered 23/4, 2023 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.