Skip to content

Commit

Permalink
VideoPlayerAdCallback added to IMA integration (#82)
Browse files Browse the repository at this point in the history
* VideoPlayerAdCallback added to ima.

* Some error desc

* Use aderror + cleanup

* Doc clarification

* Add kotlin APIs

* cleanup

* Actually set the ad player callback

---------

Co-authored-by: Emily Dixon <[email protected]>
  • Loading branch information
tomkordic and daytime-em authored Sep 30, 2024
1 parent 3d21dc5 commit 87e828b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ima.ImaAdsLoader
import com.google.ads.interactivemedia.v3.api.AdErrorEvent.AdErrorListener
import com.google.ads.interactivemedia.v3.api.AdEvent.AdEventListener
import com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer.VideoAdPlayerCallback
import com.mux.stats.sdk.muxstats.MuxStatsSdkMedia3

/**
* Monitors the [ImaAdsLoader] created by the given [ImaAdsLoader.Builder].
*
* Mux must take ownership of the [AdErrorListener] and [AdEventListener] for this ads loader, but
* you can provide your own listeners and logic using the provided optional params
* Mux must take ownership of the [AdErrorListener], [AdEventListener], and [VideoAdPlayerCallback]
* for this ads loader, but you can provide your own listeners and logic using the provided optional
* params.
*
* @param muxStats The [MuxStatsSdkMedia3] instance monitoring your player
* @param customerAdEventListener Optional. An [AdEventListener] with your apps custom ad-event handling
Expand All @@ -23,15 +25,18 @@ fun ImaAdsLoader.Builder.monitorWith(
muxStats: MuxStatsSdkMedia3<*>,
customerAdEventListener: AdEventListener = AdEventListener { },
customerAdErrorListener: AdErrorListener = AdErrorListener { },
customerAdPlayerAdCallback: VideoAdPlayerCallback? = null
): ImaAdsLoader.Builder {
val adsListener = MuxImaAdsListener.newListener(
{ muxStats },
muxStats,
customerAdEventListener,
customerAdErrorListener
customerAdErrorListener,
customerAdPlayerAdCallback,
)

setAdEventListener(adsListener)
setAdErrorListener(adsListener)
customerAdPlayerAdCallback?.let { setVideoAdPlayerCallback(adsListener) }

return this
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.google.ads.interactivemedia.v3.api.AdErrorEvent
import com.google.ads.interactivemedia.v3.api.AdErrorEvent.AdErrorListener
import com.google.ads.interactivemedia.v3.api.AdEvent
import com.google.ads.interactivemedia.v3.api.AdEvent.AdEventListener
import com.google.ads.interactivemedia.v3.api.player.AdMediaInfo
import com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer.VideoAdPlayerCallback
import com.google.ads.interactivemedia.v3.api.player.VideoProgressUpdate
import com.mux.android.util.oneOf
import com.mux.stats.sdk.core.events.playback.*
import com.mux.stats.sdk.core.model.AdData
Expand All @@ -24,7 +27,8 @@ class MuxImaAdsListener private constructor(
private val provider: Provider,
private val customerAdEventListener: AdEventListener = AdEventListener { },
private val customerAdErrorListener: AdErrorListener = AdErrorListener { },
) : AdErrorListener, AdEventListener {
private val customerVideoAdPlayerCallback: VideoAdPlayerCallback? = null,
) : AdErrorListener, AdEventListener, VideoAdPlayerCallback {

/** The ExoPlayer that is playing the ads */
private val exoPlayer: Player? get() = provider.boundPlayer
Expand Down Expand Up @@ -222,6 +226,50 @@ class MuxImaAdsListener private constructor(
adCollector?.dispatch(event)
}


/** VideoAdPlayerCallback */
override fun onAdProgress(mediaInfo: AdMediaInfo, progress: VideoProgressUpdate) {
customerVideoAdPlayerCallback?.onAdProgress(mediaInfo, progress)
}

override fun onBuffering(mediaInfo: AdMediaInfo) {
customerVideoAdPlayerCallback?.onBuffering(mediaInfo)
}

override fun onContentComplete() {
customerVideoAdPlayerCallback?.onContentComplete()
}

override fun onEnded(mediaInfo: AdMediaInfo) {
customerVideoAdPlayerCallback?.onEnded(mediaInfo)
}

override fun onError(mediaInfo: AdMediaInfo) {
adCollector?.dispatch(MuxAdErrorEvent(null))
customerVideoAdPlayerCallback?.onError(mediaInfo)
}

override fun onLoaded(mediaInfo: AdMediaInfo) {
adCollector?.dispatch(AdResponseEvent(null))
customerVideoAdPlayerCallback?.onLoaded(mediaInfo)
}

override fun onPause(mediaInfo: AdMediaInfo) {
customerVideoAdPlayerCallback?.onPause(mediaInfo)
}

override fun onPlay(mediaInfo: AdMediaInfo) {
customerVideoAdPlayerCallback?.onPlay(mediaInfo)
}

override fun onResume(mediaInfo: AdMediaInfo) {
customerVideoAdPlayerCallback?.onResume(mediaInfo)
}

override fun onVolumeChanged(mediaInfo: AdMediaInfo, p1: Int) {
customerVideoAdPlayerCallback?.onVolumeChanged(mediaInfo, p1)
}

companion object {
private const val TAG = "MuxImaAdsListener"

Expand All @@ -233,28 +281,30 @@ class MuxImaAdsListener private constructor(
muxSdk: MuxStatsSdkMedia3<*>,
customerAdEventListener: AdEventListener = AdEventListener { },
customerAdErrorListener: AdErrorListener = AdErrorListener { },
customerVideoAdPlayerCallback: VideoAdPlayerCallback? = null,
): MuxImaAdsListener {
return MuxImaAdsListener(
Provider { muxSdk },
customerAdEventListener,
customerAdErrorListener,
customerVideoAdPlayerCallback
)
}
/**
* Creates a new [MuxImaAdsListener] based on the given [MuxStatsSdkMedia3]
*/
@JvmStatic
fun newListener(
muxSdkProvider:() -> MuxStatsSdkMedia3<*>?,
customerAdEventListener: AdEventListener = AdEventListener { },
customerAdErrorListener: AdErrorListener = AdErrorListener { },
): MuxImaAdsListener {
return MuxImaAdsListener(
Provider { muxSdkProvider() },
customerAdEventListener,
customerAdErrorListener
)
}
/**
* Creates a new [MuxImaAdsListener] based on the given [MuxStatsSdkMedia3]
*/
@JvmStatic
fun newListener(
muxSdkProvider:() -> MuxStatsSdkMedia3<*>?,
customerAdEventListener: AdEventListener = AdEventListener { },
customerAdErrorListener: AdErrorListener = AdErrorListener { },
): MuxImaAdsListener {
return MuxImaAdsListener(
Provider { muxSdkProvider() },
customerAdEventListener,
customerAdErrorListener
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package com.mux.stats.sdk.media3_ima
import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ima.ImaAdsLoader
import androidx.media3.exoplayer.ima.ImaServerSideAdInsertionMediaSource
import androidx.media3.exoplayer.ima.ImaServerSideAdInsertionMediaSource.AdsLoader
import com.google.ads.interactivemedia.v3.api.AdErrorEvent.AdErrorListener
import com.google.ads.interactivemedia.v3.api.AdEvent.AdEventListener
import com.mux.stats.sdk.muxstats.MuxStatsSdkMedia3

/**
* Monitors the [ImaAdsLoader] created by the given [ImaAdsLoader.Builder].
* Monitors the [ImaServerSideAdInsertionMediaSource.AdsLoader] created by the a
* [ImaServerSideAdInsertionMediaSource.AdsLoader.Builder].
*
* This method is just for DAI server-side ad-insertion. If you're doing Client-Side Ad Insertion
* (CSAI), use [ImaAdsLoader.Builder.monitorWith]
*
* Mux must take ownership of the [AdErrorListener] and [AdEventListener] for this ads loader, but
* you can provide your own listeners and logic using the provided optional params
Expand Down

0 comments on commit 87e828b

Please sign in to comment.