简体   繁体   中英

How to display AdMob banner in AVPlayer?

I'm working on an app to watch live streams. It is based on a table view and I'd like to offer it for free with ads (with the chance to buy the "Pro version" with in-app purchase). I managed to insert AdMob banners in the table view, but I'd like to insert small banners in the video streams too. I use AVPlayer to display streams and I know there's a method called contentOverlayView that allows to insert a custom view that stays visible when the video is playing, so I thought I have to use that method to display this banner, but I wasn't able to. The following code sets the player and it works perfectly, but the code I added to show the banner seems to be totally useless (i still see the video perfectly but no banner is displayed):

func allestisciPlayer(indexPath: IndexPath, destinazione: AVPlayerViewController) {

    // Ricava il canale corretto per mezzo della funzione apposita
    let canaleSelezionato = ritornaIlCanaleCorretto(indexPath: indexPath)

    // Prende il percorso giusto per lo streaming, poi crea il player nella destinazione
    let url = URL(string: canaleSelezionato.urlCanale)
    if let streamingURL = url {
        destinazione.player = AVPlayer(url: streamingURL)

        // Il do-try-catch seguente serve per far funzionare l'audio dell'app anche se l'iPhone è in modalità silenziosa; togli il blocco per fare in modo che l'app non produca audio se l'iPhone è in modalità silenziosa
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        } catch {
            print("Errore: non riesco a riprodurre l'audio se iPhone è in modalità silenziosa.")
        }

        let bannerView = GADBannerView()
        bannerView.adUnitID = "ca-app-pub-xxxx/xxxx"
        bannerView.rootViewController = self

        let request = GADRequest()
        request.testDevices = ["xxxx"]
        bannerView.load(request)

        bannerView.bounds = CGRect(x: 0, y: 0, width: 320, height: 50)

        destinazione.contentOverlayView?.addSubview(bannerView)

        // Infine avvia il player
        destinazione.player?.play()
    }
}

Where do I get wrong? I also tried to use this code in the prepareForSegue method and even by overriding the AVPlayerViewController's viewDidLoad method but still without success... anyone can help me please?

EDIT : I found out it wasn't that difficult :) I simply create a new banner (different from the existing one in the table view) and display it by creating another UIView, adding the banner as subview to this new view and finally adding the new view as subview to my AVPlayerViewController. I didn't manage to display banner using the contentOverlayView method, but I read somewhere that even if I would be able to display the ad using it, I wouldn't be able to click it cause user touches are disabled with the contentOverlayView content. Thank you anyway :)

This is not correct way to initialize the ad. You should initialize it in appdelegate, then you should be able to display the ad freely anywhere you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM