The GADMobileAds
class provides global settings for controlling
certain information collected by the Mobile Ads SDK.
Video ad volume control
If your app has its own volume controls (such as custom music or sound effect volumes), disclosing app volume to the Google Mobile Ads SDK allows video ads to respect app volume settings. This ensures users receive video ads with the expected audio volume.
The device volume, controlled through volume buttons or OS-level
volume slider, determines the volume for device audio output.
However, apps can independently adjust volume levels relative to
the device volume to tailor the audio experience. You can report
the relative app volume to the Google Mobile Ads SDK by setting
the applicationVolume
property. Valid ad volume values range
from 0.0 (silent) to 1.0 (current device volume). Here's an example
of how to report the relative app volume to the SDK:
Swift
func viewDidLoad() { super.viewDidLoad() // Set app volume to be half of the current device volume. GADMobileAds.sharedInstance().applicationVolume = 0.5 ... }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; // Set app volume to be half of the current device volume. [GADMobileAds sharedInstance].applicationVolume = 0.5; ... }
To inform the Google Mobile Ads SDK that the app volume has been
muted, set the applicationMuted
property, as shown below:
Swift
GADMobileAds.sharedInstance().applicationMuted = true
Objective-C
[GADMobileAds sharedInstance].applicationMuted = YES;
Unmuting the app volume reverts it to its previously set level. By default, the app volume for the Google Mobile Ads SDK is set to 1 (the current device volume).
Changing audio session
Audio sessions allow you to express to the system your intentions
for your app's audio behavior. Additional information on audio
sessions can be found in Apple's Audio Session Programming
Guide.
The available options for managing the Google Mobile Ads SDK audio is via the
audioVideoManager
property.
If you don't use audio in your app, you don't need to use these APIs. The Google Mobile Ads SDK will automatically manage the audio session category when it plays audio. If you do play audio in your app and you want tighter control of how and when Google Mobile Ads SDK plays audio, these APIs can help.
On the audio video manager, you can set the audioSessionIsApplicationManaged
property to YES
if you want to take responsibility for managing the audio
session category yourself.
If you will manage the audio session category, you should implement
GADAudioVideoManagerDelegate
and set the delegate
property
on the audio video manager to be notified of ads video and audio playback
events. You should then change the audio session category to the relevant
category as per Apple's Audio Session Programming Guide linked above.
Here is a simplified code sample which shows the recommended approach if your app plays music, using above APIs.
Swift
func setUp() { GADMobileAds.sharedInstance().audioVideoManager.delegate = self GADMobileAds.sharedInstance().audioVideoManager.audioSessionIsApplicationManaged = false } func myAppWillStartPlayingMusic() { // Mutes all Google video ads. GADMobileAds.sharedInstance().audioVideoManager.audioSessionIsApplicationManaged = true GADMobileAds.sharedInstance().applicationMuted = true } func myAppDidStopPlayingMusic() { // Un-mutes all of our video ads. GADMobileAds.sharedInstance().audioVideoManager.audioSessionIsApplicationManaged = false GADMobileAds.sharedInstance().applicationMuted = false } // MARK: - GADAudioVideoManagerDelegate func audioVideoManagerWillPlayAudio(_ audioVideoManager: GADAudioVideoManager) { // The mobile ads SDK is notifying your app that it will play audio. You // could optionally pause music depending on your apps design. MyAppObject.sharedInstance().pauseAllMusic() } func audioVideoManagerDidStopPlayingAudio(_ audioVideoManager: GADAudioVideoManager) { // The mobile ads SDK is notifying your app that it has stopped playing // audio. Depending on your design, you could resume music here. MyAppObject.sharedInstance().resumeAllMusic() }
Objective-C
- (void)setUp { [GADMobileAds sharedInstance].audioVideoManager.delegate = self; [GADMobileAds sharedInstance].audioVideoManager.audioSessionIsApplicationManaged = NO; } - (void)myAppWillStartPlayingMusic { // Mutes all Google video ads. [GADMobileAds sharedInstance].audioVideoManager.audioSessionIsApplicationManaged = YES; [GADMobileAds sharedInstance].applicationMuted = YES; } - (void)myAppDidStopPlayingMusic { // Un-mutes all of our video ads. [GADMobileAds sharedInstance].audioVideoManager.audioSessionIsApplicationManaged = NO; [GADMobileAds sharedInstance].applicationMuted = NO; } #pragma mark - GADAudioVideoManagerDelegate - (void)audioVideoManagerWillPlayAudio:(GADAudioVideoManager *)audioVideoManager { // The mobile ads SDK is notifying your app that it will play audio. You // could optionally pause music depending on your apps design. [[MyAppObject sharedInstance] pauseAllMusic]; } - (void)audioVideoManagerDidStopPlayingAudio:(GADAudioVideoManager *)audioVideoManager { // The mobile ads SDK is notifying your app that it has stopped playing // audio. Depending on your design, you could resume music here. [[MyAppObject sharedInstance] resumeAllMusic]; }
Crash reporting
The Mobile Ads SDK inspects exceptions that occur in an iOS app and records them if they are caused by the SDK. These exceptions are collected so we can prevent them in future SDK versions.
Crash reporting is enabled by default.
If you don't want SDK-related exceptions to be recorded,
you can disable this feature by calling
the disableSDKCrashReporting
method.
The best place to call this method is when the app launches:
Swift
AppDelegate.swiftfunc application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { GADMobileAds.disableSDKCrashReporting() return true }
Objective-C
AppDelegate.m- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [GADMobileAds disableSDKCrashReporting]; return YES; }