通用設定

MobileAds 類別提供 Google Mobile Ads SDK 的通用設定。

影片廣告音量控制項

如果應用程式有專屬的音量控制選項 (例如自訂音樂或音效音量),只要向 Google Mobile Ads SDK 揭露應用程式音量,影片廣告即可依應用程式音量設定放送。這樣可確保使用者接收到預期音訊音量的影片廣告。

裝置音量 (透過音量按鈕或作業系統層級音量滑桿控制) 會決定裝置音訊輸出的音量。不過,應用程式可以根據裝置音量獨立調整音量,打造專屬的音訊體驗。如果是應用程式開啟頁面、橫幅廣告、插頁式廣告、獎勵廣告和獎勵插頁式廣告格式,您可以透過靜態 setAppVolume() 方法向 SDK 回報相對的應用程式數量。有效廣告音量值介於 0.0 (靜音) 到 1.0 (目前裝置音量) 之間。以下範例說明如何向 SDK 回報相對的應用程式數量:

Java

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_my);

  new Thread(
          () -> {
            // Initialize the Google Mobile Ads SDK on a background thread.
            MobileAds.initialize(this, initializationStatus -> {});
            // Set app volume to be half of current device volume.
            MobileAds.setAppVolume(0.5f);
          })
      .start();
}

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_main)

  val backgroundScope = CoroutineScope(Dispatchers.IO)
  backgroundScope.launch {
    // Initialize the Google Mobile Ads SDK on a background thread.
    MobileAds.initialize(this@MainActivity) {}
    // Set app volume to be half of current device volume.
    MobileAds.setAppVolume(0.5f)
  }
}

如要告知 SDK 應用程式音量已設為靜音,請使用 setAppMuted() 方法:

Java

MobileAds.setAppMuted(true);

Kotlin

MobileAds.setAppMuted(true)

根據預設,應用程式音量設為 1 (目前裝置音量),且應用程式不會設為靜音。

原生廣告

想瞭解如何控制靜音設定,請參閱 VideoOptions。原生廣告目前不支援自訂音量控制項。

如果您的應用程式有特殊需求,您可以設定選用的 SharedPreferences gad_has_consent_for_cookies。當 gad_has_consent_for_cookies 偏好設定設為零時,SDK 會啟用 受限制的廣告 (LTD)

Java

Context activity = getActivity();
SharedPreferences sharedPreferences =
  PreferenceManager.getDefaultSharedPreferences(activity);
sharedPreferences.edit().putInt("gad_has_consent_for_cookies", 0).apply();

Kotlin

val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context)
sharedPrefs.edit().putInt("gad_has_consent_for_cookies", 0).apply()