1 year ago
#22676
WISHY
Events are not triggered and UI is not displaying for IMA SDK Android
I am implementing pre roll ads with the help of IMA SDK and using Media Player on Surface / Texture View.
Have been closely following the implementation google samples IMA ANDROID
The initialisation code is as follows
fun requestAds(){
videoAdPlayer = object : VideoAdPlayer {
override fun getAdProgress(): VideoProgressUpdate {
return if (myVideoPlayer == null || (myVideoPlayer?.getDuration() ?: 0 <= 0)) {
VideoProgressUpdate.VIDEO_TIME_NOT_READY
} else if (myVideoPlayer != null) {
VideoProgressUpdate(myVideoPlayer!!.getCurrentPosition().toLong(),
myVideoPlayer!!.getDuration().toLong())
} else {
VideoProgressUpdate.VIDEO_TIME_NOT_READY
}
}
override fun getVolume(): Int {
return myVideoPlayer?.getVolume() ?: 0
}
override fun addCallback(p0: VideoAdPlayer.VideoAdPlayerCallback?) {
if (p0 != null) {
videoPlayerCallbacks.add(p0)
}
}
override fun loadAd(adMediaInfo: AdMediaInfo?, adpodInfo: AdPodInfo?) {
if (adMediaInfo != null && !TextUtils.isEmpty(adMediaInfo.url)) {
mCurrentMediaInfo = adMediaInfo
var duration: Int? = null
duration = (mAdsManager?.currentAd?.duration ?: 0).toInt()
adViewLayout.getAdViewController()
?.addMediationUrl(adMediaInfo.url, this@videoController,
duration, -1, mAdsManager?.currentAd?.vastMediaHeight,
mAdsManager?.currentAd?.vastMediaWidth)
}
}
override fun pauseAd(p0: AdMediaInfo?) {
myVideoPlayer?.pauseAd(mIsCalledByDev)
for (callback in videoPlayerCallbacks) {
callback.onPause(p0)
}
}
override fun playAd(p0: AdMediaInfo?) {
val playbackState = myVideoPlayer?.getPlayerState()
if (playbackState == MediaPlayer.STATE_PAUSED) {
myVideoPlayer?.resumeAd(mIsCalledByDev)
for (callback in videoPlayerCallbacks) {
callback.onResume(p0)
}
} else if (playbackState == MediaPlayer.STATE_PREPARED) {
mAdsManager.start()
myVideoPlayer?.startVideo()
for (callback in videoPlayerCallbacks) {
callback.onPlay(p0)
}
}
}
override fun release() {
myVideoPlayer?.release()
}
override fun removeCallback(p0: VideoAdPlayer.VideoAdPlayerCallback?) {
if (p0 != null) {
videoPlayerCallbacks.remove(p0)
}
}
override fun stopAd(p0: AdMediaInfo?) {
myVideoPlayer?.stopAds()
}
}
mAdUiContainer = FrameLayout(mContext)
val mAdDisplayContainer =
ImaSdkFactory.createAdDisplayContainer(mAdUiContainer, videoAdPlayer)
mAdDisplayContainer.player = videoAdPlayer
mSdkFactory = ImaSdkFactory.getInstance()
val request = mSdkFactory!!.createAdsRequest()
request!!.adTagUrl = mAdTagUrl
val imaSdkSettings = mSdkFactory!!.createImaSdkSettings()!!
mAdsLoader =
mSdkFactory?.createAdsLoader(mContext, imaSdkSettings, mAdDisplayContainer!!)
mAdsLoader?.addAdErrorListener(this)
mAdsLoader?.addAdsLoadedListener(this)
// Request the ad. After the ad is loaded, onAdsManagerLoaded() will be called.
mAdsLoader?.requestAds(request)
}
Media player is connected to AdsManager
as it is in sample. Have enabled preloading .setEnablePreloading(true);
I am receiing the LOADED
event but after this not receiving any event.
Also I don't see the AD UI which should be having the webview and skip button.
After ads manger is loaded enabling preload
override fun onAdsManagerLoaded(adsManagerLoadedEvent: AdsManagerLoadedEvent?) {
if (adsManagerLoadedEvent?.adsManager != null) {
mAdsManager = adsManagerLoadedEvent.adsManager
if (mAdsManager != null && mSdkFactory != null) {
mAdsManager!!.addAdErrorListener(this)
mAdsManager!!.addAdEventListener(this)
val adsRenderingSettings: AdsRenderingSettings =
mSdkFactory!!.createAdsRenderingSettings()
adsRenderingSettings.enablePreloading = true
adsRenderingSettings.setLoadVideoTimeout(20000)
mAdsManager?.init(adsRenderingSettings)
}
}
}
Also the player is connected to adcallback
fun setVideoPlayer() {
videoPlayer!!.setPlayerCallback(object : VideoPlayer.Callback {
override fun onPrepared() {
}
override fun onStarted() {
if (mCurrentMediaInfo != null) for (callback in videoPlayerCallbacks) {
callback.onPlay(mCurrentMediaInfo)
}
}
override fun onComplete() {
if (mCurrentMediaInfo != null) for (callback in videoPlayerCallbacks) {
callback.onEnded(mCurrentMediaInfo)
}
}
override fun onPaused() {
if (mCurrentMediaInfo != null) for (callback in videoPlayerCallbacks) {
callback.onPause(mCurrentMediaInfo)
}
}
override fun onError() {
if (mCurrentMediaInfo != null) for (callback in videoPlayerCallbacks) {
callback.onError(mCurrentMediaInfo)
}
}
override fun onProgress(totalDuration: Long, progress: Long) {
if (mCurrentMediaInfo != null && videoAdPlayer != null) for (callback in videoPlayerCallbacks) {
callback.onAdProgress(mCurrentMediaInfo, videoAdPlayer!!.adProgress)
}
}
override fun onResumed() {
if (mCurrentMediaInfo != null) for (callback in videoPlayerCallbacks) {
callback.onResume(mCurrentMediaInfo)
}
}
})
}
Unable to figure out the issue here..
android
admob
android-mediaplayer
mobile-ad-mediation
imasdk
0 Answers
Your Answer