Search code examples
iosswiftxcodewebkitavfoundation

How to mute my app programmatically (disable sounds)


I'm creating an app-browser that hides advertising. Almost done. Sometimes there are autoplaying sounds in browser(probably powered with JavaScript)

Now I need to turn off all sounds in this app. I've tried two ways: through AVFoundation and through WebKit.

Please help me to get idea how I can do this. One more variants are were welcome :)


Solution

  • This can be done through the MediaPlayer module:

    (MPVolumeView().subviews.filter{ NSStringFromClass($0.classForCoder) == "MPVolumeSlider" }.first as? UISlider)?.setValue(0, animated: false)
    

    Note the two values you can change:

    value: how much volume you want (0 in this case)
    animated: allowing you to animate the change visually