I want to play Audio when the rewarded Ad is completed and dismissed from the ad screen.
Here is my code:-
var rewardedAd: GADRewardedAd?
override func viewDidLoad() {
super.viewDidLoad()
rewardedAd?.fullScreenContentDelegate = self
createAndLoadRewardAd()
}
Receiving the reward:
if let ad = rewardedAd {
ad.present(fromRootViewController: self) {
let reward = ad.adReward
print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
Player.shared.play()
}
} else {
let alert = UIAlertController(
title: "Rewarded ad isn't available yet.",
message: "Sound Cannot Be play without watching Rewarded ad. Try after few moments or check Internet Connection",
preferredStyle: .alert)
let alertAction = UIAlertAction(
title: "OK",
style: .cancel,
handler: { [weak self] action in
})
alert.addAction(alertAction)
self.present(alert, animated: true, completion: nil)
}
The problem is sound is started before closing the Ad view. I want to play the sound after closing the ad view. I also tried the following protocol but it not working, I mean response nothing
extension SoundViewController: GADFullScreenContentDelegate {
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("ad showing")
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
createAndLoadRewardAd()
print("ad closed")
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print(error.localizedDescription)
}
func createAndLoadRewardAd() {
GADRewardedAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest()
) { (ad, error) in
if let error = error {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
return
}
print("Loading Succeeded")
self.rewardedAd = ad
}
}
}
Update 1
Audio is Started at this state. But I want to start the audio after the ad view is closed.