1

因此,当您单击缩略图时,我可以播放视频,但如果我将广告附加到它,广告将在视频上播放。之后如何等待播放广告?

func playVideo(){

    player = AVPlayer(URL: NSURL(string: String(vid))!)
    player?.play()

    if(ALInterstitialAd.isReadyForDisplay()){
        ALInterstitialAd.show()
    }
}
4

1 回答 1

1

你试过注册AVPlayerItemDidPlayToEndTimeNotification吗?

只需添加

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourViewController.itemDidFinishPlaying(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)

当您播放您的视频并itemDidFinishPlaying:在您需要展示您的广告时实施该方法。像这样的东西:

func playVideo(){
    player = AVPlayer(URL: NSURL(string: String(vid))!)
    player?.play()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourViewController.itemDidFinishPlaying(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}

func itemDidFinishPlaying(notification : NSNotification){
    if(ALInterstitialAd.isReadyForDisplay()){
        ALInterstitialAd.show()
    }
    NSNotificationCenter.defaultCenter().removeObserver(self, name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}
于 2016-04-02T09:09:09.777 回答