Как выполнить предзагрузку native ads, android?

Я добавил в свое приложение, Native ads, но на разных устройствах реклама, особенно видео, приходят с задержкой, на эмуляторе видео не показывается вообще. Знаю что можно как-то пред загружать рекламу, но не знаю как это сделать, подскажите пожалуйста.

Вот мой код:

class Fragment (private var adsId: String) : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment, container, false)
 
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val adLoader = AdLoader.Builder(activity, adsId).forUnifiedNativeAd { unifiedNativeAd ->

            val unifiedNativeAdView = layoutInflater.inflate(R.layout.native_ad_layout, null) as UnifiedNativeAdView
            mapUnifiedNativeAdToLayout(unifiedNativeAd, unifiedNativeAdView)
            val nativeAdLayout = view!!.findViewById<FrameLayout>(R.id.id_native_ad)
            nativeAdLayout.removeAllViews()
            nativeAdLayout.addView(unifiedNativeAdView)
        }.withAdListener(object : AdListener() {
            override fun onAdLoaded() {
                super.onAdLoaded()

            }

            override fun onAdFailedToLoad(errorCode: Int) {

            }

            override fun onAdClosed() {

            }

            override fun onAdLeftApplication() {

            }

            override fun onAdOpened() {

            }

            override fun onAdClicked() {

            }
        }).build()
        adLoader.loadAd(AdRequest.Builder().build())

        trans_btn.setOnClickListener {
            val tabs: TabLayout = activity!!.findViewById(R.id.tab_layout)
            tabs.getTabAt(index)!!.select()

        }
    }

    fun mapUnifiedNativeAdToLayout(adFromGoogle: UnifiedNativeAd, myAdView: UnifiedNativeAdView) {
        val mediaView: MediaView = myAdView.findViewById(R.id.ad_media)
        myAdView.mediaView = mediaView
        myAdView.bodyView = myAdView.findViewById(R.id.ad_body)

        if (adFromGoogle.body == null) {
            myAdView.bodyView.visibility = View.GONE
        } else {
            (myAdView.bodyView as TextView).text = adFromGoogle.body
        }

        myAdView.setNativeAd(adFromGoogle)

        val vc = adFromGoogle.videoController

        if (vc.hasVideoContent()) {

            vc.videoLifecycleCallbacks = object : VideoController.VideoLifecycleCallbacks() {
                override fun onVideoEnd() {
                    super.onVideoEnd()
                }
            }
        } 
    }

}

native_ad_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.formats.UnifiedNativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="2"
                android:orientation="vertical">

                <com.google.android.gms.ads.formats.MediaView
                    android:id="@+id/ad_media"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:padding="@dimen/_8sdp"
                    android:layout_gravity="center"
                    android:layout_weight="2"
                    android:background="#fff" />

                <TextView
                    android:id="@+id/ad_body"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/sf_pro_display_regular"
                    android:gravity="center"
                    android:text="body of ad"
                    android:textSize="14sp" />

            </LinearLayout>

        </RelativeLayout>
    
    </LinearLayout>

</com.google.android.gms.ads.formats.UnifiedNativeAdView>

В фрагменте, просто FrameView для рекламы.


Ответы (0 шт):