我想将分析事件添加到exo播放器需要以下数据
- 用于视频的总时间
- 总播放时间
- 总暂停时间
- 暂停了多少次
如何进行
尝试了以下回调,但无法找到此的确切解决方案
Player.EventListener
或者我需要使用AnalyticsListener?
更新后发现,exo播放器库版本2.12.0包含PlaybackStatsListener,它提供了大部分所需的分析数据,但数据不准确
playbackStatsListener.playbackStats.totalPlayTimeMs
playbackStatsListener.playbackStats.totalPausedTimeMs
这两个字段没有显示出准确或可靠的值。
以下是一段经过测试的代码,介绍如何使用AnalyticsListener
和onIsPlayingChanged
来实现此
private var playTime = 0L // in ms
private var pauseTime = 0L // in ms
private var totalTime = 0L // in ms
private var pressedPaused = 0
private val analyticsListener: AnalyticsListener = object : AnalyticsListener {
private var initTime = 0L
override fun onIsPlayingChanged(eventTime: AnalyticsListener.EventTime, isPlaying: Boolean) {
if(isPlaying) {
if(initTime != 0L) pauseTime += System.currentTimeMillis() - initTime
initTime = System.currentTimeMillis()
} else {
if(initTime != 0L) playTime += System.currentTimeMillis() - initTime
initTime = System.currentTimeMillis()
pressedPaused++
}
totalTime = playTime+pauseTime
Log.e("onIsPlaying", "PLAYTIME: $playTime")
Log.e("onIsPlaying", "PRESSEDPAUSE: $pressedPaused")
Log.e("onIsPlaying", "PAUSETIME: $pauseTime")
Log.e("onIsPlaying", "TOTALTIME: $totalTime")
super.onIsPlayingChanged(eventTime, isPlaying)
}
}
private var mPlayTime = 0L
private var mPlayTotalTime = 0L
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
if (playbackState == 3) {
if (playWhenReady) {
mPlayTime = System.currentTimeMillis()
} else {
if (mPlayTime.toInt() != 0) {
val mPauseTime = System.currentTimeMillis() - mPlayTime
mPlayTotalTime += mPauseTime
mPlayTime = 0
}
}
} else {
if (mPlayTime.toInt() != 0) {
val mPauseTime = System.currentTimeMillis() - mPlayTime
mPlayTotalTime += mPauseTime
mPlayTime = 0
}
}
val mTotalDurationInSec = (mPlayTotalTime / 1000) % 60
}