TransWikia.com

Exo player add raise/send analytics event total time spent

Stack Overflow Asked by alphanso on December 4, 2020

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.

2 Answers

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
}

Answered by abhi kumar on December 4, 2020

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)
    }
}

Answered by Biscuit on December 4, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP