Audiofy SDK - Banner Ad Unit

Add Banner

To load and show banner ads, you should initialize, configure, and add the MediafyAdView object to the app's layout and call the loadAd() method.

private fun createAd() {
    // 1. Create MediafyView
    val adView = MediafyAdView(this)

    // 2. Configure ad unit
    adView.setAdUnitId(AD_UNIT_ID)
    adView.setAdSizes(MediafyAdSize(WIDTH, HEIGHT))
    //For Multi-size:
    //adView.setAdSizes(
    //MediafyAdSize(WIDTH1, HEIGHT1),
    //MediafyAdSize(WIDTH2, HEIGHT2),
    //MediafyAdSize(WIDTH3, HEIGHT3)
    //)
    adView.setBannerListener(createListener())
    adView.setAutoRefreshDelay(30)

    // 3. Load ad
    adView.loadAd()

    // 4. Add MediafyAdView to the app UI
    containerForAd.addView(adView)
}

Configure Banner

Configuration methods for MediafyAdView:

Configuration Property Reqiured Description
setAdSizes() required One or more ad sizes supported for particular ad placement.
setAdUnitId() required An identifier provided by Mediafy SDK platform or Mediafy SDK account manager for ad placement.
setBannerListener() optional sets subscriber to the ad’s lifecycle events.
setAutoRefreshDelay() optional Refresh period of banner in seconds. The default value is 60 seconds.

Ensure you provide sizes for your ad unit in code or via settings on the publisher's UI. It's a critical property.

If you need to integrate video ads, you can also use the MediafyAdView object in the same way as for banner ads. The single required change is you should explicitly set the VIDEO ad format via ad unit property:

adView.setAdUnitFormat(MediafyAdUnitFormat.VIDEO)

Once done, the Audiofy SDK will make ad requests for video ad, and render the respective creatives.

Also, you can set the video placement type and video listener.

adView.videoPlacementType = MediafyVideoPlacementType.IN_BANNER
adView.setBannerVideoListener(createVideoListener())

Manage the Ad States

You can optionally subscribe to the ad’s lifecycle events by implementing the MediafyAdViewListener interface:

private fun createListener(): MediafyAdViewListener {
    return object : MediafyAdViewListener {
        override fun onAdLoaded(MediafyAdView: MediafyAdView) {
            // Called when ad loaded
            Log.d(TAG, "Ad loaded successfully")
        }

        override fun onAdFailed(MediafyAdView: MediafyAdView, e: MediafyAdException) {
            // Called when ad failed to load or parse
            Log.e(TAG, "Ad failed to load: " + e.message, e)
        }

        override fun onAdDisplayed(MediafyAdView: MediafyAdView) {
            // Called when ad displayed
        }

        override fun onAdClicked(MediafyAdView: MediafyAdView) {
            // Called when ad clicked
        }

        override fun onAdClosed(MediafyAdView: MediafyeAdView) {
            // Called when ad closed
        }
    }
}

Additionally, you can subscribe to the video events.

private fun createVideoListener(): MediafyAdViewVideoListener {
    return object : MediafyAdViewVideoListener {
        override fun onVideoCompleted(mediafyAdView: MediafyAdView) {
            // Called when video completed
            Log.d(TAG, "Video completed")
        }

        override fun onVideoPaused(mediafyAdView: MediafyAdView) {
            // Called when video paused
        }

        override fun onVideoResumed(mediafyAdView: MediafyAdView) {
            // Called when video resumed
        }

        override fun onVideoUnMuted(mediafyAdView: MediafyAdView) {
            // Called when video unmuted
        }

        override fun onVideoMuted(mediafyAdView: MediafyAdView) {
            // Called when video muted
        }
    }
}
+

Contact Us