【问题标题】:How to mute my app programmatically (disable sounds)如何以编程方式使我的应用程序静音(禁用声音)
【发布时间】:2017-11-01 05:39:47
【问题描述】:

我正在创建一个隐藏广告的应用浏览器。快完成了。有时浏览器会自动播放声音(可能是 JavaScript 驱动的)

现在我需要关闭此应用中的所有声音。我尝试了两种方法:通过 AVFoundation 和通过 WebKit。

请帮助我了解如何做到这一点。欢迎使用更多变体:)

【问题讨论】:

    标签: ios swift xcode webkit avfoundation


    【解决方案1】:

    这可以通过MediaPlayer 模块完成:

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

    注意您可以更改的两个值:

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

    【讨论】:

    • 谢谢!不幸的是,由于我的声誉低,我无法评价您的回答。这对我有用。我也想知道 - 我怎样才能使我的应用程序静音以避免关闭背景音乐(来自音频播放器)像这样:音乐播放器正在后台播放,而这个应用程序同时被静音。
    • @AndreySinyagin 您可以单击复选标记以接受答案。关于您的其他问题,我不确定您是否可以这样做。
    【解决方案2】:

    Swift 5 / Xcode 11

    第 1 步:

    import MediaPlayer
    

    第 2 步:

    let slider = MPVolumeView().subviews.first(where: { $0 is UISlider }) as? UISlider
    

    第 3 步:

    slider?.setValue(0.0, animated: false)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      相关资源
      最近更新 更多