Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exo player add raise/send analytics event total time spent

I Want to add analytics events to exo player need following data

  • total time spent on video
  • total play time
  • total pause time
  • how many time paused

How this can be done

Tried following callbacks but not able to find exact solution to this

Player.EventListener

or I need to use AnalyticsListener?

Update on this found that exo player library version 2.12.0 has included PlaybackStatsListener which gives most of required analytics data but data is not accurate

playbackStatsListener.playbackStats.totalPlayTimeMs
playbackStatsListener.playbackStats.totalPausedTimeMs

these two fields are not showing accurate or reliable values.

like image 928
alphanso Avatar asked Dec 10 '25 00:12

alphanso


1 Answers

Here is a tested piece of code on how you could use the AnalyticsListener with onIsPlayingChanged to achieve this

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)
    }
}
like image 93
Biscuit Avatar answered Dec 11 '25 14:12

Biscuit