Skip to content

Commit e3a7b15

Browse files
authored
Add setApplicationMuted and setApplicationVolume methods (#228)
1 parent 19acd57 commit e3a7b15

7 files changed

Lines changed: 159 additions & 6 deletions

File tree

README.md

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ AdMob.addListener(RewardAdPluginEvents.Rewarded, async () => {
254254

255255
* [`initialize(...)`](#initialize)
256256
* [`trackingAuthorizationStatus()`](#trackingauthorizationstatus)
257+
* [`setApplicationMuted(...)`](#setapplicationmuted)
258+
* [`setApplicationVolume(...)`](#setapplicationvolume)
257259
* [`showBanner(...)`](#showbanner)
258260
* [`hideBanner()`](#hidebanner)
259261
* [`resumeBanner()`](#resumebanner)
@@ -317,6 +319,36 @@ Confirm requestTrackingAuthorization status (iOS >14)
317319
--------------------
318320

319321

322+
### setApplicationMuted(...)
323+
324+
```typescript
325+
setApplicationMuted(options: ApplicationMutedOptions) => Promise<void>
326+
```
327+
328+
Report application mute state to AdMob SDK
329+
330+
| Param | Type |
331+
| ------------- | --------------------------------------------------------------------------- |
332+
| **`options`** | <code><a href="#applicationmutedoptions">ApplicationMutedOptions</a></code> |
333+
334+
--------------------
335+
336+
337+
### setApplicationVolume(...)
338+
339+
```typescript
340+
setApplicationVolume(options: ApplicationVolumeOptions) => Promise<void>
341+
```
342+
343+
Report application volume to AdMob SDK
344+
345+
| Param | Type |
346+
| ------------- | ----------------------------------------------------------------------------- |
347+
| **`options`** | <code><a href="#applicationvolumeoptions">ApplicationVolumeOptions</a></code> |
348+
349+
--------------------
350+
351+
320352
### showBanner(...)
321353

322354
```typescript
@@ -727,6 +759,20 @@ addListener(eventName: RewardAdPluginEvents.Showed, listenerFunc: () => void) =>
727759
| **`status`** | <code>'authorized' \| 'denied' \| 'notDetermined' \| 'restricted'</code> |
728760

729761

762+
#### ApplicationMutedOptions
763+
764+
| Prop | Type | Description |
765+
| ----------- | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
766+
| **`muted`** | <code>boolean</code> | To inform the SDK that the app volume has been muted. Note: Video ads that are ineligible to be shown with muted audio are not returned for ad requests made, when the app volume is reported as muted or set to a value of 0. This may restrict a subset of the broader video ads pool from serving. |
767+
768+
769+
#### ApplicationVolumeOptions
770+
771+
| Prop | Type | Description |
772+
| ------------ | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
773+
| **`volume`** | <code>0 \| 1 \| 0.1 \| 0.2 \| 0.3 \| 0.4 \| 0.5 \| 0.6 \| 0.7 \| 0.8 \| 0.9</code> | 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. enable set 0.0 - 1.0, any float allowed. |
774+
775+
730776
#### BannerAdOptions
731777

732778
This interface extends <a href="#adoptions">AdOptions</a>
@@ -756,7 +802,7 @@ When notice listener of OnAdLoaded, you can get banner size.
756802

757803
#### AdMobError
758804

759-
For more information
805+
For more information
760806
https://developers.google.com/android/reference/com/google/android/gms/ads/AdError
761807

762808
| Prop | Type | Description |
@@ -791,7 +837,7 @@ https://developers.google.com/android/reference/com/google/android/gms/ads/AdErr
791837

792838
#### AdMobRewardItem
793839

794-
For more information
840+
For more information
795841
https://developers.google.com/admob/android/rewarded-video-adapters?hl=en
796842

797843
| Prop | Type | Description |
@@ -812,9 +858,7 @@ https://developers.google.com/admob/android/rewarded-video-adapters?hl=en
812858

813859
From T, pick a set of properties whose keys are in the union K
814860

815-
<code>{
816-
[P in K]: T[P];
817-
}</code>
861+
<code>{ [P in K]: T[P]; }</code>
818862

819863

820864
### Enums

android/src/main/java/com/getcapacitor/community/admob/AdMob.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,28 @@ public void trackingAuthorizationStatus(final PluginCall call) {
7373
call.resolve(response);
7474
}
7575

76+
@PluginMethod
77+
public void setApplicationMuted(final PluginCall call) {
78+
Boolean muted = call.getBoolean("muted");
79+
if (muted == null) {
80+
call.reject("muted property cannot be null");
81+
return;
82+
}
83+
MobileAds.setAppMuted(muted);
84+
call.resolve();
85+
}
86+
87+
@PluginMethod
88+
public void setApplicationVolume(final PluginCall call) {
89+
Float volume = call.getFloat("volume");
90+
if (volume == null) {
91+
call.reject("volume property cannot be null");
92+
return;
93+
}
94+
MobileAds.setAppVolume(volume);
95+
call.resolve();
96+
}
97+
7698
// Show a banner Ad
7799
@PluginMethod
78100
public void showBanner(final PluginCall call) {

demo/angular/src/app/app.component.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ export class AppComponent {
2424
testingDevices: ['2077ef9a63d2b398840261c8221a0c9b'],
2525
initializeForTesting: true,
2626
});
27+
28+
AdMob.setApplicationMuted({
29+
muted: false,
30+
});
31+
32+
AdMob.setApplicationVolume({
33+
volume: 0.5,
34+
});
2735
});
2836
}
2937
}

ios/Plugin/Plugin.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
CAP_PLUGIN(AdMob, "AdMob",
77
CAP_PLUGIN_METHOD(initialize, CAPPluginReturnPromise);
88
CAP_PLUGIN_METHOD(trackingAuthorizationStatus, CAPPluginReturnPromise);
9+
CAP_PLUGIN_METHOD(setApplicationMuted, CAPPluginReturnPromise);
10+
CAP_PLUGIN_METHOD(setApplicationVolume, CAPPluginReturnPromise);
911
CAP_PLUGIN_METHOD(showBanner, CAPPluginReturnPromise);
1012
CAP_PLUGIN_METHOD(resumeBanner, CAPPluginReturnPromise);
1113
CAP_PLUGIN_METHOD(hideBanner, CAPPluginReturnPromise);

ios/Plugin/Plugin.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ public class AdMob: CAPPlugin {
4848
call.resolve([:])
4949
}
5050
}
51+
52+
@objc func setApplicationMuted(_ call: CAPPluginCall) {
53+
if let shouldMute = call.getBool("muted") {
54+
GADMobileAds.sharedInstance().applicationMuted = shouldMute
55+
call.resolve([:])
56+
} else {
57+
call.reject("muted property cannot be null");
58+
return;
59+
}
60+
}
61+
62+
@objc func setApplicationVolume(_ call: CAPPluginCall) {
63+
if var volume = call.getFloat("volume") {
64+
//Clamp volumes.
65+
if (volume < 0.0) {volume = 0.0}
66+
else if (volume > 1.0) {volume = 1.0}
67+
68+
GADMobileAds.sharedInstance().applicationVolume = volume
69+
70+
call.resolve([:])
71+
} else {
72+
call.reject("volume property cannot be null");
73+
return;
74+
}
75+
}
5176

5277
/**
5378
* AdMob: Banner

src/definitions.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ export interface AdMobPlugin extends AdMobDefinitions {
2424
* @since 3.1.0
2525
*/
2626
trackingAuthorizationStatus(): Promise<TrackingAuthorizationStatusInterface>;
27+
28+
/**
29+
* Report application mute state to AdMob SDK
30+
*
31+
* @see https://developer.apple.com/documentation/apptrackingtransparency/attrackingmanager/3547038-trackingauthorizationstatus
32+
* @since 4.1.1
33+
*/
34+
setApplicationMuted(options: ApplicationMutedOptions): Promise<void>;
35+
36+
/**
37+
* Report application volume to AdMob SDK
38+
*
39+
* @see https://developer.apple.com/documentation/apptrackingtransparency/attrackingmanager/3547038-trackingauthorizationstatus
40+
* @since 4.1.1
41+
*/
42+
setApplicationVolume(options: ApplicationVolumeOptions): Promise<void>;
2743
}
2844

2945
export interface AdMobInitializationOptions {
@@ -103,3 +119,27 @@ export enum MaxAdContentRating {
103119
*/
104120
MatureAudience = 'MatureAudience',
105121
}
122+
123+
export interface ApplicationMutedOptions {
124+
/**
125+
* To inform the SDK that the app volume has been muted.
126+
* Note: Video ads that are ineligible to be shown with muted audio are not returned for ad requests made,
127+
* when the app volume is reported as muted or set to a value of 0. This may restrict a subset of the broader video ads pool from serving.
128+
*
129+
* @see https://developers.google.com/admob/android/global-settings
130+
* @since 4.1.1
131+
*/
132+
muted?: boolean;
133+
}
134+
135+
export interface ApplicationVolumeOptions {
136+
/**
137+
* If your app has its own volume controls (such as custom music or sound effect volumes),
138+
* disclosing app volume to the Google Mobile Ads SDK allows video ads to respect app volume settings.
139+
* enable set 0.0 - 1.0, any float allowed.
140+
*
141+
* @see https://developers.google.com/admob/android/global-settings
142+
* @since 4.1.1
143+
*/
144+
volume?: 0.0 | 0.1 | 0.2 | 0.3 | 0.4 | 0.5 | 0.6 | 0.7 | 0.8 | 0.9 | 1.0;
145+
}

src/web.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { WebPlugin } from '@capacitor/core';
22

3-
import type { AdMobPlugin } from '.';
3+
import type {
4+
AdMobPlugin,
5+
ApplicationMutedOptions,
6+
ApplicationVolumeOptions,
7+
} from '.';
48
import type { AdMobRewardItem } from './reward';
59
import type { AdOptions, AdLoadInfo } from './shared';
610
import type { TrackingAuthorizationStatusInterface } from './shared/tracking-authorization-status.interface';
@@ -27,6 +31,14 @@ export class AdMobWeb extends WebPlugin implements AdMobPlugin {
2731
};
2832
}
2933

34+
async setApplicationMuted(options: ApplicationMutedOptions): Promise<void> {
35+
console.log('setApplicationMuted', options);
36+
}
37+
38+
async setApplicationVolume(options: ApplicationVolumeOptions): Promise<void> {
39+
console.log('setApplicationVolume', options);
40+
}
41+
3042
async showBanner(options: AdOptions): Promise<void> {
3143
console.log('showBanner', options);
3244
}

0 commit comments

Comments
 (0)