AnswerBun.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!

Related Questions

form validation with data ranges

2  Asked on August 7, 2020 by eniol

   

How to format this output?

2  Asked on August 6, 2020 by shantanu-jain

     

uploading csv file django using a form

2  Asked on August 5, 2020 by sheraram_prajapat

       

Set focus to another Control

3  Asked on August 4, 2020 by binu

 

Animate gradient bar chart – matplotlib

1  Asked on August 4, 2020 by jonboy

       

c# error – Can not convert Array to byte array

1  Asked on August 2, 2020 by rakesh

         

Correcting Business name misspellings

1  Asked on August 2, 2020 by jonathan-rauscher

     

How to cut a string to the first “/” from right to left c# .net

1  Asked on August 1, 2020 by ignacio-gomez

   

Inserting random data from a list

2  Asked on August 1, 2020 by smiley

     

Invalid token SELECT

2  Asked on July 31, 2020 by virendra-varma

     

Django – No column found for custom field?

0  Asked on July 29, 2020 by jare42

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir