在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();
}
}
});
根据那里的文档,您可能想要这样的东西:
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中使用此代码,有关更多详细信息,请参阅此文档
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月
现在在播放器版本2中,它已经更改了
player?.addListener(object :Player.EventListener{
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
super.onPlayerStateChanged(playWhenReady, playbackState)
if (playbackState == Player.STATE_READY) {
} else {
}
}
})
您应该使用新版本:com.google.android.exoplayer:exoplayer:2.18.6
此函数已弃用:
@Deprecated
default void onPlayerStateChanged(boolean playWhenReady, @State int playbackState) {}
新功能是:
default void onPlaybackStateChanged(@State int playbackState) {}
新用途:
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
}
}
}
})