Player.EventListener 已弃用 如何在 Java 中使用而不是 Player.Listener

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

在 Exoplayer 版本中 2.14.1 Playerd.EventListener() 已弃用。当我阅读文档时,它说使用而不是 Player.Listener,但我不知道如何像下面的代码那样使用该方法。

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

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

根据那里 Documentation 你会想要这样的东西:

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

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

Kotlin 中使用此代码,有关详细信息,请参阅此 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()
                    }
                }
            }
        })
        exoPlayer.addListener(new Player.Listener() {
            public void onPlaybackStateChanged(int playbackState) {
                if(playbackState == Player.STATE_ENDED){
                    ..............
                }
            }
        });

这对我 2022 年 3 月有效