通用設定

選取平台: Android iOS Unity

MobileAds 類別提供 Google Mobile Ads SDK 的全域設定。

影片廣告音量控制

如果您的應用程式有專屬的音量控制項 (例如自訂音樂或音效音量),向 Google Mobile Ads SDK 揭露應用程式音量,可讓影片廣告採用應用程式音量設定。確保使用者接收到影片廣告時,廣告的音量符合預期。

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

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

  val backgroundScope = CoroutineScope(Dispatchers.IO)
  backgroundScope.launch {
    // Initialize 
  
    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)
  }
}

Java

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  new Thread(
          () -> {
            // Initialize 
  
    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();
}

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

Kotlin

MobileAds.setAppMuted(true)

Java

MobileAds.setAppMuted(true);

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

原生廣告

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

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

Kotlin

val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context)
// Set the value to 0 to enable limited ads.
sharedPrefs.edit().putInt("gad_has_consent_for_cookies", 0).apply()

Java

Context activity = getActivity();
SharedPreferences sharedPreferences =
  PreferenceManager.getDefaultSharedPreferences(activity);
// Set the value to 0 to enable limited ads.
sharedPreferences.edit().putInt("gad_has_consent_for_cookies", 0).apply();