0

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

Audio is Started at this state. But I want to start the audio after the ad view is closed.

Y Junction
  • 181
  • 1
  • 15

1 Answers1

0

Sorry for the short answer, I will be assuming that you are using the latest AdMob SDK. You can simply do the following:

AdMob has a method called rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) for any View Controller conforming to GADRewardedAdDelegate.

You can simply add this as follows:

func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) {
    //User has earned the reward.
    //Play the sound.
    let reward = reward
    print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
    self.dismiss(animated: true, completion: nil)
    Player.shared.play()
}

This code will trigger after the user has earned the reward. If you want the sound to play regardless of whether or not the user earned the reward, then you can put it in the adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) method. You will not be able to check if the user actually did earn the reward if you use the adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) method.

Todd
  • 500
  • 4
  • 10
  • I install " pod 'Google-Mobile-Ads-SDK' " I don't sure it's the latest or not. I did not found GADRewardedAdDelegate. My code is working fine, but the problem is sound is playing before the ad close. I am updating my qus – Y Junction Apr 16 '21 at 18:15
  • Have you tried the code I suggested? If you added `Google-Mobile-Ads-SDK` in your Podfile, then it is the latest version. – Todd Apr 16 '21 at 18:16
  • Yes, the problem is still alive. The audio is playing before closing the ad view. – Y Junction Apr 16 '21 at 18:32