Exoplayer: Calculating total playback time of a media.

Created on 10 Feb 2020  路  4Comments  路  Source: google/ExoPlayer

Scenario:
I want to calculate the total playback time for a media until the user switches to a different media by going to the next or previous media.

Problem:
I have covered all the possible scenarios except one, when the user switches to the next track/media after playing the media for a while, I'm not able to get the playbackEndTime.
Where playbackEndTime is the last known position from where the media was discontinued.

In this case the EventTime.currentPlaybackPositionMs is always 0 in every Callbacks of the AnalyticsListener.

Please correct me if my approach is not appropriate.

question

All 4 comments

What implementation of AnalyticsListener are you using? Please provide the code if it is a custom one.

class ExoPlayerAnalyticsListener : AnalyticsListener, EventListener {
  private var isSeekStarted: Boolean = false
  private var hasTracksChanged: Boolean = false
  private var playbackStartTime: Long = 0L
  private var playbackEndTime: Long = 0L
  private var isPlayingTime: Long = 0L
  private var totalPlayBack: Long = 0L
  private var currentMediaId: String? = null
  private var window = Window()

  override fun onPositionDiscontinuity(
    eventTime: EventTime,
    reason: Int
  ) {
    playbackStartTime = eventTime.currentPlaybackPositionMs
  }

  override fun onTracksChanged(
    eventTime: EventTime,
    trackGroups: TrackGroupArray,
    trackSelections: TrackSelectionArray
  ) {
    hasTracksChanged = true
    // I want the currentPlaybackPositionMs here 
  }

  override fun onSeekStarted(eventTime: EventTime) {
    isSeekStarted = true
    playbackEndTime = eventTime.currentPlaybackPositionMs
    calculatePlayback()
    isSeekStarted = false
  }

  override fun onPlayerStateChanged(
    eventTime: EventTime,
    playWhenReady: Boolean,
    playbackState: Int
  ) {
     // eventTime.currentPlaybackPositionMs is 0 when track is changed
  }

  override fun onIsPlayingChanged(
    eventTime: EventTime,
    isPlaying: Boolean
  ) {
    isPlayingTime = eventTime.currentPlaybackPositionMs
    when {
      isPlaying -> playbackStartTime = isPlayingTime
      else -> playbackEndTime = isPlayingTime
    }
    if (isSeekStarted.not())
      calculatePlayback()
  }

  private fun calculatePlayback() {
    if (hasTracksChanged) {
      hasTracksChanged = false
      resetTrackers()
      return
    }

    if (playbackStartTime == 0L) {
      //Media is being played from start
      totalPlayBack += playbackEndTime
    } else if (playbackEndTime > playbackStartTime) {
      totalPlayBack += abs(playbackEndTime - playbackStartTime)
    }
  }

  private fun resetTrackers() { //.. }
}

In case you are using ExoPlayer 2.11, you can use add PlaybackStatsListener to retrieve PlaybackStats for each playlist item that has a method called getTotalPlayTimeMs(). Sounds as if you are trying to calculate this value.

God bless PlaybackStatsListener
getTotalPlayTimeMs() This is what exactly I've been looking for.
Thank you @tonihei and @kim-vde for your support. Really appreciate your time.

Was this page helpful?
0 / 5 - 0 ratings