我在 github 中搜索了两天这个问题,但我找不到真正的答案。我想要在
pause / resume
中检测 ExoPlayer > 2.x
的示例。
任何人都可以给我一个例子吗?我检查了onPlayerStateChanged
,但问题没有解决。
onPlayerStateChanged : STATE_BUFFERING
onPlayerStateChanged : STATE_READY
我刚从
onPlayerStateChanged
那里得到这个日志,这并不是一直被调用的!
编辑---
请参考
Player.isPlaying()
方法,它提供了这个作为API。
“不必单独检查这些属性,可以调用 Player.isPlaying。”
https://exoplayer.dev/listening-to-player-events.html#playback-state-changes
---编辑结束
您需要使用
Player.EventListener检查
playWhenReady
。 ExoPlayer的播放状态与播放器是否暂停无关:
player.addListener(new Player.DefaultEventListener() {
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playWhenReady && playbackState == Player.STATE_READY) {
// media actually playing
} else if (playWhenReady) {
// might be idle (plays after prepare()),
// buffering (plays when data available)
// or ended (plays when seek away from end)
} else {
// player paused in any state
}
}
});
播放/暂停 ExoPlayer 提供的播放器
player.setPlayWhenReady(boolean)
带有媒体文件的 ExoPlayer 的播放状态序列从不停止重新缓冲,在四种状态中的每一种中都有一次,并且不表示播放/暂停:
Player.STATE_IDLE;
Player.STATE_BUFFERING;
Player.STATE_READY;
Player.STATE_ENDED;
每次玩家需要缓冲时:
Player.STATE_READY;
Player.STATE_BUFFERING;
Player.STATE_READY;
设置playWhenReady不影响状态您的媒体实际上正在播放时
playWhenReady && playbackState == Player.STATE_READY
准备就绪后播放。 :)
public boolean isPlaying() {
return exoPlayer.getPlaybackState() == Player.STATE_READY && exoPlayer.getPlayWhenReady();
}
override fun onIsPlayingChanged(isPlaying: Boolean) {
// your code here
}
如果 isPlaying 为假,则暂停,否则播放。
这就是我检测播放/暂停按钮点击所做的,效果完美。我有同样的要求来检测exoplayer播放/暂停按钮的点击事件。以上答案主要是在谈论状态而不是 关于按钮点击事件。
第 1 步:创建自定义控件调度程序类并重写方法dispatchSetPlayWhenReady
class PlayerControlDispatcher : DefaultControlDispatcher() {
override fun dispatchSetPlayWhenReady(player: Player?, playWhenReady: Boolean): Boolean {
if(playWhenReady) {
// Play button clicked
} else {
// Paused button clicked
}
return super.dispatchSetPlayWhenReady(player, playWhenReady)
}
}
第2步:将自定义控件调度程序类PlayerControlDispatcher
设置到播放器视图中。
playerView.setControlDispatcher(PlayerControlDispatcher())
其中 playerView 是我们在布局文件中声明的
com.google.android.exoplayer2.ui.PlayerView
的一个实例。
Kotlin 2020 解决方案更新
状态变化和播放错误等事件将报告给已注册的Player.EventListener
实例。
Player.EventListener
默认方法为空,所以你只需要实现你感兴趣的方法即可。 首先你的班级,说你的活动,
必须符合Player.EventListener
界面。然后在类上覆盖
onIsPlayingChanged
方法。外面
onCreate
方法...将监听器添加到您的播放器实例:
// Adding player listener for tracking events
player?.addListener(this)
可以用Player.isPlaying检查玩家是否在玩(即位置在前进):
//Listening to player events
override fun onIsPlayingChanged(isPlaying: Boolean){
if (isPlaying) {
// Active playback.
} else {
// Not playing because playback is paused, ended, suppressed, or the player
// is buffering, stopped or failed. Check player.getPlaybackState,
// player.getPlayWhenReady, player.getPlaybackError and
// player.getPlaybackSuppressionReason for details.
}
}
就是这样。很简单。
exoplayer.getPlayWhenReady()
查看播放器当前是处于暂停状态还是播放状态。
fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int)
来自Player.Listener
exoPlayer.addListener(object : Player.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) {
if (isPlaying){
videoIsPlaying()
}
}
})
videoView.setControlDispatcher(object : DefaultControlDispatcher() {
override fun dispatchSetPlayWhenReady(player: Player, playWhenReady: Boolean): Boolean {
if (playWhenReady)
// Tap on Play button
else
// Tap on Pause button
return super.dispatchSetPlayWhenReady(player, playWhenReady)
}
})
private fun ExoPlayer.isPlaying() =
playbackState == Player.STATE_READY && playWhenReady
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
}
}
}
})