Closed EmilianoMorghen closed 1 year ago
:wave: Hi there, I've spent some time examining the native code for react-native-video and also doing some research in the Google IMA SDK documentation and on the web. I've come to the conclusion that on iOS devices, ads need to be muted by setting the volume to 0 using the IMAAdsManager instance. It appears that this is due to a limitation in the way that iOS handles audio playback during ads. If I'm not wrong, the SDK uses WebViews to display ads on iOS, while on Android, the SDK uses ExoPlayer which has the ability to set the ads to mute without any issues.
I think I may have found a patch that could resolve the problem we're experiencing. I've tested it on my end and it seems to be working fine, but I'd like if you could try it out and let me know if it works for you as well.
diff --git a/ios/Video/Features/RCTIMAAdsManager.swift b/ios/Video/Features/RCTIMAAdsManager.swift
index 059ec63..2188485 100644
--- a/ios/Video/Features/RCTIMAAdsManager.swift
+++ b/ios/Video/Features/RCTIMAAdsManager.swift
@@ -76,6 +76,10 @@ class RCTIMAAdsManager: NSObject, IMAAdsLoaderDelegate, IMAAdsManagerDelegate {
// MARK: - IMAAdsManagerDelegate
func adsManager(_ adsManager: IMAAdsManager, didReceive event: IMAAdEvent) {
+ // Mute ad if the main player is muted
+ if (_video.isMuted()) {
+ adsManager.volume = 0;
+ }
// Play each ad once it has been loaded
if event.type == IMAAdEventType.LOADED {
adsManager.start()
diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift
index be1c098..cc0df2c 100644
--- a/ios/Video/RCTVideo.swift
+++ b/ios/Video/RCTVideo.swift
@@ -496,6 +496,11 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
applyModifiers()
}
+ @objc
+ func isMuted() -> Bool {
+ return _muted
+ }
+
@objc
func setMuted(_ muted:Bool) {
_muted = muted
If the patch works and looks good for you, I can submit a PR to get it merged into the codebase. Let me know your thoughts.
Thanks!
Hello, thank you for the patch. I definitely think you should open a PR with that change ! Just a thing, if you change volume/ mute state during ad playback, you should also apply the same logic ? (Change volume on adManager)
hi @freeboub, thanks for your reply. I had thought of managing that case as well, but actually, with the current library, there is no command for volume or mute during the playback of the ad. Therefore, the user cannot activate/deactivate the audio during the playback of the ad. Am I missing something? Maybe you could give me a suggestion to solve this?
Bug
As you can see from the script, ads on iOS only, will be reproduced with audio ignoring
muted
orvolume
props. In Android this doesn't happen.Platform
Environment info
React native info output:
Library version: v6.0.0-alpha.5
Expected behaviour
Audio of the ad should play muted
Reproducible sample code