@@ -91,7 +91,7 @@ class MediaSession protected constructor(builder: Builder) {
9191 val mediaContentTimeSpent: Double
9292 get() { // total seconds spent playing content
9393 return currentPlaybackStartTimestamp?.let {
94- this .storedPlaybackTime + (System .currentTimeMillis().minus( it) / 1000 ).toDouble()
94+ this .storedPlaybackTime + (System .currentTimeMillis() - it) / 1000.0
9595 } ? : this .storedPlaybackTime
9696 }
9797 var mediaContentCompleteLimit: Int = 100
@@ -119,6 +119,7 @@ class MediaSession protected constructor(builder: Builder) {
119119 var storedPlaybackTime: Double = 0.0 // On Pause calculate playback time and clear currentPlaybackTime
120120 private set
121121 private var sessionSummarySent = false // Ensures we only send summary event once
122+ private var playbackState: PlaybackState = PlaybackState .PAUSED_BY_USER // Tracks whether playback was playing, paused, or paused by ad break
122123
123124 private var testing = false // Enabled for test cases
124125
@@ -191,6 +192,8 @@ class MediaSession protected constructor(builder: Builder) {
191192 if (currentPlaybackStartTimestamp == null ) {
192193 currentPlaybackStartTimestamp = System .currentTimeMillis()
193194 }
195+
196+ playbackState = PlaybackState .PLAYING
194197 val playEvent = MediaEvent (this , MediaEventName .PLAY , options = options)
195198 logEvent(playEvent)
196199 }
@@ -203,6 +206,8 @@ class MediaSession protected constructor(builder: Builder) {
203206 storedPlaybackTime + = ((System .currentTimeMillis() - it) / 1000 )
204207 currentPlaybackStartTimestamp = null ;
205208 }
209+
210+ playbackState = PlaybackState .PAUSED_BY_USER
206211 val pauseEvent = MediaEvent (this , MediaEventName .PAUSE , options = options)
207212 logEvent(pauseEvent)
208213 }
@@ -575,28 +580,30 @@ class MediaSession protected constructor(builder: Builder) {
575580 }
576581
577582 private fun pauseContentTimeIfAdBreakExclusionEnabled () {
578- if (! excludeAdBreaksFromContentTime) {
579- currentPlaybackStartTimestamp?.let {
580- storedPlaybackTime + = ((System .currentTimeMillis() - it) / 1000 )
581- currentPlaybackStartTimestamp = null
582- }
583+ if (! excludeAdBreaksFromContentTime || playbackState != PlaybackState .PLAYING ) return
584+
585+ currentPlaybackStartTimestamp?.let {
586+ storedPlaybackTime + = (System .currentTimeMillis() - it) / 1000.0
587+ currentPlaybackStartTimestamp = null
588+ playbackState = PlaybackState .PAUSED_BY_AD_BREAK
583589 }
584590 }
585591
586592 private fun resumeContentTimeIfAdBreakExclusionEnabled () {
587- if (! excludeAdBreaksFromContentTime) {
588- if (currentPlaybackStartTimestamp != null ) {
589- currentPlaybackStartTimestamp = System .currentTimeMillis()
590- }
591- }
593+ if (! excludeAdBreaksFromContentTime || playbackState != PlaybackState .PAUSED_BY_AD_BREAK ) return
594+
595+ currentPlaybackStartTimestamp = System .currentTimeMillis()
596+ playbackState = PlaybackState .PLAYING
592597 }
593598
594599 private fun logAdSummary (content : MediaAd ? ) {
595600 content?.let { ad ->
596601 ad.adStartTimestamp?.let { startTime ->
597- val endTime = System .currentTimeMillis()
598- ad.adEndTimestamp = endTime
599- mediaTotalAdTimeSpent + = ((endTime - startTime) / 1000 ).toDouble()
602+ val endTime = ad.adEndTimestamp ? : System .currentTimeMillis()
603+ if (ad.adEndTimestamp == null ) {
604+ ad.adEndTimestamp = endTime
605+ mediaTotalAdTimeSpent + = ((endTime - startTime) / 1000 ).toDouble()
606+ }
600607 }
601608
602609 val customAttributes: MutableMap <String , String > = mutableMapOf ()
@@ -777,10 +784,10 @@ class MediaSession protected constructor(builder: Builder) {
777784 }
778785
779786 /* *
780- * When enabled, automatically pauses content time tracking during ad breaks and resumes after .
787+ * When enabled, ad break time is excluded from content time tracking .
781788 * When disabled (default), ad break time is included in content time spent.
782789 */
783- fun pauseContentDuringAdBreaks (shouldPause : Boolean ): Builder {
790+ fun excludeAdBreaksFromContentTime (shouldPause : Boolean ): Builder {
784791 this .excludeAdBreaksFromContentTime = shouldPause
785792 return this
786793 }
@@ -795,6 +802,12 @@ class MediaSession protected constructor(builder: Builder) {
795802
796803}
797804
805+ private enum class PlaybackState {
806+ PLAYING ,
807+ PAUSED_BY_USER ,
808+ PAUSED_BY_AD_BREAK
809+ }
810+
798811private fun String?.require (variableName : String ): String {
799812 if (this == null ) {
800813 Logger .error(" \" $variableName \" should not be null" )
0 commit comments