如何在tvOS中播放完视频后播放广告



所以当你点击缩略图时,我可以播放视频,但如果我把广告附加到它上,广告就会在视频上播放。之后我该如何等待播放广告?

func playVideo(){
    player = AVPlayer(URL: NSURL(string: String(vid))!)
    player?.play()
    if(ALInterstitialAd.isReadyForDisplay()){
        ALInterstitialAd.show()
    }
}

您是否尝试注册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)
}

相关内容

  • 没有找到相关文章

最新更新