全局设置

请选择平台 Android 新 Android iOS Unity

GADMobileAds 类提供全局设置,用于控制某些 信息,这些信息由 Google Mobile Ads SDK 收集。

视频广告音量控制

如果您的应用有自己的音量控制,例如自定义的音乐或音效音量,那么向 Google Mobile Ads SDK 披露应用的音量后,视频广告会遵循相关的应用音量设置。这样可确保用户收到的视频广告具有预期的音频音量。

设备音量由音量按钮或操作系统级的音量滑块控制,它决定了设备的音频输出音量。但是,应用可相对于该设备音量独立调节自己的音量水平,从而定制音频体验。

对于开屏广告、横幅广告、插页式广告、激励广告和插页式激励广告,您可以通过设置 applicationVolume 属性向 Google Mobile Ads SDK 报告相对的应用音量。广告音量的有效值范围为 0.0(静音)至 1.0(当前设备音量)。以下示例展示了如何将相对的应用音量报告给该 SDK:

Swift

func viewDidLoad() {
  super.viewDidLoad()
  // Set app volume to be half of the current device volume.
  MobileAds.shared.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;
  ...
}

对于开屏广告、横幅广告、插页式广告、激励广告和插页式激励广告,您可以通过设置 applicationMuted 属性告知 Google Mobile Ads SDK 应用音量已静音:

Swift

MobileAds.shared.applicationMuted = true

Objective-C

GADMobileAds.sharedInstance.applicationMuted = YES;

默认情况下,applicationVolume 设置为 1(当前设备音量),applicationMuted 设置为 NO

原生广告

如需了解如何控制静音设置,请参阅 GADVideoOptions 。对于原生广告,不支持自定义音量控制。

音频会话

通过使用音频会话,您可以向系统告知自己的应用音频行为要求。如需了解音频会话的更多信息,请参阅 Apple's 音频会话编程 指南。 您可以通过 audioVideoManager 属性管理Google Mobile Ads SDK音频。

如果您的应用不使用音频,则无需使用这些 API。 Google Mobile Ads SDK 会在播放音频时自动管理音频会话类别。如果您在应用中播放音频,并且希望更严格地控制 何时以及如何播放音频,则可以使用这些 API。Google Mobile Ads SDK

如果您想自己负责管理音频 会话的类别,则可以在音视频管理器上将 audioSessionIsApplicationManaged 属性设置为YES

如果您想管理音频会话的类别,可以实现 GADAudioVideoManagerDelegate 并在音视频管理器上设置 delegate 属性,以便接收广告视频和音频 播放事件的通知。然后,您应该根据 Apple 的音频会话编程 指南,将音频会话类别更改为 相关类别。

以下是一个简化的代码示例,展示了如果您的应用播放音乐,使用上述 API 的推荐方法:

Swift

func setUp() {
  MobileAds.shared.audioVideoManager.delegate = self
  MobileAds.shared.audioVideoManager.audioSessionIsApplicationManaged = false
}

// MARK: - GADAudioVideoManagerDelegate
func audioVideoManagerWillPlayAudio(_ audioVideoManager: GADAudioVideoManager) {
  // The Google Mobile Ads SDK is notifying your app that it will play audio. You
  // could optionally pause music depending on your apps design.
  MyAppObject.shared.pauseAllMusic()
}

func audioVideoManagerDidStopPlayingAudio(_ audioVideoManager: GADAudioVideoManager) {
  // The Google Mobile Ads SDK is notifying your app that it has stopped playing
  // audio. Depending on your design, you could resume music here.
  MyAppObject.shared.resumeAllMusic()
}

Objective-C

- (void)setUp {
  GADMobileAds.sharedInstance.audioVideoManager.delegate = self;
  GADMobileAds.sharedInstance.audioVideoManager.audioSessionIsApplicationManaged = NO;
}

#pragma mark - GADAudioVideoManagerDelegate

- (void)audioVideoManagerWillPlayAudio:(GADAudioVideoManager *)audioVideoManager {
  // Google 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 {
    // Google 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];
}

崩溃报告

Google Mobile Ads SDK 会检查 iOS 应用中发生的异常,并会 记录 SDK 所引发的异常。这些异常随后会在未来的 SDK 版本中得到解决。

崩溃报告默认处于启用状态。如果您不想记录与 SDK 有关的异常,则可以通过调用 disableSDKCrashReporting 方法停用此功能。此方法的最佳调用时机是在应用启动时:

Swift

import GoogleMobileAds

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  func application(_ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    MobileAds.shared.disableSDKCrashReporting()
    return true
  }
}

Objective-C

@import GoogleMobileAds;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  [GADMobileAds disableSDKCrashReporting];
  return YES;
}

@end

如果您的应用有特殊要求,您可以设置可选的 NSUserDefaults gad_has_consent_for_cookiesGoogle Mobile Ads SDK 如果 gad_has_consent_for_cookies 偏好设置设为 0, 将会启用 受限广告 (LTD)

Swift

UserDefaults.standard.set(0, forKey: "gad_has_consent_for_cookies")

Objective-C

NSUserDefaults.standardUserDefaults().setObject(Int(0),
    forKey: "gad_has_consent_for_cookies");